Linux服务器上配置Nginx服务开机自启动
方法一:编辑/etc/rc.local,添加开机启动运行命令;
# 直接编辑/etc/rc.local文件,文件内容最底下添加启动命令
[root@hecs-223575 ~]# vim /etc/rc.local
# 最后追加内容 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &
方法二:添加/etc/init.d/nginx,通过chkconfig配置开机启动服务;
# 添加/etc/init.d/nginx脚本,通过chkconfig配置开机启动服务
[root@hecs-223575 ~]# touch /etc/init.d/nginx
[root@hecs-223575 ~]# vim /etc/init.d/nginx
nginx脚本具体内容如下:
#!/bin/bash
# chkconfig: 2345 85 15
# description: nginx Startup script for the Nginx HTTP Server
# processname: nginx
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/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
# 测试nginx脚本
# 添加可执行权限:
[root@hecs-223575 ~]# chmod +x /etc/init.d/nginx
[root@hecs-223575 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
[root@hecs-223575 conf]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ OK ]
# 如果出现
[root@hecs-223575 ~]# /etc/init.d/nginx start
Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
[FAILED]
如启动时出现上述错误,则先检查一下nginx 是否已经存在,如果已存在,先将原来的停止即可
[root@hecs-223575 conf]# ps -aux | grep nginx
root 13157 0.0 0.0 42348 800 ? Ss 20:30 0:00 nginx: master process nginx
nginx 13158 0.0 0.2 74528 4824 ? S 20:30 0:00 nginx: worker process
root 13590 0.0 0.0 12136 1196 pts/4 S+ 21:08 0:00 grep --color=auto nginx
#设置开机启动
[root@hecs-223575 ~]# cd /etc/init.d/
[root@hecs-223575 init.d]# chkconfig nginx on
[root@hecs-223575 init.d]# service nginx start
Starting nginx (via systemctl): [ OK ]
至此,即配置完成,可通过chkconfig命令查看系统服务运行状态。
[root@hecs-223575 init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
hostguard 0:off 1:off 2:on 3:on 4:on 5:on 6:off
multi-queue-hw 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off