Apache HTTP Server is a free and open-source cross-platform web server software, developed and maintained by a community of developers under Apache Software Foundation. It is also a powerful and flexible HTTP/1.1/2 compliant web server having support for a wide range of modules, which is the reason for its widespread and worldwide usage.
But Apache HTTP Server is a process driven appliance that creates a thread upon each new request from a website visitor. This means that the web server uses up more and more resources with the increased number of website visitors. This could serve as the source of a high-resource utilization issue when the server is experiencing heavy traffic.
There’s two flavours of caching that can be configured in Apache HTTP Server:-
• Shared Object Cache
• Disk Cache
Since serving cache from the disk is the cheapest option, we will be choosing Disk Cache for our work here.
Two modules are required for implementing Disk Caching in Apache HTTP Server:-
1. mod_cache (https://httpd.apache.org/docs/2.4/mod/mod_cache.html)
mod_cache implements an RFC 2616(https://datatracker.ietf.org/doc/html/rfc2616) compliant HTTP content caching filter.
RFC 2616 compliant caching provides a mechanism to verify whether stale or expired content is still fresh, and can represent a significant performance boost when the origin server supports conditional requests by honouring the If-None-Match HTTP request header. Content is only regenerated from scratch when the content has changed, and not when the cached entry expires.
As a filter, mod_cache can be placed in front of content originating from any handler, including flat files, output of a CGI script, or dynamic content generator, or content proxied from another server.
2. mod_cache_disk (https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html)
This module implements a disk based storage manager for mod_cache.
The headers and bodies of cached responses are stored separately on disk, in a directory structure derived from the md5 hash of the cached URL. Atomic cache updates to both header and body files are achieved without the need for locking by storing the device and inode numbers of the body file within the header file. This has the side effect that cache entries manually moved into the cache will be ignored. Also, the caching of partial content is not yet supported by this module.
By default Apache HTTP Server should have the disk caching modules enabled. But first let us verify that.
# httpd -M | grep -i cache
If the disk caching modules are not present, use the following command to search for disk caching modules.
# yum search mod_cache
Once the required disk caching modules are listed, we can use the yum install command to install those modules.
Create a cache directory for Apache HTTP Server
# mkdir /var/cache/mod_cache
Check the user and group of Apache HTTP Server processes
# find /etc/httpd -iname \*.conf | xargs grep -i ‘^User \|^Group ‘
Set the correct User and Group for cache directory
# chown USERNAME:GROUPNAME /var/cache/mod_cache
NOTE: Replace USERNAME and GROUPNAME with required name of User and Group
For our website’s HTTP and HTTPS VirtualHost, add the following content
CacheEnable disk /
CacheRoot /var/cache/mod_cache
CacheDefaultExpire 3600
NOTE:
– CacheEnable: Enable caching of specified URLs using a specified storage manager
– CacheRoot: Directory root under which cache files are stored
– CacheDefaultExpire: Default duration to cache a document when no expiry date is specified
We can also disable caching for certain paths, when they are sensitive areas or areas that would change regular.
SYNTAX:
# CacheDisable DIRECTORY_PATH
NOTE:
– Replace DIRECTORY_PATH with the required directoy path (eg: /wp-admin)
– CacheDisable: Disable caching of specified URLs by using relative directory paths
Check the syntax of Apache HTTP Server configuration files
# httpd -t
Finally restart the Apache HTTP Server
# systemctl restart httpd
Done.