下面给你一套**面向 Ubuntu + LAMP(Linux + Apache + MySQL/MariaDB + PHP)**的实用安全加固清单,按“系统 → Web 服务 → 数据库 → PHP → 运维”分层说明,适合生产环境。
sudo apt update && sudo apt upgrade -y
sudo apt autoremove --purge
adduser deploy
usermod -aG sudo deploy
编辑 SSH 配置:
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
sudo systemctl restart ssh
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo nano /etc/apache2/conf-available/security.conf
ServerTokens Prod
ServerSignature Off
<Directory /var/www/>
Options -Indexes
</Directory>
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Content-Security-Policy "default-src 'self'"
sudo mysql_secure_installation
CREATE USER 'appuser'@'localhost' IDENTIFIED BY '强密码';
GRANT SELECT, INSERT, UPDATE ON appdb.* TO 'appuser'@'localhost';
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
bind-address = 127.0.0.1
sudo nano /etc/php/*/apache2/php.ini
expose_php = Off
display_errors = Off
log_errors = On
allow_url_include = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
<Directory /var/www/html/uploads>
php_flag engine off
</Directory>
mysqldump -u root appdb > backup.sql
tar czf site.tar.gz /var/www/html
/var/log/apache2//var/log/mysql/sudo apt install lynis -y
sudo lynis audit system
✅ 系统更新
✅ 禁用 root SSH
✅ UFW + fail2ban
✅ Apache 隐藏版本
✅ HTTPS
✅ MySQL 安全初始化
✅ PHP 关闭错误显示
如果你愿意,我可以:
你当前用的是哪种 LAMP 应用?