nginx.service start-pre operation timed out. Terminating.

问题
服务器重启后Nginx服务不会自动启动,手动启动Nginx服务没有出现任何问题。

错误状态
#systemctl status nginx
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: timeout) since Fri 2020-02-28 19:13:13 CST; 6min ago
Docs: http://nginx.org/en/docs/
Process: 806 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=killed, signal=TERM)

Feb 28 19:11:43 izj6cdtn3gxvjysis55g0hz systemd[1]: Starting nginx - high performance web server...
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service start-pre operation timed out. Terminating.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Failed to start nginx - high performance web server.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Unit nginx.service entered failed state.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service failed.

#journalctl -u nginx.service
-- Logs begin at Sat 2020-02-29 03:11:38 CST, end at Fri 2020-02-28 19:20:01 CST. --
Feb 28 19:11:43 izj6cdtn3gxvjysis55g0hz systemd[1]: Starting nginx - high performance web server...
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service start-pre operation timed out. Terminating.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Failed to start nginx - high performance web server.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: Unit nginx.service entered failed state.
Feb 28 19:13:13 izj6cdtn3gxvjysis55g0hz systemd[1]: nginx.service failed.

原因
没有足够的超时值来启动Nginx服务

解决办法
vim /usr/lib/systemd/system/nginx.service
增加TimeoutStartSec参数值:
[Service]
...
TimeoutStartSec=600
-------------------------------------------
例子
cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
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 stop
PrivateTmp=true
LimitNOFILE=51200
LimitNPROC=51200
LimitCORE=51200
TimeoutStartSec=600

[Install]
WantedBy=multi-user.target
--------------------------------------------

这个情况也可能出现在其他服务上面,没有足够的超时值来启动Nginx或Apache或者其他服务

CentOS7
使用任何文本编辑器(例如vi)打开初始化init脚本:
对于Apache: /usr/lib/systemd/system/httpd.service
对于nginx: /usr/lib/systemd/system/nginx.service

增加TimeoutStartSec参数值
[Service]
...
TimeoutStartSec=600

Debian9或者Ubuntu18
可以使用以下命令找到确切初始化脚本
systemctl status apache2.service | grep loaded
systemctl status nginx.service | grep loaded

使用任何文本编辑器(例如vi)打开初始化init脚本:
对于Apache:/lib/systemd/system/apache2.service
对于nginx:/lib/systemd/system/nginx.service

增加TimeoutStartSec参数值
[Service]
...
TimeoutStartSec=600
https://support.plesk.com/hc/en-us/articles/213387149-Nginx-Apache-are-not-running-after-server-reboot-on-a-Plesk-server-service-start-operation-timed-out-Terminating

评论已关闭。