前言

执行 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()

提示的意思是80端口被占用。

排查问题

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

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服务进程已卡死,访问提示502错误。这时需要重启nginx。

解决方案

kill掉所有的nginx进程,重启nginx即可。