Debian LAMP 缓存机制优化路线图
一 分层缓存总体策略
二 Apache 层优化
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "public, max-age=86400"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
</IfModule>
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
三 PHP 层优化
sudo apt-get install php-opcache
# 编辑 /etc/php/8.x/apache2/php.ini 或相应 FPM ini
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
sudo apt-get install php-apcu
# php.ini
extension=apcu.so
apcu.enable=1
apcu.shm_size=32M
apcu.ttl=7200
sudo apt-get install redis-server php-redis # 或 php-memcached
sudo systemctl restart apache2
四 MySQL 层优化
五 前端与反向代理及验证
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
if (req.http.Cookie ~ "PHPSESSID" || req.http.Authorization) {
return (pass);
}
return (hash);
}
sub vcl_backend_response {
set beresp.ttl = 1h;
return (deliver);
}
sudo apachectl configtest && sudo systemctl reload apache2;使用浏览器开发者工具查看响应头(如 Cache‑Control、Expires、Age、X‑Proxy‑Cache),并通过日志与监控观察命中率与延迟变化。