Nginx快捷启动配置

Linux下Nginx开关服务,正常方式是这样({nginx}-为Nginx安装路径):

[java] view plain copy
 
  1. {nginx}/sbin/nginx #启动  
  2. {nginx}/sbin/nginx -s stop #停止  


这种方式带来很多不便,因此介绍一种快捷启动关闭Nginx的方式。

在/etc/init.d下创建nginx启动脚本文件:

[java] view plain copy
 
  1. vim /etc/init.d/nginx  

i进入编辑状态,粘贴以下代码后保存:
将/usr/local/nginx/替换为自身nginx的安装路径。

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

修改脚本权限:

[java] view plain copy
 
  1. chmod 755 nginx  

将脚本文件加入到chkconfig中:

[java] view plain copy
 
  1. chkconfig --add nginx  

设置nginx开机在3和5级别自动启动:

[java] view plain copy
 
  1. chkconfig --level 35 nginx on  

创建软连接:

[java] view plain copy
 
  1. cd /usr/bin       
  2. ln -s /etc/init.d/nginx  

愉快的玩耍吧!
这里边的命令都可以执行:
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
以下是示例:

[java] view plain copy
 
    1. nginx start  
    2. nginx stop  
    3. nginx restart  
原文地址:https://www.cnblogs.com/chenhaoyu/p/8899816.html