Open david2tdw opened 3 years ago
如果系统使用了防火墙(firewall),执行下面命令允许http/https
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
nginx默认监听80端口,如果未关闭防火墙需要配置iptables规则开放80端口.
编辑配置文件:vim /etc/sysconfig/iptables
在文件中间添加iptables规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启防火墙:
service iptables restart
或者关闭iptables规则:
iptables -F && iptables -t nat -F
查看Nginx服务对应的端口是否启动
netstat -lnt | grep 80
查看 nginx.conf 配置文件目录和nginx的安装目录
nginx -t
验证nginx安装成功 直接在浏览器输入linux服务器的ip地址,查看是否能显示centOS的介绍页面。
nginx启动报错 - bind() to 0.0.0.0:8090 failed (13: Permission denied)
原因selinux限制了http的端口
解决办法:关闭seLinux
修改/etc/selinux/config文件中的SELINUX值
SELINUX=disabled
然后重启。如果不想重启系统,使用命令setenforce 0
注: setenforce 1 设置SELinux 成为enforcing模式 setenforce 0 设置SELinux 成为permissive模式
[Nginx] Centos nginx bind() failed permission denied 关闭SELinux
查看防火墙状态:
systemctl status firewalld
或者
firewall-cmd --state
查看监听的端口
netstat -lnpt
PS: centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
检查端口被哪个进程占用
netstat -lnpt |grep 5672
查看进程的详细信息
ps 6832
中止进程
kill -9 6832
在添加端口到/etc/sysconfig/iptables后,需要启动nginx,才能查看端口的监听情况。没启动查询不到对应的端口。
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9999 -j ACCEPT
netstat -lnpt
使用命令直接修改(注意:需要save生效, -I是添加在第一条)
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT #开放8080端口
service iptables save #使配置生效
iptables命令小结
iptables -I INPUT -p tcp -m multiport --dport 22,80 -j ACCEPT #同时开放22和80端口
iptables -I INPUT -p tcp --dport 5000:6000 -j ACCEPT #开放5000-6000端口
iptables -I INPUT -p all -s 0.0.0.0/0 -j ACCEPT #允许某个网段的ip访问
iptables -I INPUT -s 0.0.0.0 -p tcp --dport 8080 -j ACCEPT #允许某个ip的8080端口访问
iptables -I INPUT -p tcp -s 0.0.0.0 -j DROP #禁止某台主机访问
iptables -D INPUT 7 #移除第七条规则
(注:放行使用ACCEPT,禁止使用DROP)
每次重启后要检查iptables是否启动起来,iptables在重启后不会自动启动。
systemctl status iptables
每次重启后 要检查iptables是否启动起来,iptables在重启后不会自动启动。
systemctl status iptables
查看SELinux状态, 如果是Enforcing 的话会阻止网络访问。
getenforce
临时关闭SELinux,Linux重启后SELinux恢复原状态
setenforce 0
重新启动网络访问,否则无法ping通。
service network start
iptables 命令:
service iptables start
service iptables stop
service iptables restart
service iptables status #如果没有启动,将提示没有启动,否则将会显示已经添加的过滤规则
service iptables save #保存添加的规则,记得每次更改规则以后这样保存一下
配置 EPEL源
安装Nginx
安装成功后,默认的网站目录为: /usr/share/nginx/html
默认的配置文件为:/etc/nginx/nginx.conf
自定义配置文件目录为: /etc/nginx/conf.d/
操作Nginx
1.启动 Nginx
2.停止Nginx
3.重启Nginx
4.查看Nginx状态
5.启用开机启动Nginx
6.禁用开机启动Nginx
Centos 7下安装配置Nginx