In this tutorial, we will show you how to enable Gzip compression on Apache. For those of you who didn’t know, Apache’s mod_deflate is an Apache module that will compress output from your server before it is sent to the client. If you have a newer version of Apache the mod_deflate module is probably loaded by default, but it may not be turned on.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step enable Gzip compression on Apache.
Prerequisites
- A server running one of the following operating systems: Ubuntu or any other Debian-based and RHEL-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Enable Gzip Compression on Apache
Step 1. First, verify that the module is loaded in your httpd.conf
file.
Your current version of Apache may already have enabled this module for you, but you may still want to be sure. To enable this module, add the following line (if it does not exist) in the Apache configuration file.
LoadModule deflate_module modules/mod_deflate.so
Step 2. Configure httpd.conf
file.
Open httpd.conf
file using a text editor and place the following in it:
#nano /etc/httpd/conf/httpd.conf # mod_deflate configuration <IfModule mod_deflate.c> # Restrict compression to these MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/css # Level of compression (Highest 9 - Lowest 1) DeflateCompressionLevel 9 # Netscape 4.x has some problems. BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html </IfModule>
Step 3. Restart Apache and Verify.
## CentOS ## service httpd restart ## Ubuntu ## /etc/init.d/apache2 restart
You can use this link to verify your server gzip using one of domains configured on the server.
Congratulations! You have successfully Enable Gzip Compression on the Apache webserver. For additional help or useful information, we recommend you check the official Apache website.