[Nginx] 在Linux下的啟動、停止和重加載


Nginx的啟動 

 
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
其中-c參數指定配置文件路徑。
 

Nginx的停止

Nginx支持以下幾種信號控制:
- TERM, INT 快速關閉
- QUIT 從容關閉
- HUP 平滑重啟
- USR1 重新打開日志文件,在切割文件時用處大
- USR2 平滑升級
- WINCH 從容關閉工作進程
我們可以通過信號停止Nginx主進程,首先,我們需要通過ps -ef|grep命令獲得master進程的PID,或者通過cat pid文件獲得主進程號。下面是幾個典型的停止語句:
#從容停止Nginx  
kill -QUIT master進程號  
#快速停止Nginx  
kill -TERM master進程號  
#強制停止Nginx  
kill -9 master進程號  

 

 

Nginx的重加載

如果改變了配置文件,想重啟讓其生效,同樣可以通過發送系統信號給Nginx主進程,不過,在重啟之前,要確認配置文件的語法是正確的,否則將不會加載新的配置項。
通過以下語句測試配置文件語法是否正確:
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf  

 

其中-t表示測試,並不真正執行。
然后,通過以下命令重加載Nginx配置:
kill -HUP master進程號 
執行上面命令之后,Nginx運行新的工作進程,舊工作進程繼續為已有的連接服務,等所有舊的連接成功后,舊的工作進程才被關閉。

Nginx的啟動腳本

#!/bin/sh  
# chkconfig: 2345 85 15  
# description:Nginx Server  
  
NGINX_HOME=/usr/local/nginx  
NGINX_SBIN=$NGINX_HOME/sbin/nginx  
NGINX_CONF=$NGINX_HOME/conf/nginx.conf  
NGINX_PID=$NGINX_HOME/logs/nginx.pid  
  
NGINX_NAME="Nginx"  
  
. /etc/rc.d/init.d/functions  
  
if [ ! -f $NGINX_SBIN ]  
then  
    echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "  
    exit  
fi  
  
start() {  
    $NGINX_SBIN -c $NGINX_CONF  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Starting $NGINX_NAME: " /bin/true  
    else  
        action $"Starting $NGINX_NAME: " /bin/false  
    fi  
}  
  
stop() {  
    kill `cat $NGINX_PID`  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Stopping $NGINX_NAME: " /bin/true  
    else  
        action $"Stopping $NGINX_NAME: " /bin/false  
    fi  
}  
  
restart() {  
    stop  
    start  
}  
  
check() {  
    $NGINX_SBIN -c $NGINX_CONF -t  
}  
  
  
reload() {  
    kill -HUP `cat $NGINX_PID` && echo "reload success!"  
}  
  
relog() {  
    kill -USR1 `cat $NGINX_PID` && echo "relog success!"  
}  
  
case "$1" in  
    start)  
        start  
        ;;  
    stop)  
        stop  
        ;;  
    restart)  
        restart  
        ;;  
    check|chk)  
        check  
        ;;  
    status)  
        status -p $NGINX_PID  
        ;;  
    reload)  
        reload  
        ;;  
    relog)  
        relog  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"  
        exit 1  
esac
上面是nginx的啟動腳本,只要把它拷貝至/etc/init.d目錄下,就可以通過service nginx start等目錄操作nginx。

除了上面介紹的直接發信號給Nginx主進程的方法之外,我們還可以通過nginx -s命令:
  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM