阿里雲centos7[linux]安裝nginx

標題 說明
服務器版本 Centos7 x64
nginx版本 1.19.6
作者 walton

一、準備

  1. 創建安裝包目錄並進入
mkdir /usr/dev/nginx

cd /usr/dev/nginx
  1. 下載安裝包
wget //nginx.org/download/nginx-1.19.6.tar.gz

或者

在 //nginx.org/download/ 直接下載對應版本安裝包上傳到服務器文件夾內

Q: 如何獲取下載版本?

A: 在此網站中(//nginx.org/download/)獲取版本,然後修改wget命令後的版本號

  1. 解壓安裝包並進入
tar -zxvf nginx-1.19.6.tar.gz

cd /nginx-1.19.6

二、安裝

  1. 設置安裝路徑(默認路徑:/usr/local/nginx)
[root@fanzyx nginx-1.19.6]#  ./configure --prefix=/usr/local/nginx

出現如下錯誤

1-1

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

需要安裝openssl,執行以下命令

[root@fanzyx nginx-1.19.6]# yum -y install openssl openssl-devel

再次執行命令
[root@fanzyx nginx-1.19.6]# ./configure --prefix=/usr/local/nginx

1-2

  1. 編譯nginx
當前目錄下執行make命令,該命令是將源文件編譯為可執行文件

[root@fanzyx nginx-1.19.6]# make

編譯成功,如圖所示、

1-3

  1. 安裝文件

將編譯後的文件複製到設置的安裝目錄

[root@fanzyx nginx-1.19.6]# make install

三、相關命令

  1. 啟動

為nginx指定配置文件路徑並啟動

[root@fanzyx nginx-1.19.6]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  1. 停止
方法一:
[root@fanzyx nginx-1.19.6]# /usr/local/nginx/sbin/nginx -s stop
方法二:
查詢nginx進程
ps -ef | grep nginx
[root@fanzyx nginx-1.19.6]# ps -ef | grep nginx
root     15038     1  0 11:44 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   15039 15038  0 11:44 ?        00:00:00 nginx: worker process
root     15084  9881  0 11:45 pts/0    00:00:00 grep --color=auto nginx

在進程列表中帶master的進程就是主進程

kill -QUIT 主進程號
eg: kill -QUIT 15038

再次執行查詢命令發現主進程已消失

[root@fanzyx nginx-1.19.6]# kill -QUIT 15038
[root@fanzyx nginx-1.19.6]# ps -ef | grep nginx
root     15271  9881  0 11:50 pts/0    00:00:00 grep --color=auto nginx
[root@fanzyx nginx-1.19.6]# 

1-4

  1. 重啟
安裝路徑 -s reload

/usr/local/nginx/sbin/nginx -s reload
  1. 查看配置是否正確
安裝路徑 -t

/usr/local/nginx/sbin/nginx -t

如下所示配置成功
[root@fanzyx nginx-1.19.6]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@fanzyx nginx-1.19.6]# 


Tags: