How to Configure Apache Cache in Ubuntu


configure apache cache

Apache Web Server allows you to speed up your website and reduce server load by caching content. There are 3 types of content caching available in Apache – file caching, key-value caching and HTTP caching. Let us look at how to configure Apache cache in Ubuntu. We’ll be looking at HTTP caching in Apache.

 

How to Configure Apache Cache in Ubuntu

Apache classifies content into 3 states:

  1. Fresh: New content that can be served directly.
  2. Stale: Content that has been cached but expired. So it is checked for a fresh copy before being served.
  3. Non-Existent: Content that does not exist

When Apache gets a request, it will check the timestamp. If it has expired, then Apache will look for an updated version of it. If it is available, Apache will update the cache and store it till its timestamp expires. If the existing version has not been updated, then Apache will update the content and serve the fresh content until it expires.

It uses mod_cache module to manage cache content.

 

Cache Location

You can place the cache either before or behind your Apache Server using the setting CacheQuickHandler

If CacheQuickHandler is On, then cache is checked before the request is sent to Apache. It is fast as client doesn’t have to wait for Apache to process the request. However, it is not secure because it not secure as no authentication is required to access this kind of cached content. So even the pages that require log in will be easily accessible to anyone. So it is suitable for caching content that doesn’t require authentication. E.g documentation, images, tutorials, videos, etc.

If CacheQuickHandler is Off then request is sent to Apache server before it is fetched from the cache. Although it is slower than the previous method, it is more secure as it forces authentication wherever necessary.

For our tutorial, we will keep CacheQuickHandler as Off

 

Here are the steps to configure Apache Cache in Ubuntu. We will

1. Install Apache module

We will install the required Apache modules for HTTP caching.


$ sudo apt-get update
$ sudo apt-get install apache2-utils

 

Enable the required Apache modules:


$ sudo a2enmod cache
$ sudo a2enmod cache_disk
$ sudo a2enmod expires
$ sudo a2enmod headers

 

Restart Apache to apply changes


$ sudo systemctl restart apache2

 

2. Add Virtual Host Configurations

Open the Apache config file for your domain (example.com) in a text editor


$ sudo nano /etc/apache2/sites-available/example.com.conf

If you have not configured any virtual hosts, you can open the default Apache config file


$ sudo nano /etc/apache2/sites-available/000-default.conf

 

Scroll to the <VirtualHost> block and add the following lines:


CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheLockMaxAge 5
CacheIgnoreHeaders Set-Cookie

<Location />
 CacheEnable disk
 CacheHeader on
 CacheDefaultExpire 800
 CacheMaxExpire 64000
 CacheIgnoreNoLastMod On
 ExpiresActive on
 ExpiresDefault A300
</Location>

 

In the above lines, we set cache expiry to 5 minutes. Save and close the file

Test the Apache config file


$ sudo apachectl configtest

If there are no errors, restart Apache to apply changes. In case of errors, open the config file and look for typos/syntax errors.


$ sudo systemctl restart apache2

 

3. Test the Cache

You can test the cache by listing the contents of your cache directory, where all the cached content is stored


$ ls -la /var/cache/apache2/mod_cache_disk/

 

Initially, you will only see a few items. It will grow as your site gets more traffic, and more items get cached


user@localhost:/var/www/html# ls -la /var/cache/apache2/mod_cache_disk/
total 20
drwxr-xr-x 5 www-data www-data 4096 Feb 1 21:43 .
drwxr-xr-x 3 root root 4096 Jan 6 03:20 ..
drwx------ 3 www-data www-data 4096 Feb 1 21:42 2
drwx------ 3 www-data www-data 4096 Feb 1 21:42 T

 

That’s It! You have configured Apache cache for your website.

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!