Add nginx.exe as Windows system service (like Apache)?
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)
- 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.
- Rename
winsw-*.exe
to something likenginxservice.exe
.- This is the name that will show up for the process that owns your nginx process.
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, a generic example showing all possible options here and an installation guide.
- 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 }}
Download NSSM formhttp://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.
You can using start.bat and stop.bat to realize the same effect.
start.bat
@ECHO OFFREM Start Nginxtasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NULIF 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 OFFREM Stop Nginxtasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NULIF "%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.)