Caching is the need of the hour, If your website loads slower than your competitor’s, you have almost lost the battle. Webmasters and system administrators use various techniques to improve the loading time of their sites, Apache’s mod_expires module is also used for similar purposes. By properly configuring this module, you will be able to control the cache settings of your site in visitor’s browsers. “Cache-Control HTTP headers” are the ones that are responsible for caching your website(s) in browsers, mod_expires sets the expiry values on the backend on the actual server, so your visitor’s don’t have to wait for page load from server every time. If you don’t have this module configured on the server, then your visitors will have slow load time each time they will try to visit your site.
How mod_expires is used?
There are certain methods to enable caching of your websites, some administrators specify a particular directory to cache, or some administrators cache the whole web directory, or more popular approach is to cache the web content based on file extensions. It all depends on your website that which type of caching mechanism is suitable for it. We will be reviewing how to enable browser based caching for certain file extensions like jpg, png, css etc. Most common scenario to use mod_expires is to enable it on web server and provide the list of file extensions and their expiry date.
Enable mod_expires on CentOS and Ubuntu
This module is already enabled on CentOS systems (provided your centos version is > 5 ) but it need to be enabled on Ubuntu systems. First, let’s see how to verify that mod_expires is enabled on CentOS.
Run following command on your CentOS system to see if Apache module mod_expires is enabled or not, It should show following output if enabled, otherwise you’ll see nothing in output.
Another way to verify it is via httpd.conf file, open apache configuration file in your favorite text editor application (Vi/ViM, Nano etc) and search for following line. If successfully found, then your Apache has mod_expires enabled.
For Ubuntu system, we need to use a2enmod utility to enable Apache modules, simply run following command to enable expires module on Ubuntu.
Once done, we need to restart apache for this change to take effect.
Configure File’s Caching in mod_expire
Alright, we are done with the installation of this module, lets configure it to enable caching for our specified file extenions. For the sake of demonstration, we will be enabling caching for following file type.
.jpg, .png, .css, js
Firs of all create a new file named “expire.conf” inside /etc/httpd/conf.d/ directory.
Open this newly created file in text editor and populate it as follows.
ExpiresByType image/jpg “access plus 60 days”
ExpiresByType image/png “access plus 60 days”
ExpiresByType image/js “access plus 60 days”
ExpiresByType image/jpeg “access plus 60 days”
ExpiresByType text/css “access plus 1 days”
“ExpiresActive on” will enable this module, the rest are the file extensions followed by their expiry time. If user want to load these files without using caching, he/she will need to clear their browser cache, otherwise cache will expiry after the specified amount of time and new cache will be created accordingly.
Restart apache web server for the changes to take effect.
or if you are using CentOS 7 /RHEL 7 ;
Congratulations! You have successfully configured browser caching for your websites.
Special Scenario : If you are hosting multiple websites on your system and don’t want this caching to apply to all sites, then you will need to use .htaccess file for the individual sites to place this code there. “.htaccess” is used to override apache configuration on every website’s level.
Hope you find this article useful; Apache caching can be achieved by using various modules, but mod_expires is specially useful for enable browser caching of your sites. After enabling this module, you should be seeing noticeable improvements in your website’s loading time.
If you enjoy our article(s), Please use “Get Linuxpitstop articles by Email” option to get latest articles by email. Please like, follow and share our social network pages, we need help from esteemed visitors like yourself to keep this venture going.