启动nginx时报错-提示端口被占用
在centos搭建服务器时遇到systemctl start nginx时提示ob for nginx.service failed
查看httpd占用状态
在使用centos搭建httpd服务环境时先行启动了httpd导致了服务器端口80 被占用如下
[root@centos conf.d]# netstat -apn|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10300/httpd
这里可以看出来80端口已经被httpd占用
启动nginx服务
[root@centos]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
此时开启nginx已经提示错误,使用命令来查看该服务的状态
[root@centos]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2020-04-19 11:06:44 CST; 10s ago
Docs: http://nginx.org/en/docs/
Process: 11066 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Apr 19 11:06:41 centos systemd[1]: Starting nginx - high performance web server...
Apr 19 11:06:42 centos nginx[11066]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 19 11:06:42 centos nginx[11066]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 19 11:06:43 centos nginx[11066]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 19 11:06:43 centos nginx[11066]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 19 11:06:44 centos nginx[11066]: nginx: [emerg] still could not bind()
Apr 19 11:06:44 centos systemd[1]: nginx.service: control process exited, code=exited status=1
Apr 19 11:06:44 centos systemd[1]: Failed to start nginx - high performance web server.
Apr 19 11:06:44 centos systemd[1]: Unit nginx.service entered failed state.
Apr 19 11:06:44 centos systemd[1]: nginx.service failed.
以上信息可以看出该服务的端口80被占用所以导致nginx无法启动,此时我们需要将上方的httpd给关闭,然后再次启用nginx
[root@centos conf.d]# service httpd stop
Redirecting to /bin/systemctl stop httpd.service
启动nginx
[root@centos conf.d]# systemctl restart nginx.service
[root@centos conf.d]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-04-19 11:21:46 CST; 15min ago
Docs: http://nginx.org/en/docs/
Process: 13715 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 13716 (nginx)
注意,文中的“centos”为类linux的操作系统,需有一定基础的童鞋操作。
总结
之所以出现此类错误,是由于小编手动重启httpd服务,而不是先重启nginx导致
推荐阅读
欢迎关注我的公众号“魔搜索”,原创技术/资源/活动文章先行推送。

本文介绍在CentOS环境下启动Nginx时遇到端口80被HTTPD占用的问题,提供了解决方案,包括如何检查端口占用状态、停止HTTPD服务以及重启Nginx。
1702

被折叠的 条评论
为什么被折叠?



