1. 程式人生 > 實用技巧 >設定nginx開啟時候啟動

設定nginx開啟時候啟動

1》建立nginx啟動命令指令碼

1 vi /etc/init.d/nginx

2》插入以下內容, 注意修改PATH和NAME欄位, 匹配自己的安裝路徑 (這段是從網上copy的)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #! /bin/bash # chkconfig: - 85 15 PATH=/usr/local/nginx DESC=
"nginx daemon" NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/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() { $DAEMON -s stop || echo -n "nginx not running" } do_reload() {
$DAEMON -s reload || 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

3》設定執行許可權

1 chmod a+x /etc/init.d/nginx

4》註冊成服務

1 chkconfig --add nginx

5》設定開機啟動

1 chkconfig nginx on

6重啟, 檢視nginx服務是否自動啟動

1 2 shutdown -h now netstat -apn|grep nginx

對nginx服務執行停止/啟動/重新讀取配置檔案操作

1 2 3 4 5 6 7 8 #啟動nginx服務 systemctl start nginx.service #停止nginx服務 systemctl stop nginx.service #重啟nginx服務 systemctl restart nginx.service #重新讀取nginx配置(這個最常用, 不用停止nginx服務就能使修改的配置生效) systemctl reload nginx.service