linux

如何配置Linux LAMP服务器防火墙

小樊
42
2025-12-23 21:49:27
栏目: 云计算

配置Linux LAMP服务器的防火墙是一个重要的安全步骤,可以帮助你保护服务器免受未经授权的访问。以下是使用iptablesufw(Uncomplicated Firewall)两种常见的方法来配置防火墙的步骤。

使用 iptables

  1. 安装iptables(如果尚未安装)

    sudo apt-get update
    sudo apt-get install iptables
    
  2. 允许SSH连接

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  3. 允许HTTP连接

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    
  4. 允许HTTPS连接

    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    
  5. 允许MySQL连接(假设MySQL运行在默认端口3306)

    sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
    
  6. 允许FTP连接(如果需要)

    sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    
  7. 拒绝所有其他输入

    sudo iptables -A INPUT -j DROP
    
  8. 保存iptables规则

    sudo sh -c "iptables-save > /etc/iptables/rules.v4"
    

使用 ufw

  1. 安装ufw(如果尚未安装)

    sudo apt-get update
    sudo apt-get install ufw
    
  2. 启用ufw

    sudo ufw enable
    
  3. 允许SSH连接

    sudo ufw allow 22/tcp
    
  4. 允许HTTP连接

    sudo ufw allow 80/tcp
    
  5. 允许HTTPS连接

    sudo ufw allow 443/tcp
    
  6. 允许MySQL连接(假设MySQL运行在默认端口3306)

    sudo ufw allow 3306/tcp
    
  7. 允许FTP连接(如果需要)

    sudo ufw allow 21/tcp
    
  8. 查看ufw状态

    sudo ufw status
    

注意事项

通过以上步骤,你可以有效地配置Linux LAMP服务器的防火墙,保护你的服务器免受未经授权的访问。

0
看了该问题的人还看了