Ubuntu LAMP 安全防护实操清单
一 系统与基础防护
sudo apt update && sudo apt upgrade -ysudo apt install unattended-upgrades && sudo dpkg-reconfigure -plow unattended-upgrades/etc/ssh/sshd_config:PermitRootLogin no、PasswordAuthentication no、AllowUsers your_usernamesudo systemctl restart sshsudo ufw allow 'Apache Full'(或分别放行 80/tcp 与 443/tcp)、sudo ufw enable二 Apache 安全加固
/etc/apache2/conf-available/security.conf 中设置:ServerTokens Prod、ServerSignature Off、TraceEnable OffOptions -Indexes<LimitExcept GET POST> Deny from all </LimitExcept>AllowOverride Nonesudo apt install libapache2-mod-security2 && sudo a2enmod security2三 MySQL MariaDB 安全加固
sudo mysql_secure_installation/etc/mysql/mysql.conf.d/mysqld.cnf 中设置:bind-address = 127.0.0.1[mysqld] 中加入:local-infile=0CREATE USER 'webapp'@'localhost' IDENTIFIED BY 'StrongPassword!'; GRANT SELECT,INSERT,UPDATE,DELETE ON mydb.* TO 'webapp'@'localhost'; FLUSH PRIVILEGES;[mysqld] 中:log_error = /var/log/mysql/error.log、slow_query_log = 1、long_query_time = 2四 PHP 安全配置
/etc/php/*/apache2/php.ini:display_errors = Off、log_errors = On、error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICTexpose_php = Offallow_url_fopen = Off、allow_url_include = Offdisable_functions = exec,shell_exec,passthru,system,popen,proc_open,pcntl_exec,show_sourceopen_basedir = /var/www/your_app:/tmp五 传输加密与备份监控
sudo a2enmod ssl、sudo a2enmod rewriteSSLEngine on、SSLCertificateFile /path/cert.crt、SSLCertificateKeyFile /path/key.key、SSLCertificateChainFile /path/chain.crtRewriteEngine On、RewriteCond %{HTTPS} !=on、RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [L,R=301]