温馨提示×

centos apache怎样配置防火墙

小樊
52
2026-09-18 03:05:18
栏目: 智能运维

在 CentOS 上配置 Apache(httpd) 的防火墙,通常使用的是 firewalld(CentOS 7 及以上默认)。下面按常见场景说明。


一、确认防火墙状态

systemctl status firewalld

如果未运行:

systemctl start firewalld
systemctl enable firewalld

二、放行 Apache 默认端口(80 / 443)

1️⃣ 放行 HTTP(80)

firewall-cmd --permanent --add-service=http

2️⃣ 放行 HTTPS(443)

firewall-cmd --permanent --add-service=https

3️⃣ 重新加载防火墙

firewall-cmd --reload

三、确认 Apache 已启动

systemctl start httpd
systemctl enable httpd

测试访问:

http://服务器IP

四、如果使用了自定义端口(如 8080)

例如 Apache 监听 8080:

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

五、查看当前防火墙规则

firewall-cmd --list-all

示例输出中应包含:

services: http https

或

ports: 8080/tcp

六、CentOS 6(iptables,老系统)

如果你用的是 CentOS 6:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

七、常见问题

❌ 防火墙放行了但访问不了?

检查:

  1. Apache 是否监听正确端口
    netstat -tunlp | grep httpd
    
  2. SELinux 是否阻止
    getenforce
    
    若为 Enforcing,可临时关闭测试:
    setenforce 0
    

如果你能告诉我:

  • CentOS 版本(6 / 7 / 8 / Stream)
  • Apache 端口
  • 是否使用 HTTPS

我可以给你更精确的配置命令。

0 踩