默認情況下,Centos7防火牆是打開的,如果你沒有關閉他,你安裝nginx后啟動,是無法訪問到nginx服務的。
所以需要做這件事
1、啟動nginx
啟動前先看它啟動沒有,通過linux命令查看所有端口,看看有沒有80
netstat -ntlp
如果沒有,則通過命令啟動
啟動:systemctl start nginx
停止:systemctl stop nginx
重啟:systemctl restart nginx
2、設置nginx開啟啟動
sudo chkconfig nginx on
3、關閉Centos自帶的firewall防火牆
systemctl stop firewalld.service #停止firewall
4、禁止Firewall下次啟動
systemctl disable firewalld.service #禁止firewall開機啟動
其他命令
啟動一個服務:systemctl start firewalld.service 關閉一個服務:systemctl stop firewalld.service 重啟一個服務:systemctl restart firewalld.service 顯示一個服務的狀態:systemctl status firewalld.service 在開機時啟用一個服務:systemctl enable firewalld.service 在開機時禁用一個服務:systemctl disable firewalld.service 查看服務是否開機啟動:systemctl is-enabled firewalld.service;echo $? 查看已啟動的服務列表:systemctl list-unit-files|grep enabled
Centos 7 firewall 命令:
查看已經開放的端口:
firewall-cmd --list-ports
開啟端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含義:
–zone #作用域
–add-port=80/tcp #添加端口,格式為:端口/通訊協議
–permanent #永久生效,沒有此參數重啟后失效
重啟防火牆
firewall-cmd --reload #重啟firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動 firewall-cmd --state #查看默認防火牆狀態(關閉后顯示notrunning,開啟后顯示running)
下面說下CentOS7和6的默認防火牆的區別
CentOS 7默認使用的是firewall作為防火牆,使用iptables必須重新設置一下
1、直接關閉防火牆
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
2、設置 iptables service
yum -y install iptables-services
如果要修改防火牆配置,如增加防火牆端口3306
vi /etc/sysconfig/iptables
增加規則
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
保存退出后
systemctl restart iptables.service #重啟防火牆使配置生效
systemctl enable iptables.service #設置防火牆開機啟動
最后重啟系統使設置生效即可。
systemctl start iptables.service #打開防火牆
systemctl stop iptables.service #關閉防火牆 解決主機不能訪問虛擬機CentOS中的站點 前陣子在虛擬機上裝好了CentOS6.2,並配好了apache+php+mysql,但是本機就是無法訪問。一直就沒去折騰了。
具體情況如下
1. 本機能ping通虛擬機 2. 虛擬機也能ping通本機 3.虛擬機能訪問自己的web 4.本機無法訪問虛擬機的web
后來發現是防火牆將80端口屏蔽了的緣故。
檢查是不是服務器的80端口被防火牆堵了,可以通過命令:telnet server_ip 80 來測試。
解決方法如下:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然后保存:
/etc/rc.d/init.d/iptables save
重啟防火牆
/etc/init.d/iptables restart
CentOS防火牆的關閉,關閉其服務即可:
查看CentOS防火牆信息:/etc/init.d/iptables status
關閉CentOS防火牆服務:/etc/init.d/iptables stop
ps:記得給服務器安全組開放端口