Enable Gzip compression | Google PageSpeed improvements
May 16th 2016
Learn how to enable Gzip compression and increase your Google PageSpeed score.
In this tutorial, I will show you each and every step you need to take in order to enable gzip compression. By implementing the gzip compression, your website will see an improvement in load time.
If you are a tech-savvy person, you can skip the explanations and head over to the snippet, which is located at the end of the page.
Note: This tutorial was thought out for apache servers, the
.htaccess code presented here is an excerpt from the HTML5 Boilerplate project.
Let's get started!
First off, make sure that
AllowOverride is set to
You can check the apache config file located in
sudo nano /etc/apache2/apache2.conf
Look for the section
<Directory "your/domain/path" > section (w/o quotes).
If the option is set to
None, change it to
All and restart the server.
<Directory /var/www/> AllowOverride All </Directory>
sudo service apache2 restart
Check if the modules are loaded
In order to enable gzip, you need a bunch of apache modules loaded. Sometimes these are enabled by default, but if they are not, you will deal with a lot of frustration because the compression won't work.
So, let's check if the compression modules are enabled.
apache2ctl -M | grep deflate
apache2ctl -M | grep filter
apache2ctl -M | grep setenvif
Enable the modules (if they are not loaded)
If you run the previous commands and there is no output, that means that the modules are not loaded. You can enable them with:
sudo a2enmod deflate
sudo a2enmod filter
sudo a2enmod setenvif
Gzip Compression Script
Here is the entire script that you can copy into your
.htaccess file (the one located in the root directory of your site):
Don't forget to restart the server after all is done:
sudo service apache2 restart.
That's all, by appending this script in your
.htaccess file you've improved your Google PageSpeed score.