Spread the love

Have you ever 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 in Apache web server. This will make your website faster and better and meet the requirements of google website page speed.

What does this module do? The 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:

# In the console enable mod expires
a2enmod expires
Code language: PHP (php)

Then we restart apache2

# Restart Apache web server
service apache2 force-reload
Code language: PHP (php)

Our next step is going to be to edit 000-Default.conf

# Edit 000-Default.conf in /etc/apache2/sites-available/
pico /etc/apache2/sites-available/000-default.conf
Code language: PHP (php)

Between the lines <VirtualHost *:80> and </VirtualHost> add the following code below:

# Add between the lines <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"
Code language: Apache (apache)

After this we have to restart again:

# Restart Apache web server
service apache2 restart
Code language: PHP (php)

If all is done correctly the error is going to disappear.

We hope you enjoyed this article. if that is so please rate this page with the stars bellow and subscribe to our YouTube channel.

Leave a Reply