Learn how to improve your website's per­for­mance by setting up HTTP content caching in Apache. Apache is capable of several kinds of caching, including key/value and file caching. This tutorial will focus on basic HTTP content caching, which is the most commonly used form of caching with Apache.

Re­quire­ments

VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centers
  • 24/7 premium support with a personal con­sul­tant

Apache cache states

Apache's HTTP caching system considers its content as being in one of three states:

  • Fresh: New content which can be served up without being checked.
  • Stale: Content which has expired according to the rules you set, and which needs to be checked for a fresh copy.
  • Non-Existent: Content which is missing.

When Apache receives a request, it checks the Time To Live (TTL) timestamp. If the TTL has expired, Apache will re-check that content to see if an updated version is available.

  • If an updated version is available: Apache will fetch that content and store it until the new TTL expires.
  • If the existing version has not been updated: Apache will update the TTL on the file and continue to serve it until the TTL expires.

Apache uses the mod_cache module to evaluate the TTL on content, and determine what to do when it receives a request. It uses the mod_cache_disk module to store the data on the disk.

Apache cache placement

Before con­fig­ur­ing HTTP caching, you will need to decide whether to place the cache: either in front or behind the web server.

CacheQuick­Han­dler is the setting which de­ter­mines where the cache will be placed.

Cache in front of web server

If CacheQuick­Han­dler is set to On, the cache will be checked before con­sult­ing Apache.

This is a very fast way to serve content, because requests do not have to wait for Apache to finish the request. However this is not secure. Because all of the website content is put in front of the web server, no au­then­ti­ca­tion happens.

This means that if you have content which requires users to log in or au­then­ti­cate, that content will be available to anyone who requests it.

When con­fig­ur­ing Apache HTTP caching, please note that CacheQuick­Han­dler is turned on by default. If this will cause a security issue for your site, you will need to be sure to turn it Off.

Cache behind web server

If CacheQuick­Han­dler is set to Off, the request will go through Apache before being fetched from the cache.

This method is not as fast as putting the cache in front of the web server, because requests have to be processed through Apache. However, it allows for au­then­ti­ca­tion, and is therefore best for any content where the user has to log in or au­then­ti­cate before accessing content.

For this tutorial, we will be setting CacheQuick­Han­dler to Off.

Cloud Backup powered by Acronis
Mitigate downtime with total workload pro­tec­tion
  • Automatic backup & easy recovery
  • Intuitive sched­ul­ing and man­age­ment
  • AI-based threat pro­tec­tion

Enable the Apache modules

To set up HTTP caching, first install the apache2-utils package:

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

Enable the necessary Apache modules:

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

Then restart Apache for the changes to take effect:

sudo systemctl restart apache2

Add the virtual host con­fig­u­ra­tions

The following con­fig­u­ra­tion is given as an example. It is a good starting place for a basic HTTP cache placed behind the web server, where content is set to expire 5 minutes after the last time it was accessed.

There are many con­fig­u­ra­tions you can add and change. A full list is available on the Apache website.

Apache caching is best con­fig­ured on a domain-by-domain basis. Open the Apache con­fig­u­ra­tion file for the domain with the command:

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

If you have not con­fig­ured any Apache virtual hosts, you will be editing the default con­fig­u­ra­tion file:

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

Scroll to the bottom of the file and add the following lines above the line which reads </Vir­tu­al­Host>:

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>

Save and exit the file. Then check for con­fig­u­ra­tion errors with the command:

sudo apachectl configtest

If there are no errors present in the file, restart Apache for the changes to take effect:

sudo systemctl restart apache2

Test the HTTP cache

To verify that the caching system is working, check the directory where the cache is being stored. For example, first list the contents of the cache directory:

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

If there is very little traffic to your server, and you have just set up the caching, you will probably only see a few items:

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

Browse around your website, then check the directory again:

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:45 . 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:43 b drwx------ 3 www-data www-data 4096 Feb 1 21:42 T

In the example above, the directory b was created in response to viewing one of the website's pages. This directory contains the server's cache of the page.

You can also check your site's caching using a tool such as the one available on the SEO Site Checkup website or Seomator.

$1 Domain Names – Register yours today!
  • Simple reg­is­tra­tion
  • Premium TLDs at great prices
  • 24/7 personal con­sul­tant included
  • Free privacy pro­tec­tion for eligible domains
Go to Main Menu