SoFunction
Updated on 2025-04-13

How to configure Nginx to use systemctl management

Configure Nginx to use systemctl management

1. Create Systemd Service Unit File

First, create a Systemd service unit file for Nginx. Usually, this file is located in the /etc/systemd/system/ directory.

sudo nano /etc/systemd/system/

2. Write service unit files

In the open editor, enter the following:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=

3. Reload Systemd configuration

After saving and closing the file, reload the Systemd configuration to make the new service unit file take effect.

sudo systemctl daemon-reload

4. Start and enable Nginx service

Start the Nginx service and set it to boot automatically:

sudo systemctl start nginx
sudo systemctl enable nginx

5. Check Nginx service status

Make sure that the Nginx service has been started successfully:

sudo systemctl status nginx

You should see an output similar to the following, indicating that the Nginx service is running:

● - The NGINX HTTP and reverse proxy server
Loaded: loaded (/etc/systemd/system/; enabled; vendor preset: disabled)
Active: active (running) since ...

6. Stop and reload the Nginx service

If you need to stop or reload the Nginx service, you can use the following command:

sudo systemctl stop nginx
sudo systemctl reload nginx

7. Configure Nginx

Make sure your Nginx configuration file /usr/local/nginx/conf/ is configured correctly.

You can edit this file as needed and reload the Nginx service to apply the changes.

8. Log files

Nginx log files are usually located in the /usr/local/nginx/logs/ directory.

You can view these log files for more information:

cat /usr/local/nginx/logs/
cat /usr/local/nginx/logs/

Through the above steps, you can configure Nginx to use systemctl management to facilitate starting, stopping, and managing Nginx services.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.