Nginx booting under Windows

Time:2021-4-18

Introduction

Recently, nginx is often used. You need to start it manually every time you start it. If you want to set it to automatic execution after starting itstart nginx, find the information, try to summarize.

  • System: Windows 10 home Chinese version
  • Version number: 20h2
  • Operating system version: 19042.685
  • Nginx: version 1.18.0, only the port and root fields are configured.
  • Origin
  • My GitHub

Solutions

Windows can set the startup key by manually modifying the registry. I think it’s troublesome. I’d better find a tool. Find the toolWinSWIt can package any application and manage it as a Windows service. stayhereYou can download the compiled executable file. This version is winsw v3.0.0-alpha.7.

Winsw is used as a global tool

  1. downloadWinSW.exeorWinSW.zip
  2. newly buildmyapp.xml(for more details, seefileandExamples)。
  3. functionwinsw install myapp.xml [options]Installation services.
  4. functionwinsw start myapp.xmlTurn on the service.
  5. functionwinsw status myapp.xmlCheck that the service is up and running.

In the process of actual operation, we found other points for attention

  • Input command time parametermyapp.xmlIt’s not necessary. If you want to omit it, let the configuration file name followWinSW.exeIf the name of the file is consistent, otherwise an error will be reported without the configuration file name parameter.
  • WinSW.exeThe file needs to be placed in the nginx installation directory, otherwise when executing the instruction, it will prompt that the nginx configuration file cannot be found.
  • When there is an error, it will output a log, which is helpful for troubleshooting.

The following is an example of personal configuration steps.

Step 1

Move the downloaded EXE file to the nginx directory and rename it towinsw.exe, new profilewinsw.xml, write the following configuration:

<service>
  <id>nginx service</id>
  <name>Nginx</name>
  <description>This service runs Nginx.</description>
  <env name="NGINX_COMIC" value="%BASE%" />
  <prestart>start D:\nginx-1.18.0\nginx.exe</prestart>
  <executable>D:\nginx-1.18.0\nginx.exe</executable>
  <prestop>D:\nginx-1.18.0\nginx.exe -s stop</prestop>
  <log mode="roll" />
  <onfailure action="none" />
</service>

Nginx booting under Windows

Step 2

Open the command window under the current path (enter CMD on the current file path) and execute the command:

winsw install

Nginx booting under Windows

If you succeed, the following prompt will appear:

Nginx booting under Windows

To check whether the service has been successfully added, right-click “this computer” – > “management” – > “services and Applications” – > “services”:

Nginx booting under Windows

Step 3

Check whether the startup service is normal:

winsw start

A prompt will also appear if the startup is successful. Visit the address configured by nginx to confirm whether it is valid. After confirmation, restart the computer to see if it will start automatically.

reference material