溫馨提示×

Debian LAMP怎樣配置防火墻

小樊
73
2025-03-26 22:26:11
欄目: 智能運維

在Debian系統上配置LAMP環境的同時,通常需要配置防火墻來保護服務器的安全。以下是使用iptables和ufw兩種防火墻配置方法的詳細步驟。

使用iptables配置防火墻

  1. 安裝iptables(如果尚未安裝):
sudo apt-get update
sudo apt-get install iptables
  1. 配置防火墻規則: 編輯 /etc/iptables.default.rules 文件,添加以下內容:
*filter
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and MySQL connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 3306 -j ACCEPT
# Allows SSH connections for script kiddies
-A INPUT -p tcp --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
  1. 保存規則
sudo iptables-save /etc/iptables.default.rules
  1. 啟用防火墻
sudo systemctl enable iptables
sudo systemctl start iptables
  1. 使防火墻規則在系統啟動時自動加載: 創建 /etc/network/if-pre-up.d/iptables 文件并添加以下內容:
#!/bin/bash
/sbin/iptables-restore < /etc/iptables.default.rules

賦予執行權限:

sudo chmod +x /etc/network/if-pre-up.d/iptables

使用ufw配置防火墻

  1. 安裝ufw(如果尚未安裝):
sudo apt-get update
sudo apt-get install ufw
  1. 啟用ufw
sudo ufw enable
  1. 允許必要的端口
sudo ufw allow 22/tcp  # 允許SSH
sudo ufw allow 80/tcp  # 允許HTTP
sudo ufw allow 443/tcp # 允許HTTPS
sudo ufw allow 3306/tcp # 允許MySQL
  1. 設置默認策略(可選):
sudo ufw default allow outgoing
sudo ufw default deny incoming
  1. 檢查ufw狀態
sudo ufw status verbose

以上步驟展示了如何在Debian LAMP環境中配置iptables和ufw防火墻。根據您的需求選擇使用iptables或ufw,并確保在開放端口的同時考慮服務器的安全性。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女