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