How to set up caching in Apache web server
Caching, when properly configured, can instruct user's browsers to locally store and reuse previously fetched resources. This ensures faster page loads for returning users, as their browsers can retrieve resources from the local cache instead of making repeated requests to the server.
Most modern websites and web applications use browser caching to enhance the user experience. In Apache, this can be achieved using specific headers that direct the user's browser on what content should be cached and for how long. Caching in Apache can be implemented using different mechanisms such as mod_cache, mod_cache_disk, mod_mem_cache and mod_socache_shmcb, depending on the requirements and available resources.
Configuring caching in Apache involves understanding the nature of your web content, determining what content should be cached, and configuring the relevant caching directives. This may include setting expiration dates, cache size, and other parameters related to caching behavior.
Steps to enable and configure caching in Apache:
-
Install the necessary caching modules for Apache.
$ sudo a2enmod socache_shmcb expires Enabling module socache_shmcb. Enabling module expires. To activate the new configuration, you need to run: systemctl restart apache2
Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
-
Open the Apache configuration file using your preferred text editor.
$ sudo vi /etc/apache2/apache2.conf
Related: Location for Apache configuration
-
Add the following lines to configure the cache settings according to your needs.
<IfModule mod_cache_socache.c> CacheEnable socache / CacheSocache shmcb CacheSocacheMaxSize 512000 CacheDefaultExpire 3600 # Other directives as necessary </IfModule>
Directive Description CacheEnable This directive defines the storage type and the URL space for the cache. Example: CacheEnable socache / enables memory-based caching (using shared object cache) for all URLs. CacheSocache Defines the shared object cache provider to use for caching. In this case, shmcb indicates using a shared memory mechanism for storage. CacheSocacheMaxSize Sets the maximum amount of shared memory storage in bytes. In this example, 512000 bytes (or 0.5MB) is allocated for the cache storage. CacheDefaultExpire Determines the default expiration time in seconds for cached content. If no expiration time is specified in the HTTP headers, this value is used. Example: 3600 sets a default expiration time of one hour. -
Set up cache control headers in your htaccess or virtual host file for more granular control.
<FilesMatch ".(jpg|jpeg|png|gif|js|css)$"> ExpiresActive On ExpiresDefault "access plus 1 month" </FilesMatch>
-
Restart Apache to apply the changes.
$ sudo systemctl restart apache2 # Ubuntu and Debian $ sudo systemctl restart httpd # CentOS and Red Hat
-
Verify that caching is working by inspecting the HTTP headers of a cached resource.
$ curl -I https://www.example.com/image.png HTTP/1.1 200 OK Date: Fri, 25 Aug 2023 09:13:40 GMT Server: Apache/2.4.55 (Ubuntu) Last-Modified: Fri, 25 Aug 2023 08:57:56 GMT ETag: "1f61-603bb8a696d78" Accept-Ranges: bytes Content-Length: 8033 Cache-Control: max-age=2592000 Expires: Sun, 24 Sep 2023 09:13:40 GMT Content-Type: image/png
Look for headers like Cache-Control and ETag in the response to confirm caching.