Systemctl 配置 Nginx 自启动显示超时

2018-08-04 16:45:56 +08:00
 RobertYang

nginx.service 是这样编写的

    [Unit]
    Description=A high performance web server and a reverse proxy server
    After=network.target network-online.target nss-lookup.target

    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    PrivateDevices=yes
    SyslogLevel=err

    ExecStart=/usr/local/nginx/sbin/nginx -g 'pid /usr/local/nginx/logs/nginx.pid; error_log stderr;'
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    KillMode=mixed

    [Install]
    WantedBy=multi-user.target

开机后 Nginx 没有正确启动

    $ sudo systemctl status nginx
    * nginx.service - A high performance web server and a reverse proxy server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: failed (Result: timeout) since Sat 2018-08-04 16:26:14 CST; 10s ago

    Aug 04 16:24:44 isthnew systemd[1]: Starting A high performance web server and a reverse proxy server...
    Aug 04 16:26:14 isthnew systemd[1]: nginx.service: Start operation timed out. Terminating.
    Aug 04 16:26:14 isthnew systemd[1]: nginx.service: Failed with result 'timeout'.
    Aug 04 16:26:14 isthnew systemd[1]: Failed to start A high performance web server and a reverse proxy server.

系统是 Arch,nginx 版本 1.15.2,貌似很早之前就出现过了,但是没注意,开机后使用 sudo systemctl start nginx 来启动 nginx 一问题都没有。

6864 次点击
所在节点    NGINX
28 条回复
wqyyy
2018-08-04 19:24:43 +08:00
仅作对比:

```
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
```
MeteorCat
2018-08-04 19:27:11 +08:00
手动编译,手动写 systemctl ?
RobertYang
2018-08-04 20:36:43 +08:00
@MeteorCat 是的
RobertYang
2018-08-04 20:58:10 +08:00
@wqyyy 替换了你写的这个也启动不了,systemd 显示启动时间超过 1min30s,然后显示错误
cominghome
2018-08-04 20:58:37 +08:00
看看 nignx error-log 和 system message,或者直接启动 nginx 试试排除 nginx 配置原因,nginx.service 就那两行,不一定是 systemctl 的问题
Hardrain
2018-08-04 20:59:29 +08:00
journalctl -xe -u nginx.service 的输出?
wqyyy
2018-08-04 21:02:36 +08:00
@RobertYang 这样子的话我也不知道了,这个是从 Ubuntu 源里拆出来之后略作修改的。
RobertYang
2018-08-04 21:24:55 +08:00
@cominghome @Hardrain nginx 日志没有记录到错误,journalctl 还是提示启动超时的那两行
iUnix
2018-08-04 21:58:03 +08:00
RobertYang
2018-08-04 22:16:39 +08:00
@iUnix 依然不工作,放弃了
iUnix
2018-08-04 23:24:04 +08:00
@RobertYang 手动 ok,我猜它启动时某些服务还没 ready,问题应该在 after 那里。
iUnix
2018-08-04 23:25:54 +08:00
@RobertYang 把启动顺序往后调看看是不是这个问题。
henryshen233
2018-08-04 23:27:09 +08:00
@iUnix 有些时候 ssh 上去就开了,默认开机不会打开,这种是什么原因呢
iUnix
2018-08-04 23:29:54 +08:00
@henryshen233 有没有日志? systemctl 或者 nginx
RobertYang
2018-08-05 00:14:49 +08:00
@iUnix systemctl 日志就是 nginx.service: Start operation timed out. Terminating. nginx 未记录到日志。
RobertYang
2018-08-05 00:16:54 +08:00
@iUnix 刚刚看错楼了,我等下调到 after=php-fpm 后面试试
RobertYang
2018-08-05 00:27:10 +08:00
@iUnix 调到 php-fpm 之后启动时间依然超过 1 分 30 秒然后报错
zjb861107
2018-08-05 00:29:03 +08:00
好像权限问题~我以前也遇到过
winterbells
2018-08-05 00:56:35 +08:00
```
[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
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
```
Nginx 默认的文件
Nginx 是手动编译的,用这个在 CentOS 和 Ubuntu 上都正常启动
RobertYang
2018-08-05 01:12:35 +08:00
@winterbells 同样不行,有点玄学了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/476872

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX