sudo apt update && sudo apt upgrade
(Ubuntu/Debian) 或 sudo yum update
(CentOS/RHEL)bash
sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
bash
sudo ufw enable
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
bash
sudo adduser webadmin
sudo usermod -aG sudo webadmin
bash
sudo chown -R webadmin:www-data /var/www
sudo chmod -R 750 /var/www
sudo find /var/www -type d -exec chmod 755 {} \;
sudo find /var/www -type f -exec chmod 644 {} \;
apache
Options -Indexes
apache
ServerTokens Prod
ServerSignature Off
apache
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>
禁用不必要的模块:
sudo a2dismod autoindex cgi status
sudo systemctl restart apache2
运行安全安装脚本:
sudo mysql_secure_installation
sql
CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
sql
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
FLUSH PRIVILEGES;
expose_php = Off
display_errors = Off
log_errors = On
allow_url_fopen = Off
allow_url_include = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
bash
sudo apt purge php5.* # 移除PHP5
sudo chown -R webadmin:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 750 {} \;
sudo find /var/www/html -type f -exec chmod 640 {} \;
bash
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/mysql/error.log
安装和配置fail2ban:
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
bash
mysqldump -u root -p --all-databases > all-databases-backup.sql
bash
tar -czvf /backup/website-$(date +%Y%m%d).tar.gz /var/www/html /etc/apache2 /etc/mysql
bash
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
通过实施以上安全措施,可以显著提高LAMP服务器的安全性,减少被攻击的风险。