在 Linux 上“开启 Apache2 的防火墙”通常有两层含义,下面分别说明,你可以按需要操作。
Apache 本身不带防火墙,防火墙一般由 系统防火墙 管理(如 ufw 或 firewalld)。
# 开启 UFW
sudo ufw enable
# 允许 Apache(HTTP)
sudo ufw allow 80/tcp
# 允许 HTTPS(可选)
sudo ufw allow 443/tcp
# 查看状态
sudo ufw status
# 启动防火墙
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
如果你指的是 限制 IP、端口、访问规则,可以用 Apache 配置。
编辑 Apache 配置(如 /etc/apache2/apache2.conf 或虚拟主机):
<Directory /var/www/html>
Require ip 192.168.1.100
</Directory>
<Directory /var/www/html>
Require not ip 10.0.0.5
Require all granted
</Directory>
修改后重启 Apache:
sudo systemctl restart apache2
如果需要 WAF(Web 应用防火墙):
sudo apt install libapache2-mod-security2
sudo a2enmod security2
sudo systemctl restart apache2
| 需求 | 方法 |
|---|---|
| 开放 80/443 端口 | UFW / firewalld |
| 限制访问来源 | Apache 访问控制 |
| 防攻击 | mod_security |
如果你能告诉我:
我可以给你更精确的操作命令。