简体   繁体   中英

run nginx as windows service

I am trying to run nginx (reverse proxy) as a windows service so that it's possible to proxy a request even when a user is not connected.

I searched a lot around and found winsw that should create a service from an .exe file (such as nginx).

i found many tutorials online saying to create an xml file as following

<service>
   <id>nginx</id>
   <name>nginx</name>
   <description>nginx</description>
   <executable>c:\nginx\nginx.exe</executable>
   <logpath>c:\nginx\</logpath>
   <logmode>roll</logmode>
   <depend></depend>
   <startargument>-p c:\nginx</startargument>
   <stopargument>-p c:\nginx -s stop</stopargument>
</service>

(i have nginx.exe in a folder called nginx under c: o the paths are correct).

Now the problem is that the service is created but i can't seem to make it start, every time i try to start it a windows pops up saying

Error 1053: The service didn't respond to the start or control request in a timely fashion

Does anyone know how can i fix this or a different way to run nginx as a window service?

Just stumbled here and managed to get things working with this free open source alternative: https://nssm.cc/

It basically is just a GUI to help you create a service. Steps I used:

  1. Download NGinx ( http://nginx.org/en/download.html ) and uzip to C:\\foobar\\nginx
  2. Download nssm ( https://nssm.cc/ )
  3. Run "nssm install nginx" from the command line
  4. In NSSM gui do the following:
  5. On the application tab: set path to C:\\foobar\\nginx\\nginx.exe, set startup directory to C:\\foorbar\\nginx
  6. On the I/O tab type "start nginx" on the Input slow. Optionally set C:\\foobar\\nginx\\logs\\service.out.log and C:\\foobar\\nginx\\logs\\service.err.log in the output and error slots.
  7. Click "install service". Go to services, start "nginx". Hit http://localhost:80 and you should get the nginx logon. Turn off the service, disable browser cache and refresh, screen should now fail to load.

You should be good to go from then on.

NSSM is very nice, but there is another alternative: The PowerShell Cmdlet New-Service

Here is just a simple example:

$params = @{
    Name = "MyService"
    BinaryPathName = "path/to/exe"
    DisplayName = "My Service"
    StartupType = "Automatic"
    Description = "Description of my service"
}
New-Service @params

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-6

I found other solution other than NSSM . That is Windows Service Wrapper and the following are the instructions:

  1. Download the latest version of Windows Service Wrapper via github .

    • Current version as of this writing is v2.1.2(Since v2.x executables for .NET2.0 and .NET4.0 are available - others only on demand.)
  2. Rename winsw-xxxx.exe to something like nginxservice.exe.

    • This is the name that will show up for the process that owns your Nginx process.
  3. Place an XML file next to the exe with the same base name, eg nginxservice.xml. The contents should be like below (verify your nginx location).

<service> <id>nginx</id> <name>nginx</name> <description>nginx</description> <executable>c:\\nginx\\nginx.exe</executable> <logpath>c:\\nginx\\</logpath> <logmode>roll</logmode> <depend></depend> <startargument>-p</startargument> <startargument>c:\\nginx</startargument> <stopexecutable>c:\\nginx\\nginx.exe</stopexecutable> <stopargument>-p</stopargument> <stopargument>c:\\nginx</stopargument> <stopargument>-s</stopargument> <stopargument>stop</stopargument> </service>

  • You can find up to date details about the configuration on the config GitHub page and a generic example showing all possible options here .

    1. Run the command nginxservice.exe install.
      • You will now have a Nginx service in your Services! (It is set to start automatically on boot; if you want to start your server, you must manually start the service (net start Nginx).)

The Above answer was taken from a post .

我找到了NSSM (Non-Sucking Service Manager):一个程序完全符合我的要求,而且设置起来要容易得多。

As told in other answers 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 ie 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.

You'll need this for winsw

   <service>
        <id>nginx</id>
        <name>nginx</name>
        <description>nginx</description>
        <executable>c:\...\nginx.exe</executable>
        <logpath>...</logpath>
        <logmode>roll</logmode>
        <stopexecutable>c:\nginx\nginx-1.14.0\nginx.exe</stopexecutable>
        <stopargument>-s</stopargument>
        <stopargument>stop</stopargument>
    </service>

You will need an <executable> assuming you are using the nginx.conf hence don't need any starting up arguments and also a <stopexecutable> and <stopargument> s (to emlate nginx -s stop )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM