在CentOS系统中,Apache2通常被称为httpd。要在CentOS上设置Apache2缓存,您可以使用mod_cache和mod_cache_disk模块。以下是设置缓存的步骤:
sudo yum install mod_cache mod_cache_disk
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl status httpd
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheRoot "/var/cache/httpd"
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheMaxExpire 86400
CacheMinExpire 600
CacheLastModifiedFactor 0.5
CacheOnlyOnMissingMemcachedServer off
CacheSaveNoStore on
CacheSavePrivate off
CacheStoreNoStore on
CacheSyncInterval 600
ReadWriteTimeout 15
Timeout 60
</IfModule>
</IfModule>
这个配置将启用磁盘缓存,并将其存储在/var/cache/httpd目录中。缓存项的默认过期时间为3600秒(1小时),最大过期时间为86400秒(1天),最小过期时间为600秒(10分钟)。
sudo systemctl restart httpd
sudo yum install mod_memcached
LoadModule memcached_module modules/mod_memcached.so
<IfModule mod_memcached.c>
MemcachedServers localhost:11211
CacheEnable memcached /
CacheLockPath /var/run/mod_memcached.lock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheMaxExpire 86400
CacheMinExpire 600
CacheLastModifiedFactor 0.5
</IfModule>
这将配置Apache使用本地memcached服务器(端口11211)作为缓存后端。
sudo systemctl restart httpd
现在,您的CentOS Apache2服务器已配置好缓存。请注意,这些设置仅适用于静态内容,例如图像、CSS和JavaScript文件。动态内容(如PHP脚本)不会被缓存。