将 nginx.exe 添加为 Windows 系统服务(如 Apache)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10061191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add nginx.exe as Windows system service (like Apache)?
提问by user1305810
I set up NGINX as a front end server for static content and I use Apache as a back-end server for other thing.
我将 NGINX 设置为静态内容的前端服务器,并将 Apache 用作其他内容的后端服务器。
The thing is I can't find a logical answer that allows me to make nginx.exe
a Windows system service (like my Apache).
问题是我找不到允许我制作nginx.exe
Windows 系统服务(如我的 Apache)的合乎逻辑的答案。
Any come across an answer to this?
有没有遇到过这个问题的答案?
回答by J?rg
How to do it with Windows Service Wrapper
如何使用Windows Service Wrapper
(Note: There are easier alternatives by now - see also solutions described here below using chocolatey package managerby sunegand using NSSM directlyfrom Adamy)
(注:有现在是更容易的选择-见还描述了以下解决方案使用的巧克力包管理器通过suneg和直接使用NSSM从Adamy)
- Download the latest version of Windows Service Wrappervia githubor 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 hereand an installation guide.
- Run the command
nginxservice.exe install
as administrator.
- 通过github或nuget下载最新版本的Windows Service Wrapper。
- 撰写本文时的当前版本是v2.2.0
- 由于 .NET2.0 和 .NET4.0 的 v2.x 可执行文件可用 - 其他仅按需提供。
- 重命名
winsw-*.exe
为类似nginxservice.exe
.- 这是将为拥有 nginx 进程的进程显示的名称。
在 exe 旁边放置一个具有相同基本名称的 XML 文件,例如
nginxservice.xml
. 内容应如下所示(验证您的 nginx 位置)。<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>
- 你可以找到多达约上配置的最新资讯,配置GitHub的页面,显示出所有可能的选择一个通用的例子在这里和安装指南。
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
).)
您现在将nginx
在您的服务中拥有一项服务!(它设置为在启动时自动启动;如果要启动服务器,则必须手动启动该服务 ( 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/
正确设置nginx作为Windows服务的详细说明:http://web.archive.org/web/20150819035021/http: //misterdai.yougeezer.co.uk/posts/2009/10/16/nginx-windows-服务/
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
您还可以通过此 Maven 存储库找到最新版本的 Windows 服务包装器:http: //repo.jenkins-ci.org
Examples for Maven + Gradle:
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 }
}
回答by Adamy
Download NSSM form http://nssm.cc/download. "Run %NSSM_HOME%\nssm.exe install “Nginx”"
从http://nssm.cc/download下载 NSSM 。"运行 %NSSM_HOME%\nssm.exe 安装“Nginx”"
Select the Nginx executable in the NSSM dialog, then OK. Go to Services and start the new created service "Nginx", done.
在 NSSM 对话框中选择 Nginx 可执行文件,然后确定。转到服务并启动新创建的服务“Nginx”,完成。
回答by CoreTech
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)...
SC.EXE 仅适用于已经支持 Windows 服务 API 并且可以正确响应来自服务控制管理器 (SCM) 的启动和停止请求的可执行文件。其他常规应用程序,不是专门作为服务编写的,将无法启动(通常出现错误 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.
对于那些 exe,您需要一个“服务包装器”——一个可以接受来自 SCM 的启动/停止命令并相应地运行/终止您的应用程序的小实用程序。Microsoft 提供Srvany(免费但非常基础),但还有其他几个免费和商业替代品。
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)...
顺便说一句,您应该查看本指南,该指南展示了如何将 Nginix 作为服务运行,尤其是第 7 步,其中讨论了如何正确停止 Nginix。并非每个包装器都支持该功能(Srvany 不支持)...
回答by xgqfrms
You can using start.batand stop.batto realize the same effect.
您可以使用start.bat和stop.bat来实现相同的效果。
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
停止.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.
)
回答by suneg
The easiest way I've found, was using the Chocolateypackage manager.
我发现的最简单的方法是使用Chocolatey包管理器。
Once Chocolatey is installed, you open an administrative prompt and type:
安装 Chocolatey 后,您打开管理提示并键入:
choco install nginx
You now have a Windows service named 'nginx' running.
您现在有一个名为“nginx”的 Windows 服务正在运行。
回答by Soumendra
NSSM is the best tool to run Nginx as a service.
If you do not want to use any external 3rd party softwarethen you can implement any of these two methods.
NSSM 是将 Nginx 作为服务运行的最佳工具。
如果您不想使用任何外部 3rd 方软件,那么您可以实现这两种方法中的任何一种。
- Windows Task Scheduler
- Windows startup shortcut
- Windows 任务计划程序
- Windows 启动快捷方式
Windows Task Scheduler
Windows 任务计划程序
- As mentioned in this answerprepare 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 answerto 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.
- 如本答案所述,准备一个 start.bat 文件。
- 将此文件放在 nginx.exe 所在的位置。
- 打开 Windows 任务计划程序并按照此答案中的说明设置任务以无限期地运行它。
- 不要忘记使用系统帐户以最高权限运行此任务,可以在此处找到更多详细信息。
- 使任务每天在某个时间启动,通过bat文件检查服务是否已经在运行,避免创建多个nginx.exe实例。
- 如果由于某种原因 Nginx 关闭,它会在 5 分钟内启动。
Windows Startup shortcut
Windows 启动快捷方式
Create one shortcut of nginx.exe and put it in the startup folder of Windows.
Follow this answerto 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.
创建一个nginx.exe的快捷方式,放到Windows的启动文件夹中。
按照此答案查找您的启动位置。
- 每当您登录系统时,Nginx 都会自动运行。
- 这个是最简单的。但是,它取决于用户配置文件,即如果您在服务器上运行 Nginx,它只会为您的用户帐户运行,当您注销时它会停止。
- 这是开发环境的理想选择。
回答by user3132194
Official nginx wikireferes on winginxfor this purpose. It builds exe-installer in linux environment. Process looks like this:
官方nginx的维基上referes winginx用于这一目的。它在 linux 环境中构建 exe 安装程序。过程如下所示:
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
.
要获得实际版本,您应该在Makefile
.
回答by Dileepa
回答by sdw
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"
而不是把 nginx 变成服务,或者用 CMD 启动一个进程,这似乎真的行不通。我发现 Powershell 可以轻松地将 nginx 作为分离进程启动。我已经将 nginx 与 PHP 结合起来。下面是脚本,名为“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.
该脚本可以从任何目录执行,但需要根据您的 nginx 安装位置进行自定义。
This script includes a silent attempt to kill nginx and PHP before launching both.
该脚本包括在启动 nginx 和 PHP 之前无声地尝试杀死它们。
Windows systems are supposed to recognize ".ps1" files as powershell, even in the CMD prompt.
Windows 系统应该将“.ps1”文件识别为 powershell,即使在 CMD 提示符下也是如此。
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
为了在启动时运行,我使用 win-R 命令导航到目录 shell:startup
Placing a shortcut to the startup script in this directory, nginx starts at boot!
将启动脚本的快捷方式放置在此目录中,nginx 在启动时启动!
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
Powershell 还包括更复杂的任务调度能力,并且可以调度此脚本在启动时运行。看到这个链接
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.
结合起来,我认为这种方法可以从 nginx Windows 服务中获得您需要的一切,并且不需要任何第三方应用程序。