DEBIAN 8.4 - NGINX 启动失败

DEBIAN 8.4 - NGINX start fails

我在 debian 8.0、8.4 或 Ubuntu 14.04 上的不同 OS 版本中有 4 个单元 运行ning Nginx。从昨天开始,NGINX 在我的开发站上启动失败(2 天前 运行 还不错)。奇怪!

$ sudo service nginx start

$ Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

所以正在寻找日志:

$ sudo systemctl -l status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: failed (Result: exit-code) since Thu 2016-04-28 22:15:18 CEST; 6min ago
  Process: 24148 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 24145 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Apr 28 22:15:15 mercure nginx[24148]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:15:16 mercure nginx[24148]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:15:16 mercure nginx[24148]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:15:17 mercure nginx[24148]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:15:17 mercure nginx[24148]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:15:18 mercure nginx[24148]: nginx: [emerg] still could not bind()
Apr 28 22:15:18 mercure systemd[1]: nginx.service: control process exited, code=exited status=1
Apr 28 22:15:18 mercure systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Apr 28 22:15:18 mercure systemd[1]: Unit nginx.service entered failed state

第二期:

$ sudo journalctl -xn
-- Logs begin at Thu 2016-04-28 08:41:36 CEST, end at Thu 2016-04-28 22:23:08 CEST. --
Apr 28 22:22:54 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:55 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:55 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:56 mercure nginx[24290]: nginx: [emerg] still could not bind()
Apr 28 22:22:56 mercure systemd[1]: nginx.service: control process exited, code=exited status=1
Apr 28 22:22:56 mercure systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
-- 
-- The result is failed.
Apr 28 22:22:56 mercure systemd[1]: Unit nginx.service entered failed state.
Apr 28 22:22:56 mercure sudo[24263]: pam_unix(sudo:session): session closed for user root
Apr 28 22:23:08 mercure sudo[24293]: jeby6372 : TTY=pts/3 ; PWD=/etc/nginx ; USER=root ; COMMAND=/bin/journalctl -xn
Apr 28 22:23:08 mercure sudo[24293]: pam_unix(sudo:session): session opened for user root by jeby6372(uid=0)

我在 /var/log/syslog 中发现了一个奇怪的 DHCP 行为,不是吗?

Apr 28 22:22:24 mercure NetworkManager[676]: <info> (eth0): DHCPv4 state changed renew -> renew
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   address 192.168.1.3
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   plen 24 (255.255.255.0)
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   gateway 192.168.1.254
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   server identifier 192.168.1.1
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   lease time 120
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   nameserver '192.168.1.1'
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   domain name 'hznteam.lan'
Apr 28 22:22:24 mercure NetworkManager[676]: <info>   domain search 'hznteam.lan.'
Apr 28 22:22:24 mercure dbus[722]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Apr 28 22:22:24 mercure dbus[722]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Apr 28 22:22:24 mercure nm-dispatcher: Dispatching action 'dhcp4-change' for eth0
Apr 28 22:22:24 mercure dhclient: bound to 192.168.1.3 -- renewal in 47 seconds.
Apr 28 22:22:53 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:54 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:54 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:55 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:55 mercure nginx[24290]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 28 22:22:56 mercure nginx[24290]: nginx: [emerg] still could not bind()
Apr 28 22:22:56 mercure systemd[1]: nginx.service: control process exited, code=exited status=1
Apr 28 22:22:56 mercure systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Apr 28 22:22:56 mercure systemd[1]: Unit nginx.service entered failed state

.

运行ninx 作为前台任务显示 0.0.0.0:80 已被使用,但我没有安装 apache

jeby6372@mercure:~$ sudo nginx -g "daemon off;"
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

nmap 命令结果表明没有程序正在侦听端口 80

jeby6372@mercure:~$ nmap -p 80 localhost

Starting Nmap 6.47 ( http://nmap.org ) at 2016-04-29 18:57 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000054s latency).
Other addresses for localhost (not scanned): 127.0.0.1
PORT   STATE SERVICE
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

有什么想法吗?

尝试运行前台模式下的nginx,看看是什么原因失败:

nginx -g "daemon off;"

如果您仍然收到错误:

bind() to 0.0.0.0:80 failed

那么也许服务 运行 已经在端口 80 上了。 然后尝试使用 lsof 命令找出它是哪个服务,并使用 kill -15 或 kill -9 将其杀死。您可以通过 运行ning:

检查端口 80 上是否有东西 运行ning
nmap -p 80 localhost

看来,某些其他服务正在使用您尝试通过 nginx 访问的地址或端口。因此找出正在使用该端口的任何第二个进程。然后关闭它然后启动 nginx .