[Nginx日記](1)Nginx啟動與關閉
阿新 • • 發佈:2019-01-02
1. Nginx的啟動
在Nginx伺服器搭建成功後,要啟動它,Nginx伺服器才能正常執行。啟動程式碼格式如下:
Nginx地址-c Nginx配置檔案地址
例項:
[email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2. Nginx的停止
Nginx伺服器啟動之後,要想其停止執行,有三種方式,第一種是從容停止,第二種是快速停止,第三種是強制停止。
(1)從容停止
[email protected]:/usr/local
root 76552151013:19?00:00:00 nginx: master process/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 76567655013:19?00:00:00 nginx: worker process
xiaosi 84667586014:07 pts/2400:00:00 grep --color=auto nginx
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 85057586014:10 pts/2400:00:00 grep --color=auto nginx
(2)快速停止
[email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[email protected]
root 88052151014:24?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 88068805014:24?00:00:00 nginx: worker process
xiaosi 88087586014:24 pts/2400:00:00 grep --color=auto nginx
[email protected]:/usr/local/nginx/sbin$ sudo kill -TERM 8805
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 88597586014:26 pts/2400:00:00 grep --color=auto nginx
(3)強制停止
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 88762151014:27?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 88778876014:27?00:00:00 nginx: worker process
xiaosi 89017586014:28 pts/2400:00:00 grep --color=auto nginx
[email protected]:/usr/local/nginx/sbin$ sudo pkill -9 nginx
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
xiaosi 89117586014:28 pts/2400:00:00 grep --color=auto nginx
3. Nginx的重啟
Nginx伺服器在執行的時候,若想對其進行重啟啟動,比如當我們修改了配置檔案,需要讓新配置的檔案生效,就得重啟等。
3.1 在重啟之前我們需要驗證配置檔案是否是正確。
第一種方法:進入nginx的sbin/目錄下:
[email protected]:/usr/local/nginx/sbin$ sudo ./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
第二種方法:
[email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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
3.2 重啟
[email protected]:/usr/local/nginx/sbin$ sudo ./nginx -s reload
nginx:[alert] kill(8876,1) failed (3:No such process)
我們可以看到我們重啟失敗,主要是因為我們已經關閉了nginx,只有nginx執行中我們才能重啟,所以我們先啟動nginx。
[email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[email protected]:/usr/local/nginx/sbin$ sudo ./nginx -s reload
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 94412151014:45?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 94459441014:45?00:00:00 nginx: worker process
xiaosi 94477586014:45 pts/2400:00:00 grep --color=auto nginx
從上面可以看到我們先啟動之後,再重啟就可以了。
我們還可以根據發射訊號的方式重啟nginx:
[email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 94412151014:45?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 94459441014:45?00:00:00 nginx: worker process
xiaosi 95087586014:48 pts/2400:00:00 grep --color=auto nginx
[email protected]:/usr/local/nginx/sbin$ sudo kill -HUP 9441