Nginx 开机自启动

2016年5月13日

1、 vim /etc/init.d/nginx 内容为:

  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 

2、为该脚本增加启动权限

chmod 755 /etc/init.d/nginx

3、加为系统服务,自动启动

chkconfig --level 2345 nginx on

4、手动启动

/etc/init.d/nginx start

service nginx start

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注