LinuxTutorials

How To Enable Gzip Compression on Apache

Enable Gzip Compression on Apache

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 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 the root user. We recommend acting as a non-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. Installing Apache.

By default, Apache is available on Ubuntu or Debian base repository. Now run the following command below to install the latest version of Apache to your Ubuntu system:

sudo apt install apache2 libapache2-mod-deflate

After successful installation, enable Apache (to start automatically upon system boot), start, and verify the status using the commands below:

sudo systemctl enable apache2
sudo systemctl start apache2
sudo systemctl status apache2

Step 2. Configure Apache mod_deflate.

To configure the mod_deflate module, open the Apache configuration file in a favorite text editor:

nano /etc/apache2/apache2.conf

At the end of the file, add the following code:

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
DeflateCompressionLevel 9
</IfModule>

Save and close the file, then restart the Apache service to apply the changes:

sudo systemctl restart apache2

Step 3.  Test the Configuration.

To test if gzip compression is working on your Apache server, you can use a tool such as check Gzip compression.

Congratulations! You have successfully enabled Gzip compression. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button