Nginx 启动脚本/重启脚本

转载自:http://blog.csdn.net/yongzhang52545/article/details/7619187


第一步

先运行命令关闭nginx

[plain] view plaincopy
  1. sudo kill `cat /usr/local/nginx/logs/nginx.pid`  

第二步

[html] view plaincopy
  1. vi /etc/init.d/nginx  

输入以下内容

[plain] view plaincopy
  1. #!/bin/sh  
  2. #  
  3. # nginx - this script starts and stops the nginx daemin  
  4. #  
  5. # chkconfig:   - 85 15  
  6. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse   
  7. #               proxy and IMAP/POP3 proxy server  
  8. # processname: nginx  
  9. # config:      /usr/local/nginx/conf/nginx.conf  
  10. # pidfile:     /usr/local/nginx/logs/nginx.pid  
  11.   
  12. # Source function library.  
  13. . /etc/rc.d/init.d/functions  
  14.   
  15. # Source networking configuration.  
  16. . /etc/sysconfig/network  
  17.   
  18. # Check that networking is up.  
  19. [ "$NETWORKING" = "no" ] && exit 0  
  20.   
  21. nginx="/usr/local/nginx/sbin/nginx"  
  22. prog=$(basename $nginx)  
  23.   
  24. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"  
  25.   
  26. lockfile=/var/lock/subsys/nginx  
  27.   
  28. start() {  
  29.     [ -x $nginx ] || exit 5  
  30.     [ -f $NGINX_CONF_FILE ] || exit 6  
  31.     echo -n $"Starting $prog: "  
  32.     daemon $nginx -c $NGINX_CONF_FILE  
  33.     retval=$?  
  34.     echo  
  35.     [ $retval -eq 0 ] && touch $lockfile  
  36.     return $retval  
  37. }  
  38.   
  39. stop() {  
  40.     echo -n $"Stopping $prog: "  
  41.     killproc $prog -QUIT  
  42.     retval=$?  
  43.     echo  
  44.     [ $retval -eq 0 ] && rm -f $lockfile  
  45.     return $retval  
  46. }  
  47.   
  48. restart() {  
  49.     configtest || return $?  
  50.     stop  
  51.     start  
  52. }  
  53.   
  54. reload() {  
  55.     configtest || return $?  
  56.     echo -n $"Reloading $prog: "  
  57.     killproc $nginx -HUP  
  58.     RETVAL=$?  
  59.     echo  
  60. }  
  61.   
  62. force_reload() {  
  63.     restart  
  64. }  
  65.   
  66. configtest() {  
  67.   $nginx -t -c $NGINX_CONF_FILE  
  68. }  
  69.   
  70. rh_status() {  
  71.     status $prog  
  72. }  
  73.   
  74. rh_status_q() {  
  75.     rh_status >/dev/null 2>&1  
  76. }  
  77.   
  78. case "$1" in  
  79.     start)  
  80.         rh_status_q && exit 0  
  81.         $1  
  82.         ;;  
  83.     stop)  
  84.         rh_status_q || exit 0  
  85.         $1  
  86.         ;;  
  87.     restart|configtest)  
  88.         $1  
  89.         ;;  
  90.     reload)  
  91.         rh_status_q || exit 7  
  92.         $1  
  93.         ;;  
  94.     force-reload)  
  95.         force_reload  
  96.         ;;  
  97.     status)  
  98.         rh_status  
  99.         ;;  
  100.     condrestart|try-restart)  
  101.         rh_status_q || exit 0  
  102.             ;;  
  103.     *)  
  104.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
  105.         exit 2  
  106. esac  

保存退出

第三步

[html] view plaincopy
  1. chmod +x /etc/init.d/nginx  

第四步

[plain] view plaincopy
  1. /sbin/chkconfig nginx on  

检查一下

[plain] view plaincopy
  1. sudo /sbin/chkconfig --list nginx  
  2. nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off  

完成!

之后,就可以使用以下命令了

[plain] view plaincopy
  1. service nginx start  
  2. service nginx stop  
  3. service nginx restart  
  4. service nginx reload  
  5.   
  6. /etc/init.d/nginx start  
  7. /etc/init.d/nginx stop  
  8. /etc/init.d/nginx restart  
  9. /etc/init.d/nginx reload  

原文地址:https://www.cnblogs.com/ycpanda/p/3637306.html