Setting Up Caching on Apache

Sharing is Caring

For the majority of websites, a great amount of the content doesn’t change from day to day or even month to month. Any time a user visits a website it will request the same resources and the web server will process the request and eventually retransmit to the client regardles of whether it’s been modified or not.

For a really busy website, as you can imagine, this can really begin to amount to a lot of really needless requests if the same people are visiting your website regularly. This isn’t really an efficient use of resources, and it’s really costly because you’ll need to keep adding more and more hardware to deal with the load. Thankfully, most of the applications that are used to host websites can handle this by the use of caching.

What is Caching?

A cache is software that is used to temporarily store data for more efficient usage. Caches can dramatically reduce the cost of web hosting and dramatically improve the user experience for web browsers or mobile apps.

By serving preprocessed content and telling the web browsers to temporarily store it, it’s possible to dramatically reduce the server. We do this by telling the “client” to only ask for the content when it’s been modified or when a certain amount of time has been asked.

What is Apache?

For those not aware, Apache is one of the most used pieces of web server software on the internet. It powers at least 40% of the websites on the internet and has been around since before 2000.

Configuring Apache

The type of caching that can be setup in Apache is going to depend on the resources you have available on your server. There’s two flavours of caching that can be done in Apache without having to really do any large changes to the system.

I recommend starting with serving from disk, it’s the cheapest option and basically everyone can do this in a few minutes.

Disk Caching

In the Apache Server Configuration you’ll need to find the following line and uncomment it by removing the # at the sart of it.

LoadModule cache_module modules/mod_cache.so

For disk caching you’ll need to also find this line and uncomment it in the Apache Server Configuration.

LoadModule disk_cache_module modules/mod_disk_cache.so

For a WordPress site, we would probably add the following lines to the Apache Server Configuration we need to also add the following lines.

CacheEnable disk /
CacheRoot /webapps/cache/app1
CacheDefaultExpire 3600
CacheDisable /wp-admin

CacheDefaultExpire is the number of seconds to cache after the request.

CacheDisable disables caching for relative paths; you wouldn’t want to do this for sensitive areas or areas that would change regular.

Setting Content Expiration

For caching to work correctly, it requires a date that the content has expired otherwise you would need to determine if it was stale every time there was a new request. Using Apache’s mod_expires module, we can set expiration dates for content in whole or individually based on the type or a matching string.

Generally, you want to set caching on content that is unlikely to change much (css, javascript files, images, videos, etc). Usually you want to set the caching to be at least “access plus 1 day” when you get started with the site.

After a little while, the site is unlikely to change much so you can probably up things like images to be a week, css to be a week, javascript to be a week, and things like favicons to be a year.

In an htaccess file or the global Apache configuration file, you would end up setting it to be something like this:

<IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 day"
        ExpiresByType image/jpg "access plus 1 wee"
        ExpiresByType image/jpeg "access plus 1 week"
        ExpiresByType image/gif "access plus 1 week"
        ExpiresByType image/png "access plus 1 week"
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType text/x-javascript "access plus 1 month"
        ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

As you can see, we can set the default expiration and we can set expiration based on the type.

Caching Without Being Able to configure Apache

In the http headers, we can also set the caching to use if we wanted to be setting the “Cache-Control” header to be something like “Cache-Control: private, max-age=3600”

In html, we can also use a meta tag if we wanted to which would look something like this:

<meta http-equiv="Cache-control" content="private,max-age:3600">

Wrapping It Up

There’s a lot of ways we can reduce our web server’s load. The easiest way to start reducing the load when there’s a lot of repeat traffic is to configure apache to do some caching of static files like images, css or javascript.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.