os:centos 6.8
1.在该目录下”/etc/init.d”新建一个nginx文件,添加下列代码。
[root@localhost init.d]# vi nginx
[root@localhost init.d]# cat nginx
#nginx startup script for the nginx http server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: nginx is a high-performance web and proxy server.
# it has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/application/nginx/sbin/nginx
nginx_config=/application/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
retval=0
prog=nginx
# source function library.
./etc/rc.d/init.d/functions
# source networking configuration.
./etc/sysconfig/network
# check that networking is up.
[ ${networking} = no ] && exit 0
[ -x $nginxd ] || exit 0
# start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo nginx already running....
exit 1
fi
echo -n $starting $prog:
daemon $nginxd -c ${nginx_config}
retval=$?
echo
[ $retval = 0 ] && touch /var/lock/subsys/nginx
return $retval
}
# stop nginx daemons functions.
stop() {
echo -n $stopping $prog:
killproc $nginxd
retval=$?
echo
[ $retval = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $reloading $prog:
#kill -hup `cat ${nginx_pid}`
killproc $nginxd -hup
retval=$?
echo
}
# see how we were called.
case $1 in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
retval=$?
;;
*)
echo $usage: $prog {start|stop|restart|reload|status|help}
exit 1
esac
exit $retval
2.设置开机启动服务,保存好 /etc/init.d/nginx文件后,执行以下命令:
[root@localhost init.d]# chmod 755 /etc/init.d/nginx #赋予文件执行权限
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 2345 nginx on #设置开机启动
3、nginx启动、停止、无间断服务重启
[root@localhost /]# service nginx status
nginx (pid 1621 1619) 正在运行...
[root@localhost /]# service nginx stop
停止 nginx: [确定]
[root@localhost /]# service nginx start
正在启动 nginx: [确定]
[root@localhost /]# service nginx reload
重新载入 nginx: [确定]
