创建nginx启动命令脚本
vi /etc/init.d/nginx
输入以下内容,安装位置按情况修改
#!/bin/bash
# chkconfig: - 85 15
#nginx安装位置
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 --list | grep nginx
设置开机自启动
chkconfig nginx on
如果要测试服务自启,可以重启,然后查看nginx服务是否自动启动
reboot netstat -anput | grep nginx
启动nginx服务
service nginx start
停止nginx服务
service nginx stop
重启nginx服务
service nginx restart
重新加载nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
service nginx reload



