启动Nginx服务提示98: Address already in use

今日早晨去到公司,就被其他人告知官网所有页面打开都是502。平常都是正常的怎么就突然页面就502了呢,下面就开始 troubleshooting。

公司web服务器,是搭建在 centos 上的lnmp环境,首先网站502肯定是服务器端错误,首先想到是不是nginx服务挂掉了,然后执行service nginx status,查看下nginx的状态:nginx is stopped,果然是nginx服务挂了。执行service nginx start启动nginx服务,无法开启,提示错误如下:

Starting nginx: 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()

大概意思就是:端口地址已被使用。很大的可能nginx服务进程卡死了,导致80端口被占用。
解决办法:

首先用lsof -i :80查看80端口被什么程序占用,返回结果如下:

1
2
3
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3274 root 6u IPv4 10664 0t0 TCP :http (LISTEN)
nginx 3547 nginx 6u IPv4 10664 0t0 TCP :http (LISTEN)

发现是nginx进程占用了80端口,所以我们把nginx进程kill掉,重新启动服务。命令如下(kill 掉所有的nginx进程):

1
2
3
kill -9 lsof -i :80 |grep nginx |grep -v grep|awk '{print $2}'
service nginx start
Starting nginx... [ OK ]

OK,nginx服务已经成功启动!