1 Download Nginx
Download address: http://nginx.org/en/download.html
2 Start Nginx
There are two ways to start nginx: one is to directly click nginx.exe to start, and the other is to start through the command line
2.1 Direct boot
Find the nginx directory, double-click nginx.exe to start
2.2 Command line startup
Enter cmd in the nginx directory address bar, enter the cmd window and enter the following command line
nginx.exe
or
start nginx
2.3 Check whether the startup is successful
Enter the command line in the cmd window tasklist /fi “imagename eq nginx.exe”
, and the following results show that the startup is successful
2.4 Close nginx
Use the following two command lines to end the nginx process.
nginx -s stop (close nginx quickly)
nginx -s quit (close nginx completely, keep operation log)
3 Use winsw to set Nginx to start automatically at boot
The winsw tool has already been mentioned in the previous article on setting the java project to start automatically, and I will repeat it here.
3.1 Download tool
Its use principle is that winsw.exe itself is registered as a windows service, which can be set to start automatically. When it starts, it cooperates with the configuration in the xml file with the same name as the exe, and executes the set command to achieve the effect of self-starting.
Winsw open source address: https://github.com/winsw/winsw
I am using v2.11.0 version, download address: https://github.com/winsw/winsw/releases/tag/v2.11.0
According to .Net Framework Just download the version, such as: WinSW.NET4.exe
sample-minimal.xml is a minimal configuration example.
sample-allOptions.xml is an example of all configurable parameters.
3.2 Modification and configuration
- Create a new service log folder server-logs folder in the nginx installation directory to store nginx service related logs.
- Download the sample-minimal.xml file, or create a new xml file by yourself, write in the configuration information, and put it in the nginx directory. After everything is done, you can register Nginx as a Windows service. Note:
xml文件要和exe文件名称一致
, here are all modified tonginx-service
<service>
<id>nginx</id>
<name>nginx</name>
<description>nginx</description>
<logpath>D:\software\nginx-1.20.2\server-logs\</logpath>
<logmode>roll</logmode>
<depend></depend>
<executable>D:\software\nginx-1.20.2\nginx.exe</executable>
<stopexecutable>D:\software\nginx-1.20.2\nginx.exe -s stop</stopexecutable>
</service>
The relevant parameters are described as follows:
- id: The service ID after installing the windows service, which must be unique.
- name: Service name, which must also be unique. Generally, it should be the same as the id.
- description: service description, can use Chinese, can be used as a note.
- executable: The command to execute, such as the command java to start the springboot application.
- arguments: command execution parameters, such as package path, class path, etc.
Note: Here D:\software\nginx-1.20.2
is my nginx directory, you should pay attention to your own nginx directory when writing xml files
3.3 Register nginx as a windows service
After everything is configured, run the command as an administrator in the nginx installation directory: .\nginx-service.exe install to successfully register it as a Windows service. At this time, we can view the nginx service in the service of Windows Task Manager
and change the startup type to automatic
Tool Miscellaneous Commands
Command Line | meaning |
---|---|
.\nginx-service.exe installl | install service |
.\nginx-service.exe start | start service |
.\nginx-service.exe stop | Out of service |
.\nginx-service.exe restart | restart service |
.\nginx-service.exe uninstall | delete service |
.\nginx-service.exe status | check status |