Nginx 纳入 systemd 管理

将源码安装的 Nginx 纳入 systemd 管理

适用于通过源码编译安装(默认路径 /usr/local/nginx)的 Nginx,配置完成后即可使用 systemctl 统一管理,并支持开机自启。


一、背景

源码编译安装的 Nginx 默认不带 systemd 服务单元,只能通过 ./nginx、nginx -s stop 等命令手动启停,无法使用 systemctl、也不能开机自启。

通过手动创建一个 nginx.service 单元文件,即可把它纳入 systemd 规范管理,享受以下能力:

  • systemctl start/stop/reload/restart nginx 统一启停
  • systemctl status nginx 查看运行状态
  • systemctl enable nginx 开机自启
  • 进程异常退出时自动拉起
  • 日志统一接入 journalctl

二、创建服务单元文件

创建文件 /usr/lib/systemd/system/nginx.service,内容如下(路径需与本机实际安装位置一致):

ini 复制代码
[Unit]
Description=The NGINX HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

关键参数说明

参数 作用
Type=forking Nginx 以守护进程方式后台运行并 fork,systemd 通过 PID 文件跟踪主进程
PIDFile 指向 nginx.pid,须与实际路径一致,否则 systemd 会误判进程状态
ExecStartPre=...nginx -t 启动前先做配置语法检查,配置有误则不启动
ExecStart 启动 Nginx
ExecReload=...-s reload 平滑重载配置,不中断业务
ExecStop=...-s quit 优雅停止,处理完当前请求再退出
Restart=on-failure 进程异常退出时自动拉起(正常 stop 不触发)
WantedBy=multi-user.target 多用户运行级别下生效,支持开机自启

也可以用以下命令一次性写入,避免编辑器粘贴问题:

bash 复制代码
cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
EOF

三、重载配置并设置开机自启

bash 复制代码
# 重载 systemd,识别新服务
systemctl daemon-reload

# 设置开机自启
systemctl enable nginx

四、切换为 systemd 托管

若 Nginx 当前是手动启动的,直接 systemctl start 会因端口被占用而失败,需先停掉手动进程,再由 systemd 拉起:

bash 复制代码
# 1. 优雅停止手动启动的 nginx
/usr/local/nginx/sbin/nginx -s quit

# 2. 确认进程已退出、端口已释放
sleep 2
ps aux | grep nginx

# 3. 由 systemd 启动
systemctl start nginx

切换瞬间会有约 1~2 秒中断,建议放在低峰期或维护窗口执行。


五、验证

bash 复制代码
systemctl status nginx

看到 Active: active (running)、Main PID 有值、且显示 master + worker 进程即表示接管成功。

再验证业务与重载功能:

bash 复制代码
systemctl reload nginx          # 平滑重载
systemctl status nginx          # 仍为 active (running)
curl -I http://127.0.0.1:端口    # 业务正常返回

六、常用命令速查

bash 复制代码
systemctl status nginx     # 查看状态
systemctl start nginx      # 启动
systemctl stop nginx       # 停止
systemctl reload nginx     # 平滑重载配置(推荐,不中断)
systemctl restart nginx    # 重启(有短暂中断)
systemctl enable nginx     # 开机自启
systemctl disable nginx    # 关闭开机自启
journalctl -u nginx -e     # 查看 systemd 收集的日志

七、注意事项

  1. 统一用 systemctl 管理 ,不要再混用 ./nginx 手动启停,否则会导致 systemd 记录的状态与实际进程不一致。
  2. PIDFile 路径必须准确,写错会导致 systemd 无法正确跟踪进程。
  3. 修改单元文件后 需执行 systemctl daemon-reload 才会生效。
  4. Restart=on-failure 可在进程异常崩溃时自动拉起,提升服务健壮性。
相关推荐
fruge1 小时前
ClipSketch AI视频转手绘故事板实战:关键帧选取、Gemini绘图与文案生成
网络·网络协议·http
qeen871 小时前
【Linux】线程的基本认识及与进程的关系、对比
linux·运维
Smoothcloud润云3 小时前
GPU服务器租用训练缓存小文件与inode耗尽排查实战
运维·服务器·人工智能·缓存·ai·gpu算力
Lsetea3 小时前
DNS-01 委托有 CNAME 却查不到 TXT?用 CertbotX 核对签发前和挑战中的记录
运维·https·ssl·dns·acme
lijie2353 小时前
第81天 网络问题
网络
To Be Clean Coder4 小时前
【往事如风】从「缝缝补补」到领域隔离:一次策略下发模块的重构
网络·安全·重构
Decay__4 小时前
集群安装部署实战 —— kubeadm 搭建 3 控制面 + 3 工作节点高可用集群
运维·云原生·容器·kubernetes·k8s
都在酒里4 小时前
嵌入式台架自动化:用 Python 回归测试 RS-485 和 CAN
网络·python·自动化
llilian_165 小时前
单北斗信创 NTP 服务器自主可控溯源要求
运维·服务器·功能测试·嵌入式硬件·51单片机·软件工程