Debian Apache 响应速度优化清单
一 基础与连接优化
sudo a2enmod deflate
# /etc/apache2/mods-enabled/deflate.conf
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
# /etc/apache2/apache2.conf 或 ports.conf 的 <VirtualHost> 内
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
sudo a2enmod http2
# /etc/apache2/sites-enabled/000-default.conf 的 <VirtualHost *:443> 内
Protocols h2 http/1.1
sudo a2dismod autoindex
# CustomLog ${APACHE_LOG_DIR}/access.log common
LogLevel warn
以上措施能直接减少传输量、连接开销与无效负载,是最快见效的优化项。
二 MPM 并发与进程模型
sudo a2enmod mpm_event
# /etc/apache2/mods-enabled/mpm_event.conf
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork
# /etc/apache2/mods-enabled/mpm_prefork.conf
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
MPM 决定了并发模型与资源占用,是吞吐与稳定性的核心杠杆。
三 缓存与静态资源加速
sudo a2enmod cache_disk expires
# /etc/apache2/conf-available/cache_disk.conf
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
# /etc/apache2/mods-enabled/expires.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
sudo apt install brotli
sudo a2enmod brotli
# /etc/apache2/mods-enabled/brotli.conf
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
四 启用 TLS 与网络层优化
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
# /etc/apache2/sites-enabled/000-default.conf 的 <VirtualHost *:443> 内
SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"
SSLSessionCacheTimeout 300
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com
net.ipv4.tcp_tw_reuse = 1
net.core.somaxconn = 4096
TLS 与网络栈优化能显著降低首包时间并提升高并发下的稳定性。
五 应用层与数据库优化
# /etc/php/*/apache2/php.ini
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
[mysqld]
innodb_buffer_pool_size = 1G