服务器重启后nginx无法启动。

22

我已经从自行编译的nginx 1.2.6版本迁移到Ubuntu 13.04上的1.4.1版本。

通过Ubuntu PPA安装,http://wiki.nginx.org/Install#Ubuntu_PPA。所有安装都很顺利。

手动操作可以使用以下方式重新加载/启动/停止服务:

sudo service nginx <command>

服务器重启后,nginx不能自动启动。

在之前的手动安装中,我有一个手写的Upstart脚本放在/etc/init目录下,可以正常工作。而通过PPA安装则会创建/etc/init.d/脚本,因此我希望使用PPA方法而不是进行一些魔改。

可能是因为我对服务接口不太熟悉,所以可能有些东西我遗漏了。

我还检查了/var/log/nginx/access.log和error.log,发现在启动过程中没有记录任何内容。

我该怎么做来解决这个问题?

7个回答

34

sudo update-rc.d nginx enable 对我有用。


2
太好了。update-rc.d nginx defaults 对我没有任何作用,告诉我 init.d 已经存在。 - shukshin.ivan

19

我对Ubuntu的经验不是很丰富 - 但你尝试过运行chkconfig吗?

你可以尝试:

# sudo update-rc.d nginx defaults

或者:

# sudo apt-get install chkconfig

# sudo chkconfig nginx on

似乎可能正在运行。现在重新启动。~$ sudo update-rc.d nginx defaults 添加/etc/init.d/nginx的系统启动... /etc/rc0.d/K20nginx -> ../init.d/nginx /etc/rc1.d/K20nginx -> ../init.d/nginx /etc/rc6.d/K20nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S20nginx -> ../init.d/nginx - Geuis
5
补充说明,通过apt-get安装不了chkconfig。 - Geuis
是的,可以运行。看起来在Ubuntu 13.04中没有chkconfig可用。 - Geuis
5
您可以使用以下命令检查nginx配置:sudo nginx -t该命令通常会突出显示任何问题。您是否同时安装了两个版本的nginx? - outrunthewolf

6

对于Ubuntu系统:

首先检查状态以确保没有错误

systemctl status nginx.service

它会显示错误或显示尚未启动

如果它没有启动,那么就需要启用它

sudo /etc/init.d/systemctl enable nginx
sudo reboot

2

2016年3月28日更新

我已经将下面我提出的对Ubuntu Upstart脚本的小改进作为一个pull request提交给了NGinX wiki。 我的pull request已经被接受,补丁已经合并


我使用的是Ubuntu 14.04 LTS,也遇到了同样的问题;NginX可以使用sudo service nginx restart正常启动,但不能在服务器启动时自动启动。

我按照NGinX Ubuntu Upstart Wiki页面的说明进行操作,但一开始它并没有起作用。所以我深入地挖掘了一下。首先我发现Ubuntu确实尝试在启动时启动NGinX,但失败了:

$ sudo cat /var/log/boot.log
...
* Starting nginx http daemon                                            [fail]
...

我找到了initctl upstart作业的日志文件:

$ cat /var/log/upstart/nginx.log
...
nginx: [emerg] host not found in upstream "<mydomain>:443" in /etc/nginx/sites-enabled/mydomain:2
...

所以,我发现NGinX启动脚本是在网络接口启动之前运行的。这个Server Fault答案给了我修复NginX Ubuntu Upstart脚本所需的提示。总结一下,缺少了一个'!',IFACE=lo需要改为IFACE!=lo。为了方便起见,我在下面粘贴了完整的脚本:

# nginx

description "nginx http daemon"
author "George Shammas <georgyo@gmail.com>"

start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [!2345]

env DAEMON=/usr/sbin/nginx
env PID=/var/run/nginx.pid

expect fork
respawn
respawn limit 10 5
#oom never

pre-start script
        $DAEMON -t
        if [ $? -ne 0 ]
                then exit $?
        fi
end script

exec $DAEMON

重启后,我检查了NGinX是否正在运行:

$ sudo initctl list | grep nginx
nginx start/running, process 1551

我会通知脚本作者我所发现的问题。

我遇到了同样的问题,但是我没有看到你发布的脚本在哪里(在你的盒子上)。在我的情况下,我使用的是Ubuntu 15.10,我怀疑启动失败的问题是因为我的Express应用程序在Nginx之后启动(我使用PM2)。由于某种原因,如果我在GUI模式下启动,一切都正常,但是当在无头模式下启动时,Nginx无法启动。 - Pavel P
嗨@Pavel,请查看“Ubuntu Upstart”脚本[1]。在那里,它说:“将此文件保存为/ etc / init / nginx.conf”,因此这是脚本的位置。是否可能在GUI模式下,当NGinX启动时,网络接口已经启动,在无头模式下没有启动? [1] https://www.nginx.com/resources/wiki/start/topics/examples/ubuntuupstart/# - Visionscaper
经过一些谷歌搜索和测试,我在nginx日志中看到了它为什么无法启动,并且似乎是因为它无法连接到我的node.js应用程序,该应用程序被声明为localhost:3333(nginx是反向代理)。 显然由于时间的缘故,“localhost”在启动时可能无法解析(什么鬼!?)。 在我的情况下,我不得不将其替换为127.0.0.1,这解决了问题。 - Pavel P

0

如果你正在使用 apache2,则必须停止它

service apache2 stop

然后再启动nginx服务

service nginx start

0
除了Visionscaper的回答之外,从标准的trusty repos安装的nginx使用sysvinit服务文件而不是upstart服务文件。因此,不要编辑upstart服务的nginx服务,而是编辑rc-sysinit upstart服务。对于这个问题,使用upstart overrides是一个好主意:
# echo "start on (filesystem and net-device-up IFACE!=lo) or failsafe-boot" > /etc/init/rc-sysinit.override

0

对我来说,这是因为在安装sudo apt-get install nginx之前,我没有执行sudo apt-get update --fix-missing

架构:armv7l

操作系统:raspLITE


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接