1. 程式人生 > 實用技巧 >阿里雲centos7[linux]安裝nginx

阿里雲centos7[linux]安裝nginx

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

一、準備

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

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

或者

在 http://nginx.org/download/ 直接下載對應版本安裝包上傳到伺服器資料夾內

Q: 如何獲取下載版本?

A: 在此網站中(http://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

出現如下錯誤

./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. 編譯nginx
當前目錄下執行make命令,該命令是將原始檔編譯為可執行檔案

[root@fanzyx nginx-1.19.6]# make

編譯成功,如圖所示、

  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. 重啟
安裝路徑 -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]#