首先停止nginx /usr/local/nginx/sbin/nginx -s stop
在linux系统的/etc/init.d/目录下创建nginx文件
1、vim /etc/init.d/nginx
在脚本中添加如下命令:
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx #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
你也可以去nginx脚本地址下载使用:https://wiki.nginx.org/RedHatNginxInitScript
修改上述两项配置项
DAEMON 修改成nginx执行程序的路径。
CONFIGFILE 修改成配置文件的路径。
保存文件后设置文件执行权限
1、chmod a+x /etc/init.d/nginx
然后,就可以通过该脚本对nginx服务进行管理了:
1、/etc/init.d/nginx start
2、/etc/init.d/nginx stop
使用chkconfig进行管理
先将nginx服务加入chkconfig管理列表:
1、chkconfig --add /etc/init.d/nginx
加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
1、service nginx start
2、service nginx stop
设置终端模式开机启动:
1、chkconfig nginx on
到此nginx设置开启自启完成