1. 程式人生 > >CentOS7安裝及配置Nginx服務

CentOS7安裝及配置Nginx服務

set nta type 安裝 clas Opens 創建 emctl 失敗

Nginx是CentOS中最常用的HTTP服務程序之一,以下針對CentOS7下Nginx二進制包安裝、編譯安裝兩個場景分別予以說明。


一、二進制包安裝方式


在安裝CentOS7時選擇同時安裝Nginx或者後來通過yum命令安裝Nginx應用,得到的Nginx的安裝路徑、默認配置都是一樣的,並且已經配置好Nginx服務,可通過如命令查看Nginx相關進程:

ps -ef | grep nginx

並可通過systemctl xx命令管理Nginx服務(xx命令可以是:enable-開機自動啟動,disable-開機不自動啟動,start-啟動,status-查看狀態,stop-停止),例如啟動Nginx服務:

systemctl start nginx

查看版本,執行:

nginx -V

查看Nginx的服務信息,包括二進制、配置文件路徑,執行如下命令:

systemctl status nginx

結果顯示:

● nginx.service - The nginx HTTP and reverse proxy server   
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)   
Active: active (running) since Wed 2018-07-04 22:13:33 CST; 50min ago  
Process: 964 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)  
Process: 914 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)  
Process: 909 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) 
Main PID: 966 (nginx)   CGroup: /system.slice/nginx.service           
├─966 nginx: master process /usr/sbin/nginx           
├─967 nginx: worker process           
└─968 nginx: worker process
Jul 04 22:13:33 centos.vm0 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 22:13:33 centos.vm0 systemd[1]: Started The nginx HTTP and reverse proxy server.

只查看配置文件路徑,可通過命令:

nginx -t

結果顯示:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


二、編譯安裝方式


1、準備源碼包到服務器目錄

可從Nginx的網站(http://nginx.org/en/download.html) 下載與當前CentOS7配套的源碼包(例如nginx-1.14.0.tar.gz),然後上傳到CentOS7服務器,並解壓,例如:

tar -zxvf nginx-1.14.0.tar.gz

2、檢查gcc

執行如下命令:

gcc --version

如果報錯,說明未安裝gcc,需先通過如下命令安裝gcc

yum install gcc

3、檢查PCRE

進入到解壓後的源碼目錄:

cd nginx-1.14.0/

執行configure命令:

./configure

如果configure失敗,如果是缺少目錄,需根據目錄名並補充創建缺少的目錄,如果是缺少PCRE庫,通過如下命令安裝PCRE依賴庫:

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

4、正常編譯及安裝Nginx

在解壓後的源碼目錄,為了確保執行正常,請以root身份依次執行如下命令:

./configure
make
make install

以上操作後Nginx將被安裝在/usr/local/nginx目錄下

5、普通方式啟動Nginx程序

進入/usr/local/nginx/sbin目錄下:

cd /usr/local/nginx/sbin

執行:

./nginx -c /usr/local/nginx/conf/nginx.conf

註:上述可選參數-c後面指定了/usr/local/nginx/conf/nginx.conf為配置文件

6、普通方式停止Nginx程序

進入/usr/local/nginx/sbin目錄後,如立即停止Nginx並退出,可執行:

./nginx -s stop

如優雅停止Nginx並退出,可執行

./nginx -s quit

7、重新加載nginx.conf配置文件

進入/usr/local/nginx/sbin目錄後,執行:

./nginx -s reload

8、將Nginx配置服務可以通過systemctl操作的服務

CentOS7的服務systemctl腳本存放在:/usr/lib/systemd/,有系統(system)和用戶(user)之分,需要開機不登陸就能運行的程序,存在系統服務裏,即:/usr/lib/systemd/system目錄下.
CentOS7的每一個服務以.service結尾,一般會分為3部分:[Unit]、[Service]和[Install],對Nginx服務,通過vi命令打開服務配置:

vi /usr/lib/systemd/system/nginx.service

配置文件nginx.service的內容示例如下:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
#  
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginxExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target


建議在修改之前,如果舊的文件已經存在,可以先做好備份,例如:

cp /usr/lib/systemd/system/nginx.service /usr/local/nginx/nginx.service.bak

之後將nginx.service內容中的路徑修改為正確的路徑即可,例如:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
#  
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUITTimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]WantedBy=multi-user.target

註:pid文件的路徑,在編譯安裝的情況下,默認位置一般為/usr/local/nginx/logs/nginx.pid(即nginx.conf中不設置pid路徑時的情況),在二進制包安裝方式下,默認位置一般為/run/nginx.pid(相應的nginx.conf中有一行pid /run/nginx.pid;),這個位置在/usr/lib/systemd/system/nginx.service中配置需與nginx.conf文件中的保持一致;

之後需要檢查Nginx目錄下的相關權限:

[root@centos hnl]# cd /usr/local/nginx
[root@centos nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@centos nginx]# ll
total 4
drwx------. 2 nobody root    6 Jul  4 22:43 client_body_temp
drwxr-xr-x. 2 root   root 4096 Jul  5 00:45 conf
drwx------. 2 nobody root    6 Jul  4 22:43 fastcgi_temp
drwxr-xr-x. 2 root   root   40 Jul  4 22:43 html
drwxr-xr-x. 2 root   root   58 Jul  5 01:11 logs
drwx------. 2 nobody root    6 Jul  4 22:43 proxy_temp
dr-xr-xr-x. 2 root   root   19 Jul  4 22:43 sbin
drwx------. 2 nobody root    6 Jul  4 22:43 scgi_temp
drwx------. 2 nobody root    6 Jul  4 22:43 uwsgi_temp
[root@centos nginx]# ll sbin
total 3660
-rwxr-xr-x. 1 root root 3746616 Jul  4 22:43 nginx

為了配置服務自動啟動,執行

sysemctl enable nginx

然後重啟CentOS,重啟完成後,參考一中的命令檢查Nginx的運行情況:

ps -ef |grep nginx
systemctl status nginx



CentOS7安裝及配置Nginx服務