温馨提示×

怎样开启apache2防火墙

小樊
47
2026-10-11 01:26:35
栏目: 编程语言

在 Linux 上“开启 Apache2 的防火墙”通常有两层含义,下面分别说明,你可以按需要操作。


一、系统层防火墙(推荐,最常用)

Apache 本身不带防火墙,防火墙一般由 系统防火墙 管理(如 ufw 或 firewalld)。

1️⃣ 使用 UFW(Ubuntu / Debian 常见)

# 开启 UFW
sudo ufw enable

# 允许 Apache(HTTP)
sudo ufw allow 80/tcp

# 允许 HTTPS(可选)
sudo ufw allow 443/tcp

# 查看状态
sudo ufw status

2️⃣ 使用 firewalld(CentOS / Rocky / Alma)

# 启动防火墙
sudo systemctl start firewalld
sudo systemctl enable firewalld

# 开放 Web 服务
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# 重新加载
sudo firewall-cmd --reload

二、Apache 自身的“访问控制”(类似防火墙)

如果你指的是 限制 IP、端口、访问规则,可以用 Apache 配置。

示例:只允许指定 IP 访问

编辑 Apache 配置(如 /etc/apache2/apache2.conf 或虚拟主机):

<Directory /var/www/html>
    Require ip 192.168.1.100
</Directory>

禁止某个 IP

<Directory /var/www/html>
    Require not ip 10.0.0.5
    Require all granted
</Directory>

修改后重启 Apache:

sudo systemctl restart apache2

三、配合 mod_security(真正的 Web 防火墙)

如果需要 WAF(Web 应用防火墙):

sudo apt install libapache2-mod-security2
sudo a2enmod security2
sudo systemctl restart apache2

总结

需求 方法
开放 80/443 端口 UFW / firewalld
限制访问来源 Apache 访问控制
防攻击 mod_security

如果你能告诉我:

  • 系统版本(Ubuntu / CentOS?)
  • 想防什么(端口 / IP / 攻击)

我可以给你更精确的操作命令。

0 踩