Apache作為全球佔有率第一的Web服務器,安全加固是運維必修課。本文梳理十三大加固維度,附具體命令與配置片段,可直接落地執行。
groupadd apache
useradd -g apache -s /sbin/nologin apache
# httpd.conf 添加:
User apache
Group apache
檢查:ps aux | grep httpd 確認非root進程。
/usr/local/apache 屬主root:root,非超級用戶不可寫chmod 600 /etc/httpd/conf/httpd.conf 配置文件僅屬主讀寫chmod 644 /var/log/httpd/*.log 日誌屬主讀寫、他人只讀LogLevel notice
ErrorLog /var/log/httpd/error_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd/access_log combined
Order Deny,Allow
Deny from all
Order Allow,Deny
Allow from all
Options FollowSymLinks # 刪除 Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 500 /custom500.html
# 其餘400/401/403/405/502/503同理
Timeout 60
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
ServerSignature Off
ServerTokens Prod
TraceEnable Off # Apache 2.0+
#LoadModule cgi_module modules/mod_cgi.so
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
Listen 192.168.1.100:80 # 僅綁內網管理IP,拒絕公網直連
rm -rf /var/www/html/* /var/www/cgi-bin/* /usr/local/apache/manual
Allow GET POST
Deny from all
httpd -t 語法檢查通過systemctl restart httpd 重啟無錯十三條落地,Apache安全基線即達標。持續關注CVE、定期打補丁,方能長治久安。