nginx 添加到系統服務

創建nginx啓動命令腳本

vi /etc/init.d/nginx

插入以下內容, 注意修改PATH和NAME字段, 匹配自己的安裝路徑 

#! /bin/bash
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

設置執行權限

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

註冊成服務

chkconfig --add nginx

設置開機啓動

chkconfig nginx on

重啓, 查看nginx服務是否自動啓動

shutdown -h 0 -r netstat -apn|grep nginx

對nginx服務執行停止/啓動/重新讀取配置文件操作

#啓動nginx服務

systemctl start nginx.service

#停止nginx服務

systemctl stop nginx.service

#重啓nginx服務

systemctl restart nginx.service

#重新讀取nginx配置(這個最常用, 不用停止nginx服務就能使修改的配置生效)

systemctl reload nginx.service

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章