Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 343
John Doe Avatar asked Nov 28 '16 14:11

John Doe


People also ask

How do I run nginx as a Windows service?

Windows Startup shortcutCreate one shortcut of nginx.exe and put it in the startup folder of Windows. Follow this answer to find your startup location. You can type `shell: startup` in your run window to navigate to the startup directory. Nginx will run automatically whenever you log in to the system.

Can nginx run on Windows Server?

Nginx is a web server that is very popular with Linux and BSD systems. It can also be installed on Windows 10.

Can we use nginx as web server?

Apache and Nginx are two popular open source web servers often used with PHP. It can be useful to run both of them on the same virtual machine when hosting multiple websites which have varied requirements.


2 Answers

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.

like image 107
Dave Avatar answered Sep 20 '22 17:09

Dave


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

like image 22
Immanuel Lechner Avatar answered Sep 21 '22 17:09

Immanuel Lechner