search
HomeBackend DevelopmentPHP TutorialStart Nginx with start-stop-daemon

用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教程有兴趣的朋友有所帮助。

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools