Nginx开机自动启动

不指定
2013-11-30 21:51 by:果面 PC应用 » 网络应用 本站原创 | |
把以下脚本保存为nginx文件放入/etc/init.d/nginx,然后修改权限:

chmod 755 nginx


就可以通过常规命令控制Nginx状态:

#启动nginx
/etc/init.d/nginx start

#停止nginx
/etc/init.d/nginx stop

#重启nginx
/etc/init.d/nginx restart

#仅重新读取配置
/etc/init.d/nginx reload



如果需要开机自动启动nginx服务,保存好 /etc/init.d/nginx文件后,执行以下命令:


chkconfig --add nginx
chkconfig --level 2345 nginx on


脚本内容

#! /bin/sh
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# chkconfig: - 85 15
# Description: Nginx Web Server

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# If have Luajit Module
# export LD_LIBRARY_PATH=/path/to/luajit/lib/:$LD_LIBRARY_PATH
DESC="nginx daemon"
NAME=nginx
DAEMON=/etc/nginx/$NAME
CONFIGFILE=/etc/nginx/conf/$NAME.conf
PIDFILE=/etc/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


其中


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


按照实际情况配置路径。
评论(0) 引用(0) 阅读(5866)