Caching is an essential technique to improve website performance and reduce server load. Below is a step-by-step guide on how to set up caching on an Apache server using mod_cache and mod_cache_disk.
Make sure you have the following:
bash sudo a2enmod cache sudo a2enmod cache_disk sudo systemctl restart apache2
For CentOS/RHEL: Edit the configuration file directly, as mod_rewrite is usually enabled by default:sudo nano /etc/httpd/conf/httpd.conf
3. Load the Modules: Ensure that the following lines are included in your Apache configuration file (usually located at /etc/httpd/conf/httpd.conf
or /etc/apache2/apache2.conf
):
LoadModule cache_module modules/mod_cache.so LoadModule cache_disk_module modules/mod_cache_disk.so
4. Restart Apache: After enabling the modules, restart the Apache service to apply the changes.
sudo systemctl restart apache2 # For Ubuntu/Debian sudo systemctl restart httpd # For CentOS/RHEL
/etc/apache2/sites-available/000-default.conf
or /etc/httpd/conf/httpd.conf
.sudo nano /etc/apache2/sites-available/000-default.conf # For Ubuntu/Debian sudo nano /etc/httpd/conf/httpd.conf # For CentOS/RHEL
2. Add Cache Configuration: Inside the <VirtualHost>
block, add the following directives:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html # Enable cache CacheQuickHandler off CacheEnable disk / CacheRoot /var/cache/apache2/mod_cache # Adjust the path as needed # Define cache settings CacheDirLevels 2 CacheDirLength 1 CacheDefaultExpire 3600 # Default expiration time (in seconds) CacheMaxExpire 86400 # Maximum expiration time (in seconds) CacheLastModifiedFactor 0.1 # Factor for last modified time <Location /> CacheIgnoreNoLastMod On # Ignore lack of Last-Modified header CacheIgnoreCacheControl On # Ignore Cache-Control response header CacheStoreNoStore On # Prevent caching if Cache-Control: no-store is present </Location> </VirtualHost>
3. Set Review Permissions: Ensure that the web server has permission to read and write to the cache directory.
sudo mkdir -p /var/cache/apache2/mod_cache sudo chown -R www-data:www-data /var/cache/apache2/mod_cache # For Ubuntu/Debian sudo chown -R apache:apache /var/cache/apache2/mod_cache # For CentOS/RHEL
To enable cache for specific files or directories, use the <Location>
directive. For example, to cache all .html
files and images:
<LocationMatch "\.(html|jpg|jpeg|png|gif)$"> CacheEnable disk CacheDefaultExpire 3600 CacheMaxExpire 86400 </LocationMatch>
sudo systemctl restart apache2 # For Ubuntu/Debian sudo systemctl restart httpd # For CentOS/RHEL
2. Check Cache Status: You can observe the cache working using tools like curl
or browser developer tools. Check HTTP headers in the response like X-Cache
.
You can use curl
as follows:
curl -I http://yourwebsite.com
Look for headers that indicate caching status, such as X-Cache: HIT
or X-Cache: MISS
.
acheStatusFile /var/log/apache2/cache-status.log
2. Remember to check the logs periodically:
tail -f /var/log/apache2/cache-status.log
If you need to clear the cache, you can simply remove the contents of the cache directory:
sudo rm -rf /var/cache/apache2/mod_cache/*
You’ve successfully set up caching on your Apache server! Proper caching can significantly improve load times and reduce server load. Remember to monitor performance and regularly tweak your caching settings to suit your needs best. For production environments, always ensure you have backups and understand any implications caching might have on dynamic content.
WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…
Comprehensive guide on creating a simple website analytics system using PHP, HTML, CSS, JavaScript, and…