Caching in Apache improves web server performance by storing frequently requested resources. This lowers server load and shortens response times by serving content from a dedicated storage layer, making it essential in high-traffic environments.
Various caching options, such as disk-based caching with mod_cache_disk and memory-based caching with mod_socache_shmcb, address different performance goals. The right choice depends on the type of content served and the hardware resources available.
Careful configuration of Cache-Control directives determines how long content remains stored and when it is invalidated. Proper planning ensures that static and dynamic resources are cached appropriately, improving the user experience and reducing server overhead. In production environments, verifying correct caching behavior is vital for maintaining reliable service.
Steps to enable and configure caching in Apache:
- Enable the necessary caching modules in 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 required directives to enable and configure caching.
<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.

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.