在CentOS中,您可以使用mod_cache和mod_cache_disk模块来配置Apache2缓存。以下是配置缓存的步骤:
sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
sudo yum install mod_cache mod_cache_disk
sudo systemctl restart httpd
/etc/httpd/conf/httpd.conf文件,并添加以下内容:<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/httpd"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
</IfModule>
</IfModule>
这将启用磁盘缓存,并将其存储在/var/cache/httpd目录中。您可以根据需要自定义这些设置。
httpd.conf文件中添加以下内容:<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "max-age=3600, public"
</IfModule>
这将设置不同类型的文件的过期时间。您可以根据需要调整这些设置。
sudo systemctl restart httpd
现在,您的Apache2服务器已配置为使用磁盘缓存。您可以通过访问网站的源代码并查找带有Cache-Control或Expires响应头的文件来验证缓存是否正常工作。