Make Browsers Cache Static Files With mod_expires On Apache2 Ubuntu 18.04

User Avatar
👤 admin
🔴 Admin
✍️ The most important thing in the world is to not be alone.
⏳ Last active: 15 Apr 2025 at 16:01
📅 Created: 11 Feb 2021 at 12:07
👀 Viewed: 18 times
✉️ Send Email

If you have tested your website for speed with googles website test tool then you have seen the error: Leverage browser caching not enabled. To fix this you need to enable and configure mod_expires on Apache2. This will make your website faster and better and meet the requirements of google website page speed.


This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.


These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.


First we need to enable the mod in the terminal:


a2enmod expires

Then we restart apache2


service apache2 force-reload


If we don't have any errors we continue.


pico /etc/apache2/sites-available/000-default.conf
and add between <VirtualHost *:80> and </VirtualHost>


<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>

If you want to comment: Login or Register