>백엔드 개발 >PHP 튜토리얼 >start-stop-daemon으로 Nginx 시작

start-stop-daemon으로 Nginx 시작

WBOY
WBOY원래의
2016-07-28 08:29:101349검색

用start-stop-daemon启动Nginx

在前面学习Ubuntu apt-get install nginx 创建的nginx启动脚本中,看到start-stop-daemon的用法。

迅速查了一下手册(用man start-stop-daemon)。这个程序用来启动和关闭系统级别的进程。

下面我用该命令启动我自己编译的nginx程序:

<code><span>start</span><span>-</span><span>stop</span><span>-</span><span>daemon</span><span>-</span><span>-</span><span>start</span><span>-</span><span>-</span><span>quiet</span><span>-</span><span>-</span><span>make</span><span>-</span><span>pidfile</span><span>-</span><span>-</span><span>pidfile</span><span>/opt/nginx</span><span>.</span><span>pid</span><span>-</span><span>-</span><span>exec</span><span>/home/chenshu/nginx/bin/nginx</span><span>-</span><span>-</span><span>-</span><span>c</span><span>/home/chenshu/nginx/etc_nginx/nginx</span><span>.</span><span>conf</span></code>

注意,这里都使用绝对路径,相对路径会有问题。

  • –pidfile 指定了保存进程id的文件 /opt/nginx.pid
    –exec 指定了可执行程序
    – 之后加上传递给nginx的参数 -c /home/chenshu/nginx/etc_nginx/nginx.conf
    –make-pidfile 使用 是因为有时候start-stop-daemon 不产生pidfile

单独运行nginx的时候,还可以指定 -t参数用于测试-c 参数指定的配置文件是否正确。

由于我要将所有的nginx程序,日志,配置都放在/home/dist/carrier/nginx目录下,因此对应的修改启动脚本/etc/init.d/nginx

由于没有找到办法指定error log的位置. 因此现在绕过的方法是 编译nginx的时候使用–prefix=/home/dist/carrier/nginx参数。

这里用到了之前没有用的DAEMON_OPTS变量,传递-c参数。注意,必须用双引号。

<code><span>#!/bin/sh  
</span><span>### BEGIN INIT INFO  </span><span># Provides:          nginx  </span><span># Required-Start:    $local_fs $remote_fs $network $syslog  </span><span># Required-Stop:     $local_fs $remote_fs $network $syslog  </span><span># Default-Start:     2 3 4 5  </span><span># Default-Stop:      0 1 6  </span><span># Short-Description: starts the nginx web server  </span><span># Description:       starts nginx using start-stop-daemon  </span><span>### END INIT INFO  </span>PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
CARRIER_HOME=/home/dist/carrier  
DAEMON=<span>$CARRIER_HOME</span>/nginx/bin/nginx  
PID_FILE=<span>$CARRIER_HOME</span>/nginx/nginx.pid  
C>$CARRIER_HOME/nginx/conf/nginx.conf  
DAEM>"-c <span>$CONFIG_FILE</span>"NAME=nginx  
DESC=nginx  

<span># Include nginx defaults if available  </span><span>if</span> [ <span>-f</span> /etc/default/nginx ]; <span>then</span>    . /etc/default/nginx  
<span>fi</span>test -x <span>$DAEMON</span> || <span>exit</span><span>0</span><span>set</span><span>-e</span>. /lib/lsb/init-functions  

<span><span>test_nginx_config</span></span>() {  
    <span>if</span><span>$DAEMON</span> -t <span>$DAEMON_OPTS</span> >/dev/null <span>2</span>>&<span>1</span>; <span>then</span><span>return</span><span>0</span><span>else</span><span>$DAEMON</span> -t <span>$DAEMON_OPTS</span><span>return</span> $?  
    <span>fi</span>}  

<span>case</span><span>"<span>$1</span>"</span><span>in</span>    start)  
        <span>echo</span> -n <span>"Starting <span>$DESC</span>: "</span>        test_nginx_config  
        <span># Check if the ULIMIT is set in /etc/default/nginx  </span><span>if</span> [ -n <span>"<span>$ULIMIT</span>"</span> ]; <span>then</span><span># Set the ulimits  </span>
            ulimit <span>$ULIMIT</span><span>fi</span>        start-stop-daemon --start --quiet --make-pidfile --pidfile <span>$PID_FILE</span> --exec <span>$DAEMON</span> -- <span>$DAEMON_OPTS</span> || <span>true</span><span>echo</span><span>"<span>$NAME</span>."</span>        ;;  

    stop)  
        <span>echo</span> -n <span>"Stopping <span>$DESC</span>: "</span>        start-stop-daemon --stop --quiet --pidfile <span>$PID_FILE</span> --exec <span>$DAEMON</span> || <span>true</span><span>echo</span><span>"<span>$NAME</span>."</span>        ;;  

    restart|force-reload)  
        <span>echo</span> -n <span>"Restarting <span>$DESC</span>: "</span>        start-stop-daemon --stop --quiet --pidfile <span>$PID_FILE</span> --exec <span>$DAEMON</span> || <span>true</span>        sleep <span>1</span>        test_nginx_config  
        <span># Check if the ULIMIT is set in /etc/default/nginx  </span><span>if</span> [ -n <span>"<span>$ULIMIT</span>"</span> ]; <span>then</span><span># Set the ulimits  </span>
            ulimit <span>$ULIMIT</span><span>fi</span>        start-stop-daemon --start --quiet --make-pidfile --pidfile <span>$PID_FILE</span> --exec <span>$DAEMON</span> -- <span>$DAEMON_OPTS</span> || <span>true</span><span>echo</span><span>"<span>$NAME</span>."</span>        ;;  

    reload)  
        <span>echo</span> -n <span>"Reloading <span>$DESC</span> configuration: "</span>        test_nginx_config  
        start-stop-daemon --stop --signal HUP --quiet --pidfile <span>$PID_FILE</span> --exec <span>$DAEMON</span> || <span>true</span><span>echo</span><span>"<span>$NAME</span>."</span>        ;;  

    configtest|testconfig)  
        <span>echo</span> -n <span>"Testing <span>$DESC</span> configuration: "</span><span>if</span> test_nginx_config; <span>then</span><span>echo</span><span>"<span>$NAME</span>."</span><span>else</span><span>exit</span> $?  
        <span>fi</span>        ;;  

    status)  
        status_of_proc -p <span>$PID_FILE</span><span>"<span>$DAEMON</span>"</span> nginx && <span>exit</span><span>0</span> || <span>exit</span> $?  
        ;;  
    *)  
        <span>echo</span><span>"Usage: <span>$NAME</span> {start|stop|restart|reload|force-reload|status|configtest}"</span> >&<span>2</span><span>exit</span><span>1</span>        ;;  
<span>esac</span><span>exit</span><span>0</span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 用start-stop-daemon启动Nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.