在Ubuntu中,您可以使用Apache的模塊mod_cache和mod_cache_disk來配置緩存。以下是配置緩存的步驟:
sudo apt-get update
sudo apt-get install apache2
mod_cache和mod_cache_disk模塊:sudo a2enmod cache
sudo a2enmod cache_disk
sudo a2enmod headers
這將啟用緩存模塊以及用于存儲緩存文件的mod_cache_disk模塊。
/etc/apache2/sites-available/000-default.conf),在<VirtualHost>部分中添加以下內容:<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /your-cache-path
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
</IfModule>
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "max-age=3600, public"
</IfModule>
將/your-cache-path替換為您希望存儲緩存文件的目錄。您可以根據需要調整CacheDefaultExpire(以秒為單位)以設置緩存過期時間。
sudo systemctl reload apache2
現在,Apache2將為您的網站啟用緩存。請注意,這些設置僅適用于靜態內容,例如圖像、CSS和JavaScript文件。動態內容(如PHP腳本)不會被緩存。如果您需要緩存動態內容,可以考慮使用其他技術,如OPcache或Varnish。