其他进程占用了端口,导致无法重新启动nginx

目前状态

尝试启动nginx时,会提示“地址已在使用中”,导致无法重新启动。

$ sudo /usr/sbin/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 -v
nginx version: nginx/1.12.2

导致这个结果是什么?

原因是Nginx发生异常终止,导致旧的httpd进程残留。

应对措施 duì cuò shī)

可以使用lsof命令来显示旧进程,并使用kill命令终止它们。

$ sudo lsof -i | grep nginx
nginx    3254     root    6u  IPv4  20710      0t0  TCP *:http (LISTEN)
nginx    3254     root    7u  IPv6  20711      0t0  TCP *:http (LISTEN)
nginx    3255    nginx    6u  IPv4  20710      0t0  TCP *:http (LISTEN)
nginx    3255    nginx    7u  IPv6  20711      0t0  TCP *:http (LISTEN)
nginx    7013     root    6u  IPv4 227085      0t0  TCP *:vcom-tunnel (LISTEN)
nginx    7014    nginx    6u  IPv4 227085      0t0  TCP *:vcom-tunnel (LISTEN)

能够发现存在具有3254、3255、7013、7014 ID的进程。

$ sudo kill -9 3254
$ sudo kill -9 3255
$ sudo kill -9 7013
$ sudo kill -9 7014

通过这个,就可以启动了。

广告
将在 10 秒后关闭
bannerAds