117.info
人生若只如初见

Debian LAMP怎样配置防火墙

在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,并确保在开放端口的同时考虑服务器的安全性。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe86aAzsKCQRWDV0.html

推荐文章

  • Debian上Filezilla配置步骤

    在Debian上配置FileZilla可以分为两个部分:安装FileZilla客户端和配置FileZilla服务器。以下是详细的步骤:
    安装FileZilla客户端 更新系统: sudo apt-get...

  • debian域名是否有备案限制

    在中国大陆,所有的域名如果需要解析到国内服务器上使用,都必须进行备案。根据相关规定,无论使用哪种操作系统(包括Debian),只要是服务于中国大陆用户的网站...

  • VirtualBox中Debian如何备份

    在VirtualBox中备份Debian虚拟机主要有以下几种方法:
    使用tar命令备份整个系统 备份命令:sudo tar -czvf backup.tar.gz --exclude=/proc --exclude=/sys ...

  • 在Debian中如何挂载分区

    在Debian中挂载分区的步骤如下:
    1. 查看可用磁盘和分区
    首先,你需要知道你的系统中有哪些磁盘和分区。可以使用以下命令来查看:
    lsblk 或者

  • Ubuntu C++文件操作怎么优化

    在Ubuntu环境下进行C++文件操作时,优化可以从多个方面入手,包括选择合适的文件I/O方法、减少磁盘访问次数、合理使用内存以及并行处理等。以下是一些具体的优化...

  • debian防火墙怎么优化

    优化Debian防火墙可以通过多种方法实现,包括使用iptables、ufw(Uncomplicated Firewall)以及firewalld等工具。以下是具体的优化步骤和建议:
    使用iptabl...

  • Linux Oracle如何实现高可用

    在Linux上实现Oracle数据库的高可用性通常涉及以下几个关键步骤和技术: 使用Oracle RAC(Real Application Clusters): Oracle RAC允许多个Oracle实例在同一台...

  • Debian Tomcat日志过大怎么办

    当Debian系统上的Tomcat日志文件过大时,可以采取以下几种方法来解决这个问题:
    1. 日志分割
    使用 cronolog 工具对日志进行分割,以避免单个日志文件过...