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?
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
Create one shortcut of nginx.exe and put it in the startup folder of Windows.
Follow this answer to find your startup location.
How to do it with Windows Service Wrapper
(Note: There are promising alternatives by now - see also NSSM solution described in answer below from Adamy.)
winsw-1.xx-bin.exe
to something likenginxservice.exe
.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).nginxservice.exe install
.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
Example:
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"
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:
Combined, I think this approach gets you everything you'd need from an nginx windows service and doesn't require any third-party applications.