1. 程式人生 > >[Nginx日記](1)Nginx啟動與關閉

[Nginx日記](1)Nginx啟動與關閉

1. Nginx的啟動

在Nginx伺服器搭建成功後,要啟動它,Nginx伺服器才能正常執行。啟動程式碼格式如下:

  1. Nginx地址-c Nginx配置檔案地址

例項:

  1. [email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2. Nginx的停止

Nginx伺服器啟動之後,要想其停止執行,有三種方式,第一種是從容停止,第二種是快速停止,第三種是強制停止。

(1)從容停止

  1. [email protected]:/usr/local
    /nginx/sbin$ ps -ef | grep nginx
  2. root      76552151013:19?00:00:00 nginx: master process/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  3. nobody    76567655013:19?00:00:00 nginx: worker process                                          
  4. xiaosi    84667586014:07 pts/2400:00:00 grep --color=auto nginx
首先我們檢視nginx的程序號,根據程序號來停止。
  1. [email protected]
    :/usr/local/nginx/sbin$ sudo
    kill -QUIT 7655
  2. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  3. xiaosi    85057586014:10 pts/2400:00:00 grep --color=auto nginx

(2)快速停止

  1. [email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  2. [email protected]
    :/usr/local/nginx/sbin$ ps -ef | grep nginx
  3. root      88052151014:24?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  4. nobody    88068805014:24?00:00:00 nginx: worker process                                          
  5. xiaosi    88087586014:24 pts/2400:00:00 grep --color=auto nginx
  1. [email protected]:/usr/local/nginx/sbin$ sudo kill -TERM 8805
  2. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  3. xiaosi    88597586014:26 pts/2400:00:00 grep --color=auto nginx

(3)強制停止

  1. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  2. root      88762151014:27?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  3. nobody    88778876014:27?00:00:00 nginx: worker process                                          
  4. xiaosi    89017586014:28 pts/2400:00:00 grep --color=auto nginx
  1. [email protected]:/usr/local/nginx/sbin$ sudo pkill -9 nginx
  2. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  3. xiaosi    89117586014:28 pts/2400:00:00 grep --color=auto nginx
3. Nginx的重啟

Nginx伺服器在執行的時候,若想對其進行重啟啟動,比如當我們修改了配置檔案,需要讓新配置的檔案生效,就得重啟等。

3.1 在重啟之前我們需要驗證配置檔案是否是正確。

第一種方法:進入nginx的sbin/目錄下:

  1. [email protected]:/usr/local/nginx/sbin$ sudo ./nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

第二種方法:

  1. [email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3.2 重啟
  1. [email protected]:/usr/local/nginx/sbin$ sudo ./nginx -s reload
  2. nginx:[alert] kill(8876,1) failed (3:No such process)

我們可以看到我們重啟失敗,主要是因為我們已經關閉了nginx,只有nginx執行中我們才能重啟,所以我們先啟動nginx。

  1. [email protected]:/usr/local/nginx/sbin$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  2. [email protected]:/usr/local/nginx/sbin$ sudo ./nginx -s reload
  3. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  4. root      94412151014:45?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  5. nobody    94459441014:45?00:00:00 nginx: worker process                                          
  6. xiaosi    94477586014:45 pts/2400:00:00 grep --color=auto nginx

從上面可以看到我們先啟動之後,再重啟就可以了。

我們還可以根據發射訊號的方式重啟nginx:

  1. [email protected]:/usr/local/nginx/sbin$ ps -ef | grep nginx
  2. root      94412151014:45?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  3. nobody    94459441014:45?00:00:00 nginx: worker process                                          
  4. xiaosi    95087586014:48 pts/2400:00:00 grep --color=auto nginx
  5. [email protected]:/usr/local/nginx/sbin$ sudo kill -HUP 9441