在 Debian + LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境中,“保护 Debian”通常指 系统安全加固 + Web 服务安全 + 防入侵。下面按 由系统到应用 的顺序,给你一套实操性很强的安全方案。
apt update && apt upgrade -y
apt autoremove -y
✅ 开启自动安全更新(推荐)
apt install unattended-upgrades
dpkg-reconfigure unattended-upgrades
编辑:
nano /etc/ssh/sshd_config
修改:
PermitRootLogin no
PasswordAuthentication no
✅ 使用 SSH 密钥登录
systemctl restart sshd
apt install ufw
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
查看状态:
ufw status
apt install fail2ban
systemctl enable fail2ban
常用保护:
server_tokens off;
client_max_body_size 10m;
location ~ /\. {
deny all;
}
location ~ /(wp-config\.php|\.env|\.git) {
deny all;
}
if ($http_user_agent ~* (nikto|sqlmap|scan|curl|python)) {
return 403;
}
编辑:
nano /etc/php/8.x/fpm/php.ini
推荐设置:
expose_php = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
allow_url_fopen = Off
display_errors = Off
重启 PHP-FPM:
systemctl restart php8.x-fpm
mysql_secure_installation
✅ 设置 root 密码
✅ 删除 test 库
✅ 禁止远程 root 登录
bind-address = 127.0.0.1
/pma-hiddenallow 1.2.3.4;
deny all;
define('DISALLOW_FILE_EDIT', true);
location = /xmlrpc.php {
deny all;
}
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
✅ 上传目录可写但不可执行:
chmod 755 uploads
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
netdataprometheus + grafanapsacct(查看历史命令)tar -czf /backup/site-$(date +%F).tar.gz /var/www
mysqldump -u root -p --all-databases > /backup/db.sql
✅ 异地备份(rsync / 对象存储)
✅ 不装多余软件
✅ 不用 root 运行服务
✅ 不用默认端口
✅ 不暴露调试信息
| 场景 | 建议 |
|---|---|
| 个人博客 | 基础加固 |
| 企业官网 | 全项加固 |
| 金融/API | WAF + 审计 |
如果你愿意,我可以:
✅ 给你 LNMP 一键安全脚本
✅ 针对 WordPress / Laravel / 自建 API 做专项加固
✅ 帮你 检查当前服务器安全状态
只要告诉我你的 Debian 版本 + 用途 即可。