How to configure Apache Microcaching in Ubuntu?

Submitted by sysop on Wed, 10/04/2023 - 22:52

Apache Microcaching in Ubuntu


Apache HTTP Server, commonly known as Apache, is a widely used open-source web server software. Caching is an essential aspect of web server optimization, and it can significantly improve the response time and reduce the load on the server. Apache Microcaching typically refers to a small and quickly accessible cache that is often used in the context of web servers to improve performance.

Here are general steps you might follow to configure Apache Microcaching in Ubuntu:

  1. Enable mod_cache and mod_disk modules: Make sure the mod_cache module is enabled in your Apache server. Kindly execute below command to enable the modules:
    sudo a2enmod cache cache_disk
  2. Configure mod_cache and mod_disk_cache: You'll need to configure the mod_cache and mod_disk_cache modules. Below is a basic example. Adjust the configuration based on your requirements. This configuration is tailor made for Drupal. Kindly adjust this to your needs for other CMSs. Kindly edit your vhost conf file and add the below lines:
      <IfModule mod_cache.c>
      #Turn off CacheQuickHandler directive.
      CacheQuickHandler Off
     
      #Turn of Cache Headers as only enable if required.
      CacheHeader Off
      CacheDetailHeader Off
      
      #Cache everything
      CacheIgnoreNoLastMod On
    
      #Set Cache expiry
      CacheDefaultExpire 180
      CacheMinExpire 5
      CacheStoreExpired Off
      CacheLastModifiedFactor 0.5
      
      #Ignore Cache control headers
      CacheIgnoreCacheControl On
      CacheIgnoreHeaders Set-Cookie Cookie
    
      #Set CacheLock to avoid stale caches
      CacheLock on
      CacheLockMaxAge 5
      CacheDisable /admin
      
      #Add GZIP compression
      SetOutputFilter CACHE
      AddOutputFilterByType DEFLATE text/html text/plain text/css 
                            application/javascript 
                            application/rss+xml 
                            text/xml image/svg+xml
      
      #Configure mod disk cache
      <IfModule mod_cache_disk.c>
      # Adjust CacheRoot according to your needs
      CacheRoot /var/cache/apache2/proxy/
      CacheEnable disk /
      CacheDirLevels 2
      CacheDirLength 1
      CacheMaxFileSize 20000000
      </IfModule>
      </IfModule>

    Adjust the CacheRoot directive to point to a directory where you want to store the cached data. Then save the file.

  3. Create the folder for CacheRoot, set the ownership to web-user i.e www-data:

  4. Restart Apache: Restart Apache to apply the changes.
    # Restart Apache
    sudo systemctl restart apache2
    
    OR
    
    sudo service apache2 restart
  5. Verify if cache is working: You can just check the CacheRoot folder, execute the below command:

    ls /var/cache/apache2/proxy

    You must notice the folders with cached files.

    Note: You may need to adjust the configuration based on your specific use case and performance goals. Always refer to the Apache documentation for the most accurate and up-to-date information.

    That's it. You have configured Apache Microcaching in Ubuntu. Congratulations!!!