Nginx啟動/重啟指令碼詳解,nginx重啟指令碼詳解

來源:互聯網
上載者:User

Nginx啟動/重啟指令碼詳解,nginx重啟指令碼詳解

Nginx手動啟動

停止操作

停止操作是通過向nginx進程發送訊號(什麼是訊號請參閱linux文 章)來進行的
步驟1:查詢nginx主進程號
ps -ef | grep nginx
在進程列表裡 面找master進程,它的編號就是主進程號了。
步驟2:發送訊號
從容停止Nginx:
kill -QUIT 主進程號
快速停止Nginx:
kill -TERM 主進程號
強制停止Nginx:
pkill -9 nginx

另外, 若在nginx.conf配置了pid檔案存放路徑則該檔案存放的就是Nginx主進程號,如果沒指定則放在nginx的logs目錄下。有了pid文 件,我們就不用先查詢Nginx的主進程號,而直接向Nginx發送訊號了,命令如下:
kill -訊號類型 '/usr/nginx/logs/nginx.pid'


命令:/usr/local/nginx/sbin/nginx

若出現:

[root@kangxiaowei ~]# /usr/local/nginx/sbin/nginx
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: still could not bind()

則再次執行 /root/lnmp stop關閉lnmp即可

 

Nginx的開機啟動指令碼

開機自動啟動nginx,

如果需要開機啟動服務,儲存好 /etc/init.d/nginx檔案後,

執行以下命令:

代碼如下 複製代碼
chkconfig --add ningx

chkconfig --level nginx 2345 on

開機自動啟動指令碼

代碼如下 複製代碼
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Descri(www.111cn.net)ption: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/platform/nginx/sbin/$NAME
CONFIGFILE=/usr/local/platform/nginx/conf/$NAME.conf
PIDFILE=/usr/local/platform/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}

do_reload() {
kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac

exit 0

需要你修改的配置有

代碼如下 複製代碼
#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin


NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid


編輯好後儲存,執行以下命令

代碼如下 複製代碼
1 chmod +x /etc/init.d/nginx

現在把Nginx加入chkconfig,並設定開機啟動。

代碼如下 複製代碼
12 chkconfig --add nginx chkconfig nginx on

# 檢查一下

代碼如下 複製代碼
1 chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

from:http://www.111cn.net/sys/nginx/52908.htm


nginx怎啟動平滑重啟關閉

假設你的nginx的目錄是:/usr/opt/nginx/sbin/nginx 假設nginx的目錄是: /usr/opt/nginx/conf/www4.conf 重啟的方法是:/usr/opt/nginx/sbin/nginx -c /usr/opt/nginx/conf/www4.conf 參數-c 指定了設定檔的路徑,如果不指定該參數,則nginx會預設載入其按照目錄的conf目錄下的nginx.conf. 平滑重啟: 1、檢查修改後的設定檔是否正確: /usr/opt/nginx/sbin/nginx -t -c /usr/opt/nginx/conf/www4.conf 2、kill -HUP Nginx主進程號。樣本如下: kill -HUP `cat/usr/local/webserver/nginx/logs/nginx.pid`主要參數介紹如下:-c 為Nginx 指定一個設定檔,來代替預設的。-t 不運行,而僅僅測試組態檔案。
 
linux下怎重啟nginx

在nginx的早期版本,nginx重啟需要通過kill命令向nginx發送訊號來實現nginx的重啟。
但是,現在,nginx增加了-s選項,實現nginx的停止,重新載入功能。
1. 如果是平滑的重啟nginx,可以用./nginx -s reload命令實現nginx的平滑重啟。
2. 如果是非平滑重啟,則可以先停止nginx,然後再啟動:
./nginx -s stop && ./nginx

當我們修改nginx配置後,希望重啟nginx以便讓nginx生效,此時為了保證nginx在重啟階段還能夠提供正常的服務,一般採用平滑重啟的方式(reload)重啟nginx。此時,nginx會載入新的配置,然後fork出新的worker進程。同時,master進程會向老的worker進程發送訊號,告訴老的worker進程當前的情況。老的worker進程受到master進程的訊號後,如果當時沒有處理請求則會退出,如果正在處理請求,則老的worker進程會處理完請求然後退出。nginx就是通過這種方式去reload新的配置,從而使得在重啟的過程中,仍然可以提供服務。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.