Home Backend Development PHP Tutorial Start Nginx with start-stop-daemon

Start Nginx with start-stop-daemon

Jul 28, 2016 am 08:29 AM
nginx start stop

用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>
Copy after login

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

  • –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>
Copy after login
').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教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles