环境:
系统:CentOS 6.5 Final
安装目录:/usr/local/nginx
Nginx开机自启:
①编写shell实现控制
vi /etc/init.d/nginx
添加内容:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it‘s not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf 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
更改访问权限:
chmod a+x /etc/init.d/nginx
测试效果:
[root@localhost ~]# /etc/init.d/nginx stop 停止 nginx: [确定] [root@localhost ~]# /etc/init.d/nginx start 正在启动 nginx: [确定] [root@localhost ~]# /etc/init.d/nginx restart 停止 nginx: [确定] 正在启动 nginx: [确定] [root@localhost ~]# /etc/init.d/nginx stop 停止 nginx: [确定] [root@localhost ~]# /etc/init.d/nginx sto Usage: nginx {start|stop|restart|reload|status|help} [root@localhost ~]# /etc/init.d/nginx restart 停止 nginx: [失败] 正在启动 nginx: [确定]
②使用该shell配置开机启动
法一:
vi /etc/rc.local
添加一行
/etc/init.d/nginx start
保存并退出,重启生效。
法二:
chkconfig --add nginx //添加索引 chkconfig --level 23 nginx on //开机启动
重启生效。
③添加自定义命令(使用nginx代替/etc/init.d/nginx,启动nginx再也不用输那么长命令了)
vi /root/.bashrc
追加
alias nginx="/etc/init.d/nginx"
重启生效。
参考:
Linux下的Nginx安装(开机自启动):http://www.cnblogs.com/meteoric_cry/archive/2011/01/27/1945882.html
Linux下chkconfig命令详解:http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html
nginx documentation:http://nginx.org/en/docs/
Nginx使用(配置开机启动),布布扣,bubuko.com
原文:http://www.cnblogs.com/hubery/p/3699350.html