How to Enable Apache mod_expires in Ubuntu


enable apache mod_expires ubuntu

Apache allows you to cache content in various ways. Caching improves website speed and reduces server load. Here we look at how to enable Apache mod_expires in Ubuntu to set the Expires header and max-age directive in Cache-control header of static files.

 

How to Enable Apache mod_expires in Ubuntu

Before you proceed please ensure you have installed Apache server in Ubuntu, or any other Linux system that you use. Here are the steps to enable Apache mod_expires in Ubuntu

 

1. Enable mod_expires

First, we enable mod_expires with the command:


$ sudo a2enmod expires

 

Restart Apache Web Server to apply changes


$ sudo systemctl restart apache2 [Ubuntu/Debian]
$ sudo systemctl restart httpd [RHEL/CentOS]

 

2. Configure mod_expires

You can place the mod_expires configuration in Apache server configuration, virtual host file, <Directory> block or even inside .htaccess file.

In this case, we place it in the default virtual host file of Apache.


$sudo vi /etc/apache2/sites-available/default

 

If you only want certain file types to expire after some time, you can use a combination of FilesMatch and ExpiresDefault directive. The following example will cache .jpg, .jpeg, .png, .gif, .js, and .css files for one week


<IfModule mod_expires.c>
 <FilesMatch "\.(jpe?g|png|gif|js|css)$">
  ExpiresActive On
  ExpiresDefault "access plus 1 week"
 </FilesMatch>
</IfModule>

 

Restart Apache Web Server to apply changes


$ sudo systemctl restart apache2 [Ubuntu/Debian]
$ sudo systemctl restart httpd [RHEL/CentOS]

 

You can also use ExpiresByType directive, instead of FilesMatch and ExpiresDefault directive

In this case, it sets the Expires directive for each file type, and also the max-age directive of Cache-Control header

 


<IfModule mod_expires.c>
 ExpiresActive on

 ExpiresByType image/jpg "access plus 60 days"
 ExpiresByType image/png "access plus 60 days"
 ExpiresByType image/gif "access plus 60 days"
 ExpiresByType image/jpeg "access plus 60 days"

 ExpiresByType text/css "access plus 1 days"

 ExpiresByType image/x-icon "access plus 1 month"

 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType audio/x-wav "access plus 1 month"
 ExpiresByType audio/mpeg "access plus 1 month"
 ExpiresByType video/mpeg "access plus 1 month"
 ExpiresByType video/mp4 "access plus 1 month"
 ExpiresByType video/quicktime "access plus 1 month"
 ExpiresByType video/x-ms-wmv "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"

 ExpiresByType text/javascript "access plus 1 week"
 ExpiresByType application/x-javascript "access plus 1 week"
 ExpiresByType application/javascript "access plus 1 week"
</IfModule>

 

In the above example, we have given time interval in days, week, month. You can also use years, hours, minutes and seconds.

 

You can also combine time units, like so:


ExpiresByType text/html "access plus 1 month 5 days 3 hours"

 

Instead of basing cache expiry on access time of the user, you can also base it on modified time of the file


ExpiresByType image/gif "modification plus 7 days"

 

3. Testing

Open web browser and visit any page of your website. After the page is loaded, right click anywhere on the page and select ‘Inspect Element’ option. You’ll see a window with page headers and other details in it. You will see an Expires header and Cache-Control header with a max-age directive.

http headers

 

 

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!