Nginx 设置为 Linux 服务器系统服务

Nginx Linux About 4,188 words

查看系统服务

检查Nginx服务是否已配置为systemd服务。

若输出包含nginx.service,说明Nginx已注册为systemd系统服务。

若无输出,说明未启用systemctl管理(可能是手动编译安装,未配置服务文件)。

systemctl list-unit-files | grep nginx

输出

[root ~]# systemctl list-unit-files
UNIT FILE                              STATE           PRESET
nginx.service                         enabled        disabled

设置开机自启

systemctl enable nginx

输出

[root ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

查看配置文件

systemctl cat nginx

如果是手动编译的源码,可以直接拷贝到 /usr/lib/systemd/system/nginx.service文件。

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

加载新配置

如果修改过nginx.service文件,需要重新加载systemd配置。

systemctl daemon-reload

开启 Nginx

systemctl start nginx

如果之前以nginx命令运行了,使用以下命令停止Nginx

nginx -s stop

或者

pkill nginx

查看进程是否结束

ps aux | grep nginx

查看 Nginx 状态

使用systemctl启动后,可使用如下命令查看当前Nginx进程状态。

systemctl status nginx

Active含义:

  • 输出中active (running) 表示服务正在运行;
  • enabled表示服务开机自启;
  • inactive (dead)表示服务已注册但未运行;
  • disabled表示服务已注册但不开机自启。

示例一:

[root ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
     Active: active (running) since Mon 2025-12-22 06:30:29 UTC; 14s ago
    Process: 459143 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 459144 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 459145 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 459146 (nginx)
      Tasks: 3 (limit: 9379)
     Memory: 3.4M
        CPU: 81ms
     CGroup: /system.slice/nginx.service
             ├─459146 "nginx: master process /usr/sbin/nginx"
             ├─459147 "nginx: worker process"
             └─459148 "nginx: worker process"

Dec 22 06:30:29 systemd[1]: Starting nginx.service - The nginx HTTP and reverse proxy server...
Dec 22 06:30:29 nginx[459144]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 22 06:30:29 nginx[459144]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 22 06:30:29 systemd[1]: Started nginx.service - The nginx HTTP and reverse proxy server.

示例二:

[root ~]# systemctl status nginx
○ nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
     Active: inactive (dead)

查看是否启用 Nginx

  • 返回enabled → 已开机自启;
  • 返回disabled → 未开机自启;
  • 返回masked → 服务被屏蔽(无法启动);
  • 返回failed → 服务配置有误。
systemctl is-enabled nginx

重新加载 Nginx 配置

systemctl reload nginx

停止 Nginx

systemctl stop nginx

查看配置文件位置

可以直接使用status命令(Loaded字段中包含了配置文件位置)

systemctl show -p FragmentPath nginx

输出

[root ~]# systemctl show -p FragmentPath nginx
FragmentPath=/usr/lib/systemd/system/nginx.service

禁用开机自启

systemctl disable nginx

编辑配置

使用edit命令可以编辑配置(与主配置文件独立,不影响主配置)

systemctl edit nginx

如添加终止后自动重启策略。

[Service]
Restart=always
RestartSec=5

edit后需要重新加载systemd以及重启Nginx

systemctl daemon-reload
systemctl restart nginx

查看日志

journalctl -u nginx

输出

[root]# journalctl -u nginx
Dec 22 06:30:29 systemd[1]: Starting nginx.service - The nginx HTTP and reverse proxy server...
Dec 22 06:30:29 nginx[459144]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 22 06:30:29 nginx[459144]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 22 06:30:29 systemd[1]: Started nginx.service - The nginx HTTP and reverse proxy server.

关键说明

只有通过yum/apt等包管理器安装的Nginx,默认会自动配置nginx.service

手动编译安装的Nginx,需要手动创建/usr/lib/systemd/system/nginx.service文件,才能用systemctl管理。

Views: 6 · Posted: 2026-04-27

———         Thanks for Reading         ———

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Prev Post
Today In History
Browsing Refresh