Apache’s caching is a great feature to improve website speed as well as reduce server load, thereby improve website performance. You can easily setup Apache content caching with a few lines. Here’s how to configure Apache Cache in Ubuntu
How to Configure Apache Cache in Ubuntu
Here are the steps to enable Apache cache in Ubuntu. After you configure Apache cache, you may want to use a reporting software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well and spot issues quickly.
1. Install Apache Cache
Open terminal and run the following commands to install Apache modules
sudo apt-get update sudo apt-get install apache2-utils
2. Enable Apache cache modules
sudo a2enmod cache sudo a2enmod cache_disk sudo a2enmod expires sudo a2enmod headers
Bonus read: How To Enable mod_ssl in Apache
3. Restart Apache Web Server
sudo systemctl restart apache2
4. Update Virtual Hosts Configuration
There are many ways to configure Apache cache. However, it is advisable to enable Apache cache on a domain-by-domain basis. For this, you can open your domain (e.g example.com) virtual host configuration file
sudo vi /etc/apache2/sites-available/example.com.conf
If you want to do it across your entire Apache web server, or you don’t use virtual hosts, then you can update Apache server configuration file.
sudo vi /etc/apache2/sites-available/000-default.conf
Scroll to the bottom of the file and add the following lines just before line </VirtualHost>
CacheQuickHandler off CacheLock on CacheLockPath /tmp/mod_cache-lock CacheLockMaxAge 5 CacheIgnoreHeaders Set-Cookie CacheEnable disk / CacheHeader on CacheDefaultExpire 800 CacheMaxExpire 64000 CacheIgnoreNoLastMod On ExpiresActive on ExpiresDefault A300
Save and Exit File. There are 2 ways to configure Apache cache. If you set the 1st line above, that is,
then Cache will be located behind the server. In this case, each request will reach Apache server and only then hit the cache. This is useful in case you want to do some kind of authentication before sending response.
If you set,
then Cache will be placed before the server. In this case, the request will first hit the cache. If there is no/expired cache entry, then the request will be sent to server.
Bonus Read: How to Redirect 404 to URL using .htaccess
5. Test Apache Configuration
Test Apache configuration for errors.
sudo apachectl configtest
6. Restart Apache Web Server
If there are no errors, restart Apache web server to enable cache.
sudo systemctl restart apache2
7. Test Apache Cache
You can test Apache cache by viewing the contents of directory where cache is being stored.
ls -la /var/cache/apache2/mod_cache_disk/
Initially, you will see very few entries in Apache cache location. As your website visitors browse your website, this directory will grow over time. If you want to clear Apache cache, simply delete the contents of the above folder.
If Apache cache doesn’t work properly for you, then you can always disable Apache cache.
By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.