在Debian系統上配置LAMP環境的同時,通常需要配置防火墻來保護服務器的安全。以下是使用iptables和ufw兩種防火墻配置方法的詳細步驟。
sudo apt-get update
sudo apt-get install iptables
/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
sudo iptables-save /etc/iptables.default.rules
sudo systemctl enable iptables
sudo systemctl start iptables
/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
sudo apt-get update
sudo apt-get install ufw
sudo ufw enable
sudo ufw allow 22/tcp # 允許SSH
sudo ufw allow 80/tcp # 允許HTTP
sudo ufw allow 443/tcp # 允許HTTPS
sudo ufw allow 3306/tcp # 允許MySQL
sudo ufw default allow outgoing
sudo ufw default deny incoming
sudo ufw status verbose
以上步驟展示了如何在Debian LAMP環境中配置iptables和ufw防火墻。根據您的需求選擇使用iptables或ufw,并確保在開放端口的同時考慮服務器的安全性。