在/etc/init.d/创建文件nginx

[root@iZ2ze0hm1pb36l78jkklpnZ sbin]# vim /etc/init.d/nginx

添加如下代码:

chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
nginx=”/usr/local/nginx/sbin/nginx”
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

保存配置添加权限

[root@iZ2ze0hm1pb36l78jkklpnZ sbin]# chmod a+x /etc/init.d/nginx

至此就可以通过下面指令控制启动停止

/etc/init.d/nginx start
/etc/init.d/nginx stop

上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。

先将nginx服务加入chkconfig管理列表:

[root@iZ2ze0hm1pb36l78jkklpnZ sbin]# chkconfig —add /etc/init.d/nginx

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

service nginx start
service nginx stop
service nginx restart

最后设置开机自动启动

[root@localhost ~]# chkconfig nginx on