版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
最近在使用Nginx部署一个项目时,报错80端口被占用:
(apollo) user@root:~/program/apollo$ sudo /usr/sbin/nginx -c ~/program/apollo/fake.conf
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()
于是检查端口使用情况:
(apollo) user@root:~/program/apollo$ netstat -ntlp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:63342 0.0.0.0:* LISTEN 26439/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6942 0.0.0.0:* LISTEN 26439/java
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 2253/python3
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
tcp6 0 0 ::1:6379 :::* LISTEN -
提示切到root用户才有权限查看:
root@root:/home# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
wps 29927 starx 16u IPv4 379103 0t0 TCP starx-B85-D3V:43408->39.130.135.226:http (CLOSE_WAIT)
nginx 30464 root 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30464 root 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30465 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30465 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30466 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30466 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30467 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30467 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30468 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30468 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30469 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30469 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30470 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30470 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30471 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30471 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
nginx 30472 www-data 6u IPv4 308715 0t0 TCP *:http (LISTEN)
nginx 30472 www-data 7u IPv6 308716 0t0 TCP *:http (LISTEN)
原来的nginx已经在监听80端口,这种情况可以将配置文件放到nginx的默认配置路径下后重启nginx即可:
(apollo) root@root:~/program/apollo$ cp ~/program/apollo/django.conf /etc/nginx/sites-enabled/
(apollo) root@root:~/program/apollo$ /etc/init.d/nginx restart