在 Debian 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境后,防黑客攻击的核心思路是:最小暴露、最小权限、持续更新、日志监控。下面按“系统层 → 服务层 → 应用层 → 运维层”给你一套实用方案。
apt update
apt upgrade -y
并开启自动安全更新:
apt install unattended-upgrades
dpkg-reconfigure unattended-upgrades
编辑:
nano /etc/ssh/sshd_config
确保:
PermitRootLogin no
PasswordAuthentication no # 推荐只用密钥
重启 SSH:
systemctl restart ssh
apt install ufw
ufw default deny incoming
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
apt install fail2ban
默认已保护 SSH,可额外保护 Apache。
nano /etc/apache2/conf-enabled/security.conf
设置:
ServerTokens Prod
ServerSignature Off
<Directory /var/www/>
Options -Indexes
</Directory>
<FilesMatch "\.(env|sql|ini|log)$">
Require all denied
</FilesMatch>
apt install certbot python3-certbot-apache
certbot --apache
mysql_secure_installation
nano /etc/mysql/mariadb.conf.d/50-server.cnf
bind-address = 127.0.0.1
不要用一个账号管所有库,按应用分用户、最小权限。
nano /etc/php/*/apache2/php.ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
expose_php = Off
allow_url_include = Off
<Directory /var/www/html/uploads>
php_flag engine off
</Directory>
chown -R www-data:www-data /var/www/html
find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;
apt install libapache2-mod-security2
a2enmod security2
启用 OWASP 规则集。
/var/log/apache2//var/log/fail2ban.logrsync /var/www/ backup/
mysqldump -u root -p --all-databases > db.sql
rkhunterchkrootkitossec / wazuhDebian LAMP 防黑客 = 系统更新 + 防火墙 + SSH 密钥 + Apache/PHP 加固 + 数据库隔离 + 应用更新 + 日志监控
如果你愿意,我可以: