添加Nginx作为系统服务

来源:互联网 发布:mac foxmail 邮件备份 编辑:程序博客网 时间:2024/05/17 02:27

前言

创建一个脚本,该脚本将改变nginx守护进程,让nginx以系统服务的形式启动,守护进程以后将由标准的命令控制且在系统启动时自动启动。

System V脚本

大多数基于linux的操作系统,使用的是System-V风格的init守护进程,启动进程由init进程管理。
守护进程遵循运行级别的原则(run level),系统运行级别表示当前计算机状态。

运行级别 状态 0 系统停止 1 单用户模式(援救模式) 2 多用户模式(不支持NFS) 3 完整的多用户模式 4 没有使用 5 图形界面按模式 6 重启系统

关闭系统:

[root@localhost ~]# telinit 0

重启系统:

[root@localhost ~]# telinit 6

对于每一个运行级别的转换都会有一组服务被执行,系统停止时它的运行级别为0,一旦开启将转换到默认启动级别,系统默认启动级别是在etc/inittab文件下配置的。

为nginx建立init脚本

目录/etc/init.d实际上是/etc/rc.d/init.d的符号链接,因此在/etc/init.d目录下新建文件nginx(需要root权限):

[root@localhost rc.d]# vi /etc/init.d/nginx

脚本内容如下:

#!/bin/bash
# chkconfig: - 85 15
#description: Nginx is a World Wide Web server.
#processname: nginx

nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n “Starting Nginx”
$nginx -c $conf
echo ” done”
;;
stop)
echo -n “Stopping Nginx”
killall -9 nginx
echo ” done”
;;
test)
$nginx -t -c $conf
;;
reload)
echo -n “Reloading Nginx”
ps auxww | grep nginx | grep master | awk ‘{print $2}’ | xargs kill -HUP
echo ” done”
;;
restart)
$0 stop
$0 start
;;
show)
ps -aux|grep nginx
;;
*)
echo -n “Usage: $0 {start|restart|reload|stop|test|show}”
;;
esac

安装nginx的init脚本

授予该脚本可执行权限:

[root@localhost rc.d]# chmod +x /etc/init.d/nginx

然后就可以开启nginx:

[root@localhost init.d]# service nginx start

或:

[root@localhost init.d]# /etc/init.d/nginx start

原地址链接:http://blog.csdn.net/zsl10/article/details/52190206