Ubuntu LAMP 缓存机制全景与落地配置
一、缓存分层与适用场景
二、Apache 层缓存配置
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 2 months"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /static/
CacheRoot /var/cache/apache2/mod_cache_disk
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
</IfModule>
</IfModule>
三、PHP 层缓存配置
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0 ; 生产建议关闭,用部署流程触发刷新
opcache.revalidate_freq=0 ; 与上行配合,文件变更由部署脚本重置
opcache.fast_shutdown=1
[apcu]
extension=apcu.so
apcu.enable_cli=0
apcu.shm_size=64M
apcu.ttl=7200
# Memcached
sudo apt install memcached php-memcached
# Redis
sudo apt install redis-server php-redis
sudo systemctl restart php**7.x**-fpm
四、MySQL 层缓存与优化
五、部署与验证要点