How to Configure Apache Cache in CentOS


how to configure apache cache in centos

Apache web server allows you to cache contents of your website, thereby improving its speed and reducing its server load. Here’s how you can configure Apache Cache in CentOS.

 

How to Configure Apache Cache in CentOS

Here are the steps to configure Apache Cache in CentOS. Before we proceed, please ensure that you have installed Apache Web Server on CentOS system.

 

1. Enable required Apache modules

Apache uses 3 modules for caching. To enable it on them on your Apache server, open your server config file


$ sudo vim /etc/httpd/conf/httpd.conf 

 

Look for the following lines and uncomment them by removing the ‘#’ sign at their beginning. To enable caching, uncomment the line


LoadModule disk_cache_module modules/mod_disk_cache.so

 

If you want you caching to be done on disk, instead of memory uncomment the following line. Otherwise, it will be done from memory, which can be fast but resource intensive.


LoadModule disk_cache_module modules/mod_disk_cache.so

 

Also, add the following lines in your server config file. If you have enabled Virtual Hosts for your website, you can add these lines in its <VirtualHost> also.


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

 

  • CacheEnable – Enables disk-based caching
  • CacheRoot – Location of your cache directory
  • CacheDefaultExpire – Default expiry time for cache items
  • CacheDisable – Folders which you don’t want to be cached

 

2. Enable mod_expires

mod_expires module allows you to set expiry conditions on your cache items, in various ways – as a whole or individually based on matching string

 

Open your Apache config file, and uncomment the line


LoadModule expires_module modules/mod_expires.so

 

Also, add the following server config


<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 1 day"
 </IfModule></pre>

 

  • ExpiresActive – Turn on mod_expires module
  • ExpiredDefault – Sets the expiration date on cache items

You can also set expiry dates for each type of item using ExpiresByType and ExpiresDefault. Here’s an example,


<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 1 day"
 ExpiresByType image/jpg "access plus 5 days"
 ExpiresByType image/jpeg "access plus 5 days"
 ExpiresByType image/gif "access plus 5 days"
 ExpiresByType image/png "access plus 5 days"
 ExpiresByType text/css "access plus 1 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType text/x-javascript "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access plus 1 month"
 ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

 

Enforce Expiration Date (Optional)

Cache content requires expiration date. If some of your cache items do not have expiration date, you can use the CacheIgnoreNoLastMod like so


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

 

Prevent Browser Caching (Optional)

When you cache content, it gets cached both in users’ web browser as well as server. If you don’t want web browsers to cache your content and want only your web server to do it, then use the CacheIgnoreCacheControl directive. This will make Apache ignore browser’s requests to refresh its cache content.


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

 

Congratulations! You know how to configure Apache cache in CentOS.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!