当服务器无法启动并且端口被占用时的解决方法是【Nginx】
查看日志
当我在Nginx上出现500错误时,我检查了`/var/log/nginx/error.log`文件,发现了以下错误。
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: still could not bind()
看起来无法将端口80绑定。
并且据说该地址已经被使用了。有许多进程正在使用相同的端口?我提出了这个假设,并进行了调查。
sudo lsof -i:80
nginx 12395 root 8u IPv4 544463 0t0 TCP *:http (LISTEN)
nginx 12396 nginx 8u IPv4 544463 0t0 TCP *:http (LISTEN)
nginx 12397 nginx 8u IPv4 544463 0t0 TCP *:http (LISTEN)
nginx 12398 nginx 8u IPv4 544463 0t0 TCP *:http (LISTEN)
nginx 12399 nginx 8u IPv4 544463 0t0 TCP *:http (LISTEN)
Nginx正常运行。
根据需要,只需提供一个选项:检查nginx的状态。
sudo service nginx status
nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 金 2019-04-12 18:05:10 JST; 8min ago
Process: 13069 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
Process: 13067 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 13064 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 2388 (code=killed, signal=KILL)
4月 12 18:05:08 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 12 18:05:08 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 12 18:05:09 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 12 18:05:09 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] still could not bind()
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: nginx.service: control process exited, code=exited status=1
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Unit nginx.service entered failed state.
4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: nginx.service failed.
Failed to start The nginx HTTP and reverse proxy server
我预计当我使用sudo lsof -i:80时,会显示正在占用80端口的进程,并且绑定失败。
解决办法:暂时终止进程
sudo kill 12395 12396 12397 12398 12399
然后启动Nginx!
sudo service nginx start
sudo service nginx status
Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 金 2019-04-12 18:19:48 JST; 2min 11s ago
Process: 13243 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 13241 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 13238 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 13245 (nginx)
CGroup: /system.slice/nginx.service
├─13245 nginx: master process /usr/sbin/nginx
├─13246 nginx: worker process
├─13247 nginx: worker process
├─13249 nginx: worker process
└─13250 nginx: worker process
4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13241]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13241]: nginx: configuration file /etc/nginx/nginx.conf test is successful
4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.
只要这样做,就可以解决错误并使进程正常运行了!
参考链接:https://easyramble.com/nginx-emerg-bind-failed.html
请以母语中文进行改写。