Nginx 服务启动流程记录

1️⃣ 创建服务文件

nano /etc/systemd/system/nginx.service

[Unit]
Description=Nginx Web Server
After=network.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStart=/usr/local/bin/nginx -c /opt/nginx/conf/nginx.conf -p /opt/nginx
ExecReload=/usr/local/bin/nginx -s reload
ExecStop=/usr/local/bin/nginx -s quit
Restart=always

[Install]
WantedBy=multi-user.target
    

2️⃣ 启用并启动服务

sudo pkill -9 nginx
sudo systemctl daemon-reexec
sudo systemctl enable nginx
sudo systemctl start nginx
    

3️⃣ 查看服务状态

sudo systemctl status nginx

● nginx.service - Nginx Web Server
     Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2026-07-01 06:12:22 UTC
   Main PID: 1937738 (nginx)
      Tasks: 3
     Memory: 10.2M
        CPU: 25ms
    

✅ Nginx 已成功启动并运行!