发布于2026-07-22 阅读(0)
扫一扫,手机访问
如果你正在运行Apache2服务器,安全配置这件事,绝对不是“装好了就能放着不管”的。从系统更新到模块精简,从权限控制到日志审计,每一步都可能在关键时刻挡住一次攻击。下面这几个环节,是经过大量实战验证后最值得优先关注的。

系统和Apache2本身以及相关软件包,必须保持最新状态。很多漏洞其实都有对应的补丁,但如果你不更新,相当于给攻击者留了一扇窗户。操作很简单:
sudo apt update && sudo apt upgrade -ysudo yum update -y防火墙是服务器的第一道防线。对于Apache来说,只需要开放HTTP(80)和HTTPS(443)两个端口就够了。其他端口一律关闭,没什么好商量的。
sudo apt install ufw -y
sudo ufw allow 'Apache Full' # 允许HTTP和HTTPS
sudo ufw enablesudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload你装了多少模块,攻击面就有多大。像autoindex、cgi、status这类模块,如果业务上不需要,直接禁用掉。禁用后记得重启Apache生效。
sudo a2dismod autoindex cgi status # Ubuntu/Debian
sudo systemctl restart apache2
注:CentOS需要编辑
/etc/httpd/conf/httpd.conf,手动注释或删除对应的LoadModule行。
不要轻易让外界知道你的Apache版本和操作系统类型,这能降低针对性攻击的风险。在Apache主配置文件(/etc/apache2/apache2.conf或/etc/httpd/conf/httpd.conf)里加上这两行:
ServerTokens Prod # 只显示“Apache”,不暴露版本号
ServerSignature Off # 错误页面也不显示服务器信息
192.168.1.100访问/admin目录:
Order Deny,Allow
Deny from all
Allow from 192.168.1.100
Options -Indexes即可:
Options -Indexes
AllowOverride None
Require all granted
HTTP明文传输就像在大街上喊话,谁都能听。强迫所有流量走HTTPS,这是保护数据安全的基本操作。
sudo a2enmod ssl(Ubuntu/Debian)sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com/etc/apache2/sites-a vailable/default-ssl.conf)时,确保证书路径正确:
ServerName yourdomain.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
DetectionOnly改为On:sudo apt install libapache2-mod-security2 -y
sudo a2enmod security2
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf
sudo systemctl restart apache2/etc/apache2/mods-enabled/evasive.conf,添加以下规则:
DOSHashTableSize 3097
DOSPageCount 2 # 1秒内2次相同请求视为攻击
DOSSiteCount 50 # 1秒内50次请求视为攻击
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10 # 攻击时封锁10秒
网站目录的权限设置不当,很容易被上传恶意文件或读取敏感数据。标准做法是:目录归Apache用户所有,目录权限755,文件权限644。
sudo chown -R www-data:www-data /var/www/html # Ubuntu/Debian(Apache用户为www-data)
sudo chmod -R 755 /var/www/html
注:CentOS的Apache用户是
apache,记得替换。
日志是事后分析攻击行为的重要依据。生产环境建议将日志级别设为warn或error,同时启用Combined格式的访问日志,方便后续分析。
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
定期查看错误日志,可以及时发现异常:
tail -f /var/log/apache2/error.log
Options -Indexes实现了。
LimitRequestBody 52428800
sudo tar -czvf /backup/apache2-backup.tar.gz /etc/apache2 /var/www/html
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8