Ubuntu LAMP 缓存策略设置
一 总体思路与分层
二 Apache HTTP 层缓存配置
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /static/
CacheEnable disk /assets/
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheMinExpire 600
</IfModule>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresDefault "access plus 1 hour"
</IfModule>
<IfModule mod_headers.c>
Header always set Cache-Control "public, max-age=31536000, immutable" env=!NO_CACHE
Header always set Cache-Control "no-store" env=NO_CACHE
</IfModule>
三 PHP 层缓存配置
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.save_comments=1
opcache.fast_shutdown=1
[apcu]
apcu.enable=1
apcu.shm_size=64M
apcu.ttl=7200
apcu.enable_cli=0 ; 生产建议关闭 CLI,避免与 FPM 冲突
四 MySQL 层缓存与内存优化
五 进阶与注意事项