Nginx 注册为windows服务 实现开机自启动

需要借助"Windows Service Wrapper"小工具

1. winsw下载

去github上面下载winsw https://github.com/winsw/winsw/releases 需要下载 WinSW.NET4.exe 和 sample-minimal.xml

up-c5578f5c54b962927b69e1fc984680e0cac.png

2.  修改sample-minimal.xml文件内容

<service>
    <id>nginx_service</id>
	<name>nginx_service</name>
	<description>This service runs nginx_service project.</description>
	<!-- 日志配置 -->
	<logpath>E:\nginx-1.14.0\logs</logpath>
	<log mode="roll-by-size">     
      <sizeThreshold>10240</sizeThreshold>     
      <keepFiles>8</keepFiles>   
    </log> 
	<!-- 需要执行的命令 -->
	<executable>E:\nginx-1.14.0\nginx.exe</executable>
	<startarguments>-p E:\nginx-1.14.0</startarguments>   
    <stopexecutable>E:\nginx-1.14.0\nginx.exe</stopexecutable>   
    <stoparguments>-p E:\nginx-1.14.0 -s stop</stoparguments> 
	<!-- 开机启动 -->
    <startmode>Automatic</startmode>
</service>

3.   修改文件名

把 WinSW.NET4.exe 和 sample-minimal.xml 分别重命名为 nginx_service.exe、nginx_service.xml [和xml文件中id同名]。

4. 编写脚本

编写启动脚本 run.bat

@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd "%~dp0"
cd ../win
nginx_service.exe install
net start nginx_service
exit

编写停止脚本 stop.bat

@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd "%~dp0"
cd ../win

net stop nginx_service
nginx_service.exe uninstall
exit

可以拷贝上面四个文件到E盘nginx安装目录下

up-bb90569cb62cb8a42bade7f0d1dccab7937.png

5、运行脚本

  • 双击 run.bat  注册Nginx windows服务并启动Nginx
  • 双击stop.bat 停止Nginx服务并卸载已注册服务

Guess you like

Origin blog.csdn.net/qq_25231683/article/details/120019138