run nginx as windows service run nginx as windows service nginx nginx

run nginx as windows 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, e.g. 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.