如何在Ubuntu上配置Apache2防盗篡改
在Ubuntu上配置Apache2防盗篡改,需通过模块防护、权限控制、加密传输、日志监控等多维度措施,构建多层次安全防线。以下是具体步骤:
首先启用mod_rewrite
(URL重写,用于拦截非法请求)、mod_headers
(HTTP头设置,增强安全性)、mod_security
(Web应用防火墙,拦截恶意攻击)等模块:
sudo a2enmod rewrite headers security2
sudo systemctl restart apache2
编辑网站虚拟主机配置文件(如/etc/apache2/sites-available/yourdomain.conf
),添加以下规则:
<IfModule mod_rewrite.c>
RewriteEngine On
# 防止目录遍历攻击(如../)
RewriteCond %{THE_REQUEST} \.\./ [NC]
RewriteRule ^ - [F,L]
# 防止直接访问敏感文件(如config.php)
RewriteRule ^(config|\.env)\b - [F,L]
# 非法字符过滤(如<script>)
RewriteCond %{THE_REQUEST} <script> [NC]
RewriteRule ^ - [F,L]
</IfModule>
保存后重启Apache:sudo systemctl restart apache2
。
安装mod_security
及核心规则集(CRS):
sudo apt install libapache2-mod-security2
sudo cp /usr/share/modsecurity-crs/*.conf /etc/apache2/conf-available/
sudo a2enconf security2-crs
编辑/etc/modsecurity/modsecurity.conf
,启用规则并调整阈值:
SecRuleEngine On
SecRequestBodyLimit 13107200 # 增大请求体限制(可选)
SecResponseBodyLimit 13107200
重启Apache生效:sudo systemctl restart apache2
。
确保网站文件及目录权限合理,避免未授权修改:
# 设置网站根目录权限(所有者:www-data,组:www-data)
sudo chown -R www-data:www-data /var/www/html
# 文件权限:644(所有者可读写,其他用户只读)
sudo find /var/www/html -type f -exec chmod 644 {} \;
# 目录权限:755(所有者可读写执行,其他用户可读执行)
sudo find /var/www/html -type d -exec chmod 755 {} \;
# 禁止敏感目录(如uploads)执行脚本
<Directory /var/www/html/uploads>
php_flag engine off
RemoveHandler .php .phtml .php3 .php4 .php5 .php7
</Directory>
保存后重启Apache:sudo systemctl restart apache2
。
使用Let’s Encrypt获取免费SSL证书,强制HTTPS访问:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
证书获取后,Apache会自动配置HTTPS虚拟主机。强制跳转HTTP到HTTPS,编辑虚拟主机配置:
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>
重启Apache:sudo systemctl restart apache2
。
对admin
、config
等敏感目录启用HTTP基本认证:
# 创建密码文件(用户名:admin)
sudo htpasswd -c /etc/apache2/.htpasswd admin
# 编辑虚拟主机配置
<Directory /var/www/html/admin>
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
重启Apache:sudo systemctl restart apache2
。
安装mod_evasive
:
sudo apt install libapache2-mod-evasive
编辑/etc/apache2/mods-available/mod-evasive.conf
,设置阈值:
<IfModule mod_evasive24.c>
DOSHashTableSize 3097
DOSPageCount 2 # 1秒内2次相同请求则拦截
DOSSiteCount 50 # 1秒内50次不同请求则拦截
DOSPageInterval 1 # 页面请求间隔(秒)
DOSSiteInterval 1 # 站点请求间隔(秒)
DOSBlockingPeriod 10 # 拦截时间(秒)
</IfModule>
重启Apache:sudo systemctl restart apache2
。
实时查看异常访问(如大量404、POST请求):
sudo tail -f /var/log/apache2/access.log | grep -E '404|POST'
使用fail2ban
自动封禁恶意IP:
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 编辑jail.local,启用Apache模块
[apache]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3
bantime = 3600
重启fail2ban:sudo systemctl restart fail2ban
。
定期更新系统和Apache:
sudo apt update && sudo apt upgrade -y
备份配置文件和网站数据:
sudo tar -czvf /backup/apache2_$(date +%F).tar.gz /etc/apache2
sudo tar -czvf /backup/html_$(date +%F).tar.gz /var/www/html
通过以上步骤,可有效提升Ubuntu上Apache2的安全性,防止网站内容被篡改。需根据实际环境调整规则(如敏感目录路径、IP白名单),并定期审查日志以应对新威胁。