本文摘自:php
http://www.01happy.com/centos-nginx-shell-chkconfig/linux
在安裝完nginx後,從新啓動須要「kill -HUP nginx進程編號」來進行從新加載,顯然十分不方便。若是能像apache同樣,直接經過腳本進行管理就方便多了。nginx
nginx官方早就想好了,也提供了這個腳本,地址:http://wiki.nginx.org/RedHatNginxInitScript。這裏將管理腳本收錄在這裏:shell
啓動腳本有坑,nginx reload 和stop的時候是直接用killproc 幹掉進程的,應該用nginx -s reload 和nginx -s stop 完整腳本以下apache
#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /www/wdlinux/nginx/conf/nginx.conf # pidfile: /www/wdlinux/nginx/logs/nginx.pid # Url http://www.wdlinux.cn # Last Updated 2010.06.01 # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/tengine/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/tengine/conf/nginx.conf" #NGINX_PID="/www/wdlinux/nginx/logs/nginx.pid" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo ##service php-fpm start [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " $nginx -s stop echo_success retval=$? echo ##service php-fpm stop [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " $nginx -s reload RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
將以上腳本保存到/etc/init.d/nginx文件,並修改兩個地方:centos
保存後,就能夠經過該腳本對nginx服務進行管理了:app
$ /etc/init.d/nginx start $ /etc/init.d/nginx stop $ /etc/init.d/nginx reload
使用chkconfig進行管理php-fpm
上面的方法完成了用腳本管理nginx服務的功能,可是仍是不太方便,好比要設置nginx開機啓動等。這時能夠使用chkconfig來設置。this
先將nginx服務加入chkconfig管理列表:rest
chkconfig --add /etc/init.d/nginx
加完這個以後,就能夠使用service對nginx進行啓動,重啓等操做了。
$ service nginx start $ service nginx stop $ service nginx reload
設置終端模式開機啓動:
$ chkconfig --level 3 nginx on