Add nginx.exe as Windows system service (like Apache)?

前端 未结 9 1045
面向向阳花
面向向阳花 2020-12-04 07:08

I set up NGINX as a front end server for static content and I use Apache as a back-end server for other thing.

The thing is I can\'t find a logical answer that allow

相关标签:
9条回答
  • 2020-12-04 07:26

    NSSM is the best tool to run Nginx as a service.
    If you do not want to use any external 3rd party software then you can implement any of these two methods.

    • Windows Task Scheduler
    • Windows startup shortcut

    Windows Task Scheduler

    • As mentioned in this answer prepare one start.bat file.
    • Put this file where nginx.exe is present.
    • Open windows task scheduler and set up the task as described in this answer to run it indefinitely.
    • Do not forget to run this task as the highest privilege with the system account, more details can be found here.
    • Make the task to start daily at a certain time, through the bat file it will check whether the service is already running to avoid creating multiple nginx.exe instances.
    • If due to some reason Nginx shuts down, within 5 minutes it will start.

    Windows Startup shortcut

    • Create one shortcut of nginx.exe and put it in the startup folder of Windows.

    • Follow this answer to find your startup location.

    • Nginx will run automatically whenever you log in to the system.
    • This one is the easiest. However, it is dependent on user profile i.e. if you are running Nginx on a server, it will run only for your user account, when you log off it stops.
    • This is ideal for dev environment.
    0 讨论(0)
  • 2020-12-04 07:32

    The easiest way I've found, was using the Chocolatey package manager.

    Once Chocolatey is installed, you open an administrative prompt and type:

    choco install nginx
    

    You now have a Windows service named 'nginx' running.

    0 讨论(0)
  • 2020-12-04 07:32

    Rather than turning nginx into a service, or using CMD to start a process, which really doesn't seem to work. I found that Powershell makes it easy to startup nginx as a detached process. I've combined starting nginx with PHP. Below is the script, named "start-nginx.ps1"

    $fcgiPort = "127.0.0.1:9000"
    $PHPini = "c:\php\php.ini"
    
    $ErrorActionPreference = "SilentlyContinue"
    
    function restart { 
    Push-Location /nginx
    Stop-Process -Force -Name nginx 
    Start-Process ./nginx.exe   -WindowStyle Hidden 
    
    Stop-Process -Force -Name php-cgi
    Start-Process  "c:\php\php-cgi.exe" -ArgumentList ("-b" + $fcgiPort  +  " -c "  +  $PHPini)   -WindowStyle Hidden 
    Pop-Location
    }
    
    restart
    

    This script can be executed from any directory, but needs to be customized for where your nginx installation is located.

    This script includes a silent attempt to kill nginx and PHP before launching both.

    Windows systems are supposed to recognize ".ps1" files as powershell, even in the CMD prompt.

    I created another small script to kill the running processes, which simply removes the "start-process" lines from this file.

    To run at startup, I used the win-R command to navigate to the directory shell:startup

    Placing a shortcut to the startup script in this directory, nginx starts at boot!

    Powershell also includes a much more sophisticated ability to schedule tasks, and it is possible to schedule this script to run at startup. See This Link

    From the article:

     >powershell
    
     $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:30
     Register-ScheduledJob -Trigger $trigger -FilePath $HOME/start-nginx.ps1 -Name startNginx
    

    Combined, I think this approach gets you everything you'd need from an nginx windows service and doesn't require any third-party applications.

    0 讨论(0)
提交回复
热议问题