GZIP compression is the process of taking your data and reducing its size by removing unnecessary characters like spaces and punctuation marks. When you use GZIP, the server can compress your data before it sends it to the client. The client then has less work to do when receiving the data, so it can receive it faster.
When you send data over a network, there are two main parts: sending and receiving. Sending involves taking your data and sending it from your computer to the server. Receiving involves taking that data and making sure it gets into your computer intact. There are lots of different things that can go wrong along these two lines – from losing power to having bad internet connections. When you compress your data, you reduce both the transmission time and the amount of work that needs to be done on both ends of the line.
Enabling Gzip Compression in Apache
The Ubuntu and Debian system users follow the below steps to enable gzip compression in the Apache server.
- First of all, enable the mod_deflate module, which provides gzip compression to the Apache server.
sudo a2enmod deflate
- Once the module is enabled in the Apache server. Create or edit the
.htaccess
file in your website document root. For example, if the website document root is /var/www/html, then create file as:sudo nano /var/www/html/.htaccess
Append the following snippet to the file.
123456789101112131415161718192021222324# GZIP compression for text files: HTML, CSS, JS, Text, XML, fonts<IfModule mod_deflate.c>AddOutputFilterByType DEFLATE text/htmlAddOutputFilterByType DEFLATE text/cssAddOutputFilterByType DEFLATE text/javascriptAddOutputFilterByType DEFLATE application/javascriptAddOutputFilterByType DEFLATE application/xmlAddOutputFilterByType DEFLATE text/plainAddOutputFilterByType DEFLATE text/xmlAddOutputFilterByType DEFLATE font/opentypeAddOutputFilterByType DEFLATE font/otfAddOutputFilterByType DEFLATE font/ttfAddOutputFilterByType DEFLATE image/svg+xmlAddOutputFilterByType DEFLATE image/x-iconAddOutputFilterByType DEFLATE application/rss+xmlAddOutputFilterByType DEFLATE application/vnd.ms-fontobjectAddOutputFilterByType DEFLATE application/x-fontAddOutputFilterByType DEFLATE application/x-font-otfAddOutputFilterByType DEFLATE application/x-font-opentypeAddOutputFilterByType DEFLATE application/x-font-truetypeAddOutputFilterByType DEFLATE application/x-font-ttfAddOutputFilterByType DEFLATE application/x-javascriptAddOutputFilterByType DEFLATE application/xhtml+xml</IfModule> - Use the following command to restart the Apache server.
sudo systemctl restart apache2
Verify Gzip Compression
Now that your site has been enabled with gzip compression, let’s use one of below online tools to verify that Gzip is working correctly.
Conclusion
GZIP compression is a technique that reduces the size of data sent over the network. This can be useful when sending large files over a slow connection, or when there is a lot of data to send. By compressing your data, you may be able to reduce the size of your transfer.
In this tutorial, you have learned about enabling Gzip compression in the Apache web server.
8 Comments
Thanks, useful tutorial.
This is good, but I highly recommend everyone check the HTML5 Boilerplate example for a bit more detail: https://github.com/h5bp/server-configs-apache
Go to the /dist folder you’ll find a .htaccess with best practices for many things Apache, including gzip
i followed above things but js/css not compressed- > google speed test tool -> http://www.namomithrarealestate.com….
Hi I am Sanjay Raturi from India.
I have to enable Gzip compression for my website yogainrishikesh.in
Please help me. please.
I have 100/100 page speed on pagespeed insights google tool.
See http://www.yogainrishikesh.in on Google pagespeed insights.
You have a typo! sudo a2enmod defalte should be sudo a2enmod deflate!
Thanks SAM!!!
Very useful, thank you.