继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Linux(CentOS)下设置nginx开机自动启动

慕森卡
关注TA
已关注
手记 256
粉丝 120
获赞 526

首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:

  1. vim /etc/init.d/nginx  

在脚本中添加如下命令:

  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.   

  13. # Source function library.  

  14. . /etc/rc.d/init.d/functions  

  15.   

  16. # Source networking configuration.  

  17. . /etc/sysconfig/network  

  18.   

  19. # Check that networking is up.  

  20. [ "$NETWORKING" = "no" ] && exit 0  

  21.   

  22. nginx="/data/server/nginx"  

  23. prog=$(basename $nginx)  

  24.   

  25. NGINX_CONF_FILE="/data/server/nginx/conf/nginx.conf"  

  26.   

  27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  

  28.   

  29. lockfile=/var/lock/subsys/nginx  

  30.   

  31. make_dirs() {  

  32.    # make required directories  

  33.    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=[]∗[].*/\1/g' -`  

  34.    if [ -n "$user" ]; then  

  35.       if [ -z "`grep $user /etc/passwd`" ]; then  

  36.          useradd -M -s /bin/nologin $user  

  37.       fi  

  38.       options=`$nginx -V 2>&1 | grep 'configure arguments:'`  

  39.       for opt in $options; do  

  40.           if [ `echo $opt | grep '.*-temp-path'` ]; then  

  41.               value=`echo $opt | cut -d "=" -f 2`  

  42.               if [ ! -d "$value" ]; then  

  43.                   # echo "creating" $value  

  44.                   mkdir -p $value && chown -R $user $value  

  45.               fi  

  46.           fi  

  47.        done  

  48.     fi  

  49. }  

  50.   

  51. start() {  

  52.     [ -x $nginx ] || exit 5  

  53.     [ -f $NGINX_CONF_FILE ] || exit 6  

  54.     make_dirs  

  55.     echo -n $"Starting $prog: "  

  56.     daemon $nginx -c $NGINX_CONF_FILE  

  57.     retval=$?  

  58.     echo  

  59.     [ $retval -eq 0 ] && touch $lockfile  

  60.     return $retval  

  61. }  

  62.   

  63. stop() {  

  64.     echo -n $"Stopping $prog: "  

  65.     killproc $prog -QUIT  

  66.     retval=$?  

  67.     echo  

  68.     [ $retval -eq 0 ] && rm -f $lockfile  

  69.     return $retval  

  70. }  

  71.   

  72. restart() {  

  73.     configtest || return $?  

  74.     stop  

  75.     sleep 1  

  76.     start  

  77. }  

  78.   

  79. reload() {  

  80.     configtest || return $?  

  81.     echo -n $"Reloading $prog: "  

  82.     killproc $nginx -HUP  

  83.     RETVAL=$?  

  84.     echo  

  85. }  

  86.   

  87. force_reload() {  

  88.     restart  

  89. }  

  90.   

  91. configtest() {  

  92.   $nginx -t -c $NGINX_CONF_FILE  

  93. }  

  94.   

  95. rh_status() {  

  96.     status $prog  

  97. }  

  98.   

  99. rh_status_q() {  

  100.     rh_status >/dev/null 2>&1  

  101. }  

  102.   

  103. case "$1" in  

  104.     start)  

  105.         rh_status_q && exit 0  

  106.         $1  

  107.         ;;  

  108.     stop)  

  109.         rh_status_q || exit 0  

  110.         $1  

  111.         ;;  

  112.     restart|configtest)  

  113.         $1  

  114.         ;;  

  115.     reload)  

  116.         rh_status_q || exit 7  

  117.         $1  

  118.         ;;  

  119.     force-reload)  

  120.         force_reload  

  121.         ;;  

  122.     status)  

  123.         rh_status  

  124.         ;;  

  125.     condrestart|try-restart)  

  126.         rh_status_q || exit 0  

  127.             ;;  

  128.     *)  

  129.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  

  130.         exit 2  

  131. esac  

这个脚本来自nginx官方,脚本地址:http://wiki.nginx.org/RedHatNginxInitScript ,不过要注意,如果你是自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:
nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

保存脚本文件后设置文件的执行权限:

  1. chmod a+x /etc/init.d/nginx  

然后,就可以通过该脚本对nginx服务进行管理了:

  1. /etc/init.d/nginx start  

  2. /etc/init.d/nginx stop  

使用chkconfig进行管理
上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。
先将nginx服务加入chkconfig管理列表:

  1. chkconfig --add /etc/init.d/nginx  

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

  1. service nginx start  

  2. service nginx stop  

  3. service nginx status  

设置终端模式开机启动:

  1. chkconfig nginx on  

原文出处

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP