Add nginx.exe as Windows system service (like Apache)?

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?


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

  • Download the latest version of Windows Service Wrapper via github or nuget.
  • Current version as of this writing is v2.1.2
  • Since v2.x executables for .NET2.0 and .NET4.0 are available - others only on demand.
  • Rename winsw-1.xx-bin.exe to something like nginxservice.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, eg nginxservice.xml . The contents should be like below (verify your nginx location).

    <service>
      <id>nginx</id>
      <name>nginx</name>
      <description>nginx</description>
      <executable>c:nginxnginx.exe</executable>
      <logpath>c:nginx</logpath>
      <logmode>roll</logmode>
      <depend></depend>
      <startargument>-p</startargument>
      <startargument>c:nginx</startargument>
      <stopexecutable>c:nginxnginx.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 .
  • 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:

    <dependency>
        <groupId>com.sun.winsw</groupId>
        <artifactId>winsw</artifactId>
        <version>2.1.2</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>
    

    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.


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

    链接地址: http://www.djcxy.com/p/18464.html

    上一篇: 如何在Windows中将Python脚本作为服务运行?

    下一篇: 添加nginx.exe作为Windows系统服务(如Apache)?