如何在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白名單),并定期審查日志以應對新威脅。