How to Enable Apache mod_expires in Debian

enable apache mod_expires debian

mod_expires is one of the most important Apache modules to enable content caching of your website. Caching helps improve website speed and reduce server load. Here’s how to enable Apache mod_expires in Debian. mod_expires allows you to set the Expires header and max-age directive of Cache-Control header of static files.



How to Enable Apache mod_expires in Debian

Here are the steps to enable Apache mod_expires in Debian. Before we proceed, please ensure you have installed Apache web server on your Debian system.


1. Enable mod_expires

First, we enable mod_expires module on Debian system

$ sudo a2enmod expires

You can also enable mod_headers, if you want. mod_headers allows you to set response headers for caching.

$ sudo a2enmod headers


Restart Apache server to apply changes

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


2. Configure mod_expires

You can add the mod_expires configuration code to your Apache’s server configuration file, virtual host file, .htaccess file or even inside the <Directory> block.

In this example, we place mod_expires configuration in virtual host file

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

The following block will check if mod_expires is enabled and then set the expiry date to 1 week from when your files were accessed last. It uses the FilesMatch directive to cache only .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"


Restart Apache server to apply changes

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


Instead of using FilesMatch, you can also use ExpiresByType to set the cache expiry date for specific file types. In the following example, we set the Expires directive as well as 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"


In the above example, we have set the interval in days, weeks and months. You can also use seconds, minutes, hours and years.

In fact, you can also combine time units, like shown below:

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


Instead of setting cache expiry by last access time, you can also set it based on last modified time

ExpiresByType image/gif "modification plus 7 days"


3. Testing

You can test the configuration by simply opening web browser and visiting any page on your website. After the page is loaded, right click anywhere on the screen, and click ‘Inspect Element’ from the dropdown menu.

You’ll see a window with page headers and other details in it. for each file loaded on the page. Select it for a static file (e.g image, js, css file). Have a look at its Expires and Cache-Control header.

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!