Linux服务器上配置Nginx服务开机自启动
Nginx安装完毕后,正常启动命令:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
或
/usr/local/nginx/sbin/nginx
Nginx启动默认nginx.conf配置文件即为/usr/local/nginx/conf/nginx.conf,所以-c 配置文件可以不指定。
关闭命令:
/usr/local/nginx/sbin/nginx -s stop
常用Linux开机启动配置的两种方法:
(1)编辑/etc/rc.local,添加开机启动运行命令;
(2)添加/etc/init.d/nginx,通过chkconfig配置开机启动服务;
方法一:编辑/etc/rc.local,添加开机启动运行命令
直接编辑/etc/rc.local文件,文件内容最底下添加启动命令:
/usr/local/nginx/sbin/nginx
即可
方法二:添加/etc/init.d/nginx脚本,通过chkconfig配置开机启动服务
第一步:编辑启动脚本:/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
第二行注释#chkconfig: 2345 85 15说明:
chkconfig 运行级别 启动服务级别 停止服务级别
运行级别:
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
服务启动/停止级别:
0-100值; 值越小,级别越高
如果A服务需要依赖B服务(例:Java服务依赖Redis服务),则启动/停止级别可设置为:
A服务:#chkconfig 2345 80 50
B服务:#chkconfig 2345 50 80
即:
启动顺序:先启动B服务,后启动A服务;
停止顺序:先停止A服务,后停止B服务;
第二步,测试nginx脚本
添加可执行权限:
chmod +x /etc/init.d/nginx
测试脚本启动/关闭:
/etc/init.d/nginx start
/etc/init.d/nginx stop
通过查看nginx进程,确认开启/关闭命令执行后状态
ps -ef|grep nginx
[root@iZ2zegbfy ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ OK ]
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep nginx
root 14151 13174 0 13:31 pts/1 00:00:00 grep --color=auto nginx
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep nginx
root 14174 1 0 13:31 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
apache 14176 14174 0 13:31 ? 00:00:00 nginx: worker process
apache 14177 14174 0 13:31 ? 00:00:00 nginx: worker process
apache 14178 14174 0 13:31 ? 00:00:00 nginx: worker process
root 14182 13174 0 13:31 pts/1 00:00:00 grep --color=auto nginx
[root@iZ2zegbfy ~]#
第三步:设置开机启动
设置开机启动命令:
cd /etc/init.d/
chkconfig nginx on
服务启动/关闭命令:
service nginx start
service nginx stop
[root@iZ2zegbfy ~]# service nginx stop
Stopping nginx (via systemctl): [ OK ]
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep nginx
root 14357 13174 0 13:34 pts/1 00:00:00 grep --color=auto nginx
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# service nginx start
Starting nginx (via systemctl): [ OK ]
[root@iZ2zegbfy ~]#
[root@iZ2zegbfy ~]# ps -ef|grep nginx
root 14174 1 0 13:31 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
apache 14176 14174 0 13:31 ? 00:00:00 nginx: worker process
apache 14177 14174 0 13:31 ? 00:00:00 nginx: worker process
apache 14178 14174 0 13:31 ? 00:00:00 nginx: worker process
root 14182 13174 0 13:31 pts/1 00:00:00 grep --color=auto nginx
[root@iZ2zegbfy ~]#
至此,即配置完成,可通过chkconfig命令查看系统服务运行状态。
chkconfig --list
[root@iZ2zegbfy ~]# 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]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 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
(完)