Ubuntu LAMP 缓存优化策略
一 分层缓存总体思路
二 Apache 层缓存配置
sudo a2enmod cache cache_disk expires deflate<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
<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_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreHeaders Set-Cookie
CacheMaxExpire 86400
CacheDefaultExpire 3600
</IfModule>
</IfModule>
apache2ctl -M | egrep 'cache|disk_cache|expires|deflate'sudo systemctl reload apache2三 PHP 层缓存配置
[opcache]
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-apcuextension=apcu.sosudo apt-get install php-memcached memcached,配置 -m 与 -l 后重启服务。sudo apt-get install php-redis,在应用/框架中配置连接 127.0.0.1:6379。四 MySQL MariaDB 层缓存优化
innodb_buffer_pool_size=1G(在 my.cnf 的 [mysqld] 段)。query_cache_type=1、query_cache_size=64M(注意高并发写入时可能带来锁争用)。key_buffer_size=256M(示例值),并合理调整会话级缓冲区,避免过大导致内存浪费。innodb_log_file_size、innodb_flush_log_at_trx_commit 等参数结合持久性与吞吐目标权衡。五 边缘层与运维实践
memory_limit=48M)、适度设置 OPcache/APCu 的共享内存,必要时考虑 Varnish 前置与对象缓存。