nginx启动提示nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx启动报错

今天在centos7部署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默认启动端口是80,之前刚好在这个服务器启动了个80端口的tomcat。现解决方式如下:

安装iptables服务

需要通过防火墙开放对外端口。如果服务器上没有iptables服务,需要安装。如果有,则跳过。

yum install iptables-services
systemctl mask firewalld.service
systemctl enable iptables.service
systemctl enable ip6tables.service

配置端口

进入iptables配置80端口,因为nginx默认是由80端口访问

vi /etc/sysconfig/iptables

打开后,默认的配置信息如下(加粗部分为新添加的):
INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:696]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

A INPUT -p tcp -m state --state NEW -m tcp --dport 30000:30999 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

后续需要开放其它端口,也是在此文件中添加修改即可!

修改完后,保存退出文件编辑。

:wq

重启防火墙

systemctl restart iptables.service

查看80端口被占用的进程

lsof -i:80

通过kill命令干掉该进程

kill -9 进程号

启动nginx

这个时候在启动nginx,一切正常了!

/usr/local/nginx/sbin/nginx
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值