在 Ubuntu 上使用 Apache2 时,“防入侵”通常不是靠单一配置,而是系统 + Web 服务 + 应用层的综合防护。下面按优先级给你一套实用方案。
sudo apt update
sudo apt upgrade -y
并开启自动安全更新:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw default deny incoming
编辑:
sudo nano /etc/ssh/sshd_config
确保:
PermitRootLogin no
PasswordAuthentication no
然后:
sudo systemctl restart ssh
sudo nano /etc/apache2/conf-enabled/security.conf
修改:
ServerTokens Prod
ServerSignature Off
重启:
sudo systemctl restart apache2
常见可禁用:
sudo a2dismod autoindex status cgi
sudo systemctl restart apache2
示例(网站根目录):
<Directory /var/www/html>
Options -Indexes
AllowOverride None
Require all granted
</Directory>
sudo apt install fail2ban
Apache 防护示例:
[apache-auth]
enabled = true
[apache-badbots]
enabled = true
sudo apt install libapache2-mod-evasive
配置:
sudo nano /etc/apache2/mods-enabled/evasive.conf
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Content-Security-Policy "default-src 'self'"
Apache 本身不能防 Web 漏洞
<Directory /var/www/html/uploads>
php_flag engine off
</Directory>
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
sudo journalctl -u apache2
可安装:
sudo apt install lynis
sudo lynis audit system
www-datasudo chown -R deploy:www-data /var/www/html
sudo chmod -R 750 /var/www/html
Ubuntu + Apache 防入侵 = 系统更新 + 防火墙 + fail2ban + HTTPS + 应用安全 + 日志监控
如果你愿意,可以告诉我:
我可以给你更针对性的配置方案。