sudo apt update && sudo apt upgrade,确保内核及Apache相关组件为最新版本。/etc/sysctl.conf,增加以下参数以优化网络和文件描述符:net.core.somaxconn=65535
net.ipv4.tcp_tw_reuse=1
fs.file-max=100000
执行 sysctl -p 生效。prefork,启用 event 模块(适合高并发):sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
配置 event.conf:<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150
</IfModule>
```。
mod_cache 和 mod_cache_disk 缓存静态资源:sudo a2enmod cache cache_disk
配置缓存路径:<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
</IfModule>
```。
KeepAlive 并设置超时:KeepAlive On
KeepAliveTimeout 5
限制 MaxClients(根据内存计算,如每个进程占用10MB则设为 总内存/10)。.htaccess 提升解析效率:AllowOverride None
限制请求体大小:LimitRequestBody 1048576 # 1MB
```。
htop、top 监控CPU/内存,netstat 查看连接状态,ab(ApacheBench)进行压力测试。sudo service apache2 restart
```。
Protocols h2 http/1.1
需先安装 mod_http2 模块。注意:修改配置后需重启Apache,建议先在测试环境验证效果,避免影响生产环境稳定性。