Ugacomp

How to set up Compression in Apache Server

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Setting up compression in an Apache server can significantly improve the performance of your website by reducing the amount of data transferred between the server and clients. Compression is achieved using modules like mod_deflate or mod_gzip.

Setting up compression with mode_flate module

mod_deflate is an Apache module that provides server-side HTTP compression to reduce the size of transmitted data. It compresses the content before sending it to the client’s web browser, and the browser then decompresses the content before rendering it. This compression helps to minimize the amount of data transferred between the server and clients, resulting in faster page loading times and reduced bandwidth usage.

When mod_deflate is enabled, it works by examining the Accept-Encoding header of the client’s HTTP request. If the client supports compression, the server compresses the response content using the gzip compression algorithm before sending it. If the client does not support compression, the server sends the uncompressed content.

How to enable mod_deflate in Apache?

Firstly, check if the mod_deflate module is already installed on your Apache server. You can do this by looking for the module file or by using a command like:

apachectl -t -D DUMP_MODULES | grep deflate

If mod_deflate is installed and enabled, the output of the command apachectl -t -D DUMP_MODULES | grep deflate would look like this:

deflate_module (shared)

This indicates that the deflate_module is loaded as a shared module.

If mod_deflate is not installed or enabled, the command will not return any output, or it may return an error message indicating that the module is not found so you will need to enable it using the following command:

sudo a2enmod deflate

The above command enables deflate_module on both Ubuntu/Debian systems and CentOS/RHEL systems

Configure mod_deflate

After enabling the module, you need to configure it to specify which types of content to compress and other settings. Open your Apache configuration file using a text editor. The location of the file may vary, but common paths include /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf.

Add the following configuration within the appropriate <Directory>, <Location>, or <VirtualHost> block:

<IfModule mod_deflate.c>
    # Enable compression for specific content types
    AddOutputFilterByType DEFLATE text/html text/plain text/xml

    # Set compression level (9 is the highest compression, but also the most CPU-intensive)
    DeflateCompressionLevel 9

    # Remove browser bugs
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    Header append Vary User-Agent
</IfModule>

What is the appropriate compression level in mod_deflate?

The appropriate compression level in mod_deflate depends on a trade-off between the amount of compression applied and the server’s CPU usage. The compression level is specified by the DeflateCompressionLevel directive, and it can range from 1 to 9, where 1 indicates the fastest (but least effective) compression, and 9 indicates the slowest (but most effective) compression.

Here’s a general guideline:

Level 1 (Fastest): This level provides the fastest compression but may not achieve the highest compression ratios. It is suitable for scenarios where minimizing CPU usage is a priority, and the focus is on improving speed rather than achieving maximum compression.

DeflateCompressionLevel 1

Level 9 (Maximum Compression): This level provides the highest compression ratios but at the expense of increased CPU usage. It is suitable for scenarios where bandwidth conservation is a priority, and the trade-off with increased server load is acceptable.

DeflateCompressionLevel 9

Default (Balanced): If you do not explicitly set the compression level, mod_deflate defaults to level 6, which strikes a balance between compression effectiveness and CPU usage. This is often a reasonable compromise for many scenarios.

# DeflateCompressionLevel not explicitly set

It’s recommended to consider the specific characteristics of your server, such as available CPU resources and the importance of minimizing bandwidth usage when choosing the compression level. You may need to experiment with different levels to find the optimal balance for your particular use case.

After making changes, restart the Apache server to apply the new configuration:

sudo service apache2 restart   # for Ubuntu/Debian
sudo systemctl restart apache2 # for CentOS/RHEL

This configuration example enables compression for HTML, plain text, and XML files. You can customize the AddOutputFilterByType directive to include other MIME types as needed.

What file types does mod_deflate compression support?

The mod_deflate module in Apache supports compressing a variety of file types, and you can specify which types of files should be compressed in your configuration. The module uses MIME types to determine which files to compress. Here’s an example of common MIME types that mod_deflate can compress:

<IfModule mod_deflate.c>
    # Compress HTML, CSS, JavaScript, Text, XML and fonts
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-otf
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml

    # Add more file types as needed
</IfModule>

The above configuration instructs mod_deflate to compress files with specific MIME types, such as JavaScript, CSS, HTML, XML, and various font and image formats. You can customize this list based on the types of files your website serves. Additionally, you can use the AddOutputFilterByType directive to specify other MIME types that should be compressed.

Setting up compression with mod_gzip module

mod_gzip is an alternative module to achieve compression in Apache, though it’s important to note that it’s less commonly used compared to mod_deflate. If you specifically want to use mod_gzip, here are the steps to set up compression with the mod_gzip module:

Check if mod_gzip is Installed

Before proceeding, check if mod_gzip is already installed and enabled on your Apache server. You can do this by looking for the module file or by using a command like:

apachectl -t -D DUMP_MODULES | grep gzip

If mod_gzip is installed, you should see a line like gzip_module (shared).

Enable mod_gzip

If the module is not enabled, you need to enable it by running the following command for Debian and CentOS/RHEL systems:

sudo a2enmod gzip

Configure mod_gzip

Open your Apache configuration file using a text editor. The location of the file may vary, but common paths include /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf.

Add the following configuration within the appropriate <Directory>, <Location>, or <VirtualHost> block:

<IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

After making changes, restart the Apache server to apply the new configuration:

sudo service apache2 restart   # for Ubuntu/Debian
sudo systemctl restart apache2 # for CentOS/RHEL

This configuration example enables compression for HTML, plain text, CSS, JavaScript, and PHP files. It also excludes compressing images and prevents double compression of already compressed content.

Should I use mode_flate or mod_gzip?

It’s important to note that mod_gzip is as feature-rich or as widely supported as mod_deflate, and mod_deflate is generally recommended for modern setups. If possible, consider using mod_deflate unless you have specific reasons to use mod_gzip.

Test your compression configuration

One of the ways to test if compression is properly configured on apache is to use online tools like GzipWTest

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.