如何把nginx做成自啟動服務


1.需要借助"Windows Service Wrapper"小工具,項目地址:  https://github.com/kohsuke/winsw (供學習)

2.下載地址:  http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe

3.下載該工具后,將其放在 Nginx安裝目錄下,並重命名為nginx-service.exe,創建配置文件nginx- service.xml(名字要和工具名一樣)(注意:存放nginx的目錄中不能存在中文,否則會導致發布失敗)

4.創建nginx-service.exe.config(為支持NET 4.0 runtime,默認只支持NET 2.0 runtime)

粘貼圖片

 

5. nginx-service.xml 內容如下:(記得修改路徑)

<service>  
<id>nginx</id> 
 <name>Nginx Service</name> 
 <description>High Performance Nginx Service</description> 
 <logpath>D:\Web\nginx\logs</logpath>  
 <log mode="roll-by-size">  
 <sizeThreshold>10240</sizeThreshold>   
 <keepFiles>8</keepFiles>   </log> 
 <executable>D:\Web\nginx\nginx.exe</executable>  
 <startarguments>-pD:\Web\nginx</startarguments> 
 <stopexecutable>D:\Web\nginx\nginx.exe</stopexecutable> 
 <stoparguments>-pD:\Web\nginx -s stop</stoparguments> </service>

 

6. nginx-service.exe.config 內容如下:

<configuration> 
 <startup>   
 <supportedRuntime version="v2.0.50727" />  
 <supportedRuntime version="v4.0" />   </startup>
 <runtime>     <generatePublisherEvidence enabled="false"/>  
 </runtime>
 </configuration>

 

7.重點:以管理員的身份運行cmd,跳轉到nginx所在的目錄下,安裝nginx的服務,執行命令:

nginx-service.exe install

 

卸載:

nginx-service.exe uninstall 或者sc delete 服務名

 

 

粘貼圖片

 

粘貼圖片

 

這時,nginx的windows服務便已經安裝好了,運行-->services.msc 打開服務列表

 

粘貼圖片

 

8.啟動服務,關閉服務的命令

啟動:net start nginx

粘貼圖片

關閉:net stop nginx

粘貼圖片

 

注意: 安裝成服務后,nginx -s reload 不可用,會報錯

 

9.重啟計算機,再打開服務列表查看,這時候服務已經是自啟動的了

 

參考:https://www.dazhuanlan.com/2019/12/16/5df6fbb4975a8/

          https://blog.csdn.net/u013302113/article/details/76080915

          https://www.cnblogs.com/cxscode/p/8262319.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM