在LNMP(Linux, Nginx, MySQL, PHP)环境中设置防火墙可以保护你的服务器免受未经授权的访问。以下是几种常见的方法:
sudo iptables -L
命令查看当前iptables规则。sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # HTTPS
sudo iptables-save > /etc/iptables/rules.v4
保存规则(路径可能因系统而异)。sudo systemctl enable iptables
和 sudo systemctl start iptables
命令启用并启动iptables服务。重启iptables服务使配置生效:sudo systemctl restart iptables
。sudo systemctl status firewalld
命令查看firewalld的状态。sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --reload
命令重新加载firewalld配置。sudo apt update
sudo apt install ufw
sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow http
sudo ufw allow https
sudo ufw allow mysql
sudo ufw allow 9000/tcp
sudo ufw status
命令查看ufw的状态和规则。无论选择哪种方法,都需要根据你的实际情况进行调整。在修改防火墙规则后,务必测试你的应用是否能够正常访问。不正确的防火墙配置可能会导致你的服务器无法访问。强烈建议在生产环境中进行操作前,先在测试环境中进行测试。