John Doe
John Doe

Reputation: 1869

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 window 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?

Upvotes: 51

Views: 120693

Answers (5)

Dave
Dave

Reputation: 1216

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:\foobar\nginx
  6. On the I/O tab type start nginx on the Input (stdin) field. 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 button. Go to Windows Services, start the "nginx" service. 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.

Upvotes: 105

Marco
Marco

Reputation: 1

I found your issue. You have stopargument instead of stoparguments. use the following:

    <executable>"%BASE%\nginx.exe"</executable>
    <!--arguments></arguments-->
    <startarguments></startarguments>

    <stopexecutable>"%BASE%\nginx.exe"</stopexecutable>
    <stoparguments>-s stop</stoparguments>

This is running on my server

Upvotes: 0

Immanuel Lechner
Immanuel Lechner

Reputation: 587

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-6

Upvotes: 39

tmjee
tmjee

Reputation: 209

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)

Upvotes: 2

Soumendra
Soumendra

Reputation: 1624

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 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.

Upvotes: 6

Related Questions