服務器重啟後 Nginx 未自動拉起,執行 nginx -s reload 提示 open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)。核心原因:Nginx 進程未運行,pid 文件自然不存在。直接指定配置文件啟動主進程即可自動生成 pid 並恢復服務。
[root@localhost sbin]# ./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
ls logs/ 確認無 nginx.pid,說明主進程不存在。
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-c 顯式指定配置文件絕對路徑。logs/nginx.pid 寫入 PID。nginx -s reload/stop/quit。[root@localhost logs]# ll
total 12
-rw-r--r-- 1 root root 1246 Dec 9 18:10 access.log
-rw-r--r-- 1 root root 516 Dec 10 15:39 error.log
-rw-r--r-- 1 root root 5 Dec 10 15:38 nginx.pid # 已生成
手動啟動僅治標,生產環境務必註冊 systemd/service:
# 建立 /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
nginx -t 驗證配置語法無誤。netstat -antp | grep :80 確認 80 端口未被佔用。error.log 檢查啟動錯誤 (權限、路徑、模組缺失)。nginx.conf 中 pid logs/nginx.pid; 路徑與實際一致。「pid 不存在」本質是進程未活,-c 參數直擊核心,30 秒恢復服務;長期治本靠 systemd 托管。