65

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 allows me to make nginx.exe a Windows system service (like my Apache).

Any come across an answer to this?

wonea
  • 4,783
  • 17
  • 86
  • 139
user1305810
  • 705
  • 1
  • 8
  • 10

9 Answers9

85

How to do it with Windows Service Wrapper

(Note: There are easier alternatives by now - see also solutions described here below using chocolatey package manager by suneg and using NSSM directly from Adamy)

  1. Download the latest version of Windows Service Wrapper via github or nuget.
    • Current version as of this writing is v2.2.0
    • Since v2.x executables for .NET2.0 and .NET4.0 are available - others only on demand.
  2. Rename winsw-*.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>
    
  4. Run the command nginxservice.exe install as administrator.

You will now have an 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).)


Detailed description of correctly setting up nginx as a Windows Service: http://web.archive.org/web/20150819035021/http://misterdai.yougeezer.co.uk/posts/2009/10/16/nginx-windows-service/

Additional info not contained in above blog post:

You can find the latest version of the Windows Service Wrapper also via this Maven Repository: http://repo.jenkins-ci.org

Examples for Maven + Gradle:

<dependency>
    <groupId>com.sun.winsw</groupId>
    <artifactId>winsw</artifactId>
    <version>2.2.0</version>
    <classifier>bin</classifier>
    <packaging>exe</packaging>
</dependency>

<repository>
    <id>jenkinsci</id>
    <name>jenkinsci-releases</name>
    <url>http://repo.jenkins-ci.org/releases</url>
</repository>

compile "com.sun.winsw:winsw:2.2.0"

repositories {
    mavenCentral()
    maven { url http://repo.jenkins-ci.org/releases }
}
Jörg
  • 2,434
  • 23
  • 37
  • 1
    Note, there appears to be a new [repo for WinSW](https://github.com/kohsuke/winsw) with [newer binaries](http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/) available. – Phrogz Apr 24 '15 at 17:50
  • It works fine, but I have issues with stoping service. It hangs. When I invoke stop command manually (from command line) it asks me for PEM pass phrase (I have SSL configured). How to get rid of that interactive question? – Bartosz Bilicki May 07 '15 at 10:58
  • @BartoszBilicki I'd say, this is a topic in its own right - so, please, post your question to the maintainer https://github.com/kohsuke or a bug @ https://github.com/kohsuke/winsw/issues – Jörg May 11 '15 at 07:57
  • 1
    Note that tou have to put 2 stop arguments (tested in 1.18):-s stop – c-toesca Sep 22 '15 at 01:18
  • @c-toesca Thanks for noticing. – Jörg Sep 22 '15 at 07:39
  • 1
    In nginxservice.xml I needed to remove the space character from after -p for it to load correctly. -pc:\nginx. Tried on 1.8.0 and 1.9.5 – Gerard Sexton Sep 25 '15 at 07:14
  • 6
    I updated the xml, because nginx processes were not stopped and I had to kill them manually with `taskkill /F /IM nginx.exe`. Windows Service Wrapper logs said I was missing the stopexecutable xml configuration. So I removed stop argument tags and replaced it by this:`c:\nginx\nginx.exe -s stop` – ssanchezz23 Jan 18 '16 at 13:44
  • Thanks to @kevin-smyth for the review. – Jörg Mar 30 '16 at 13:07
  • @Michael although, I'm not familiar with php cgi, I'd assume yes - your feedback is highly appreciated in case you try it out – Jörg May 23 '17 at 14:40
57

Download NSSM form http://nssm.cc/download . "Run %NSSM_HOME%\nssm.exe install “Nginx”"

Select the Nginx executable in the NSSM dialog, then OK. Go to Services and start the new created service "Nginx", done.

Adamy
  • 2,789
  • 3
  • 27
  • 25
  • 3
    Spent a few hours on trying to do the same with winsw (because you know, the answer has more upvotes, so gotta try that first) and after failing did it with NSSM in a few minutes. Awesomely thanks!! – Marius B Nov 10 '17 at 12:37
  • Thanks, next time I'll try it with your solution instead! The tool looks very straightforward and has a great name ;-) – Jörg Dec 20 '17 at 08:24
  • Great solution! Workink perfect! Thanks! – Anton Mitsev Jun 30 '18 at 09:20
  • 2
    The downside of this solution is that nginx command line commands, such as `nginx.exe -s reload` won't work anymore: if you change the configuration you must restart the service. As workaround I read that you must run nginx as a user in the Administrators group, so then your user, also in the Administrators group will be able to run the commands. – firepol Jul 26 '18 at 07:51
  • @firepol you could use sysinternal's `psexec` to to run process as `SERVICE`. The only problem is that it won't run when you are not in psexec directory. Which cause error in nginx since it can't find configs. The solution is to create bat file to do `cd c:\progra~1\nginx & nginx.exe -s reload` and run this bat through `psexec`. – andrej Jun 05 '20 at 09:00
  • Thanks. This other answer about NSSM was more helpful for me: https://stackoverflow.com/questions/40846356/run-nginx-as-windows-service#comment120092850_41467168 – Ryan Jun 12 '21 at 00:21
8

You can using start.bat and stop.bat to realize the same effect.

start.bat

@ECHO OFF
REM Start Nginx
tasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NUL
IF NOT "%ERRORLEVEL%"=="0" (
   REM Nginx is NOT running, so start it
   c:
   cd \nginx
   start nginx.exe
   ECHO Nginx started.
) else (
   ECHO Nginx is already running.
)

stop.bat

@ECHO OFF
REM Stop Nginx
tasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NUL
IF "%ERRORLEVEL%"=="0" (
   REM Nginx is currently running, so quit it
   c:
   cd \nginx
   nginx.exe -s quit
   ECHO Nginx quit issued.
) else (
   ECHO Nginx is not currently running.
)
Ahsaan Yousuf
  • 685
  • 7
  • 16
xgqfrms
  • 10,077
  • 1
  • 69
  • 68
  • 1
    None of the above methods worked. The service was started "successfully", but nginx.exe was still not running. This approach is straightforward, needs no extra software, can be invoked automatically at startup, and always works. Thanks. – Laszlo Jan 27 '19 at 15:21
  • `/N` is not needed in `find /I /N "nginx.exe">NUL` since the output will redirect to NUL – ws_ Feb 22 '23 at 11:19
7

SC.EXE will only work for executables that already support the Windows Services API and can respond properly to start and stop requests from the Services Control Manager (SCM). Other regular applications, not specifically written as a service, will simply fail to start (usually with error 1053)...

For those exe's, you need a "service wrapper" -- a small utility that can accept the start/stop commands from the SCM and run/terminate your application accordingly. Microsoft provides Srvany (which is free yet very basic), but there are several other free and commercial alternatives.

BTW, you should check out this guide showing how to run Nginix as a service, especially step 7 which discusses how to stop Nginix properly. Not every wrapper will support that functionality (Srvany doesn't)...

CoreTech
  • 2,345
  • 2
  • 17
  • 24
  • `SC.EXE will only work for executables that already support the Windows Services API` I don't believe this statement is correct, unless it might pertain to specific version(s) of Windows. I've used sc.exe to run non service binaries. [See here for an example](http://forum.sysinternals.com/tip-run-process-in-system-account-scexe_topic16714.html) – transistor1 Feb 21 '16 at 17:42
5

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.

suneg
  • 301
  • 3
  • 5
  • this will install nginx v1.6.2 as of this writing – Jörg Feb 05 '16 at 15:30
  • I know this is old, but use `choco install nginx` instead – Manatax Feb 09 '17 at 06:20
  • As we now have June 2017 and the chocolatey version of nginx is still 1.6.2 which was by the way released at 16 Sep 2014 I would not use it in production (even Debian is way more up to date! ). Anyway I need a fast way to test my packer provisioning with a webserver on my dev machine, so I'll go for it! – domih Jun 30 '17 at 10:53
  • This is July-2017 and chocolatey will install nginx 1.12. – Shubham Patel Jul 17 '17 at 13:48
  • 1
    In Jan. 2019, this apparently does not create a service anymore, unfortunately. – Nikita Fuchs Jan 02 '19 at 11:10
  • 1
    In July 2019, this installs nginx as a service using v1.17.1 and depends on choco package nssm (automatically installed for running as a service). – Jörg Jul 01 '19 at 15:31
  • In May 2022 this installs nginx v1.21.6 as a service using nssm as an automatic dependency. – Justin Gilman May 18 '22 at 15:55
2

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.
Soumendra
  • 1,518
  • 3
  • 27
  • 54
1

Official nginx wiki referes on winginx for this purpose. It builds exe-installer in linux environment. Process looks like this:

sudo apt-get install nsis make
wget https://github.com/InvGate/winginx/archive/master.zip
unzip master.zip
cd winginx-master/
make
ls -lh ./build/nginx-service.exe

To get actual versions you should specify them in Makefile.

user3132194
  • 2,381
  • 23
  • 17
1

Download zip file from here.

Extract nginx-service.exe from winginx\build and run it.

Dileepa
  • 1,019
  • 1
  • 15
  • 40
1

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.

sdw
  • 623
  • 9
  • 10