Apache · December 17, 2023

Apache for Newbie: Set up Apache with mod_brotli

Apache for Newbie: Set up Apache with mod_brotli

When it comes to web servers, Apache is one of the most popular choices among developers and website owners. Its flexibility, reliability, and extensive feature set make it a top choice for hosting websites. In this article, we will explore how to set up Apache with mod_brotli, a compression module that can significantly improve website performance.

What is mod_brotli?

Mod_brotli is an Apache module that provides support for the Brotli compression algorithm. Brotli is a compression algorithm developed by Google that offers better compression ratios compared to other algorithms like Gzip. By compressing web content, mod_brotli reduces the size of files sent from the server to the client, resulting in faster load times and reduced bandwidth usage.

Why use mod_brotli?

There are several reasons why you should consider using mod_brotli with Apache:

  • Improved website performance: Compressed files are smaller in size, leading to faster load times for your website visitors.
  • Reduced bandwidth usage: Smaller file sizes mean less data needs to be transferred, resulting in reduced bandwidth usage and lower hosting costs.
  • Better search engine rankings: Website speed is a crucial factor in search engine rankings. By improving your website's performance, you can potentially boost your search engine rankings.

Setting up Apache with mod_brotli

Before setting up mod_brotli, make sure you have Apache installed on your server. If you haven't installed Apache yet, you can follow the official Apache documentation for your specific operating system.

Once Apache is installed, follow these steps to set up mod_brotli:

Step 1: Enable mod_brotli

The first step is to enable the mod_brotli module in Apache. Open your Apache configuration file, which is typically located at /etc/apache2/apache2.conf or /etc/httpd/httpd.conf, depending on your operating system.

Search for the line that starts with #LoadModule brotli_module modules/mod_brotli.so and remove the # at the beginning of the line to uncomment it. Save the file and exit the text editor.

Step 2: Configure mod_brotli

Next, you need to configure mod_brotli to specify which files should be compressed. Open your Apache configuration file again and add the following lines:

 <IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
    AddOutputFilterByType BROTLI_COMPRESS application/x-javascript application/rss+xml
    AddOutputFilterByType BROTLI_COMPRESS application/xml
    AddOutputFilterByType BROTLI_COMPRESS application/json
    AddOutputFilterByType BROTLI_COMPRESS application/ld+json
    AddOutputFilterByType BROTLI_COMPRESS application/schema+json
    AddOutputFilterByType BROTLI_COMPRESS application/manifest+json
    AddOutputFilterByType BROTLI_COMPRESS application/vnd.geo+json
    AddOutputFilterByType BROTLI_COMPRESS application/vnd.api+json
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-ttf
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-opentype
    AddOutputFilterByType BROTLI_COMPRESS application/vnd.ms-fontobject
    AddOutputFilterByType BROTLI_COMPRESS application/font-woff
    AddOutputFilterByType BROTLI_COMPRESS application/font-woff2
    AddOutputFilterByType BROTLI_COMPRESS application/x-web-app-manifest+json
    AddOutputFilterByType BROTLI_COMPRESS application/xhtml+xml
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-truetype
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-opentype
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-eot
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-otf
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-ttf
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-woff
    AddOutputFilterByType BROTLI_COMPRESS application/x-font-woff2
    AddOutputFilterByType BROTLI_COMPRESS application/vnd.ms-fontobject
    AddOutputFilterByType BROTLI_COMPRESS font/ttf
    AddOutputFilterByType BROTLI_COMPRESS font/otf
    AddOutputFilterByType BROTLI_COMPRESS font/eot
    AddOutputFilterByType BROTLI_COMPRESS font/woff
    AddOutputFilterByType BROTLI_COMPRESS font/woff2
    AddOutputFilterByType BROTLI_COMPRESS image/svg+xml
    AddOutputFilterByType BROTLI_COMPRESS image/x-icon
    AddOutputFilterByType BROTLI_COMPRESS image/vnd.microsoft.icon
    AddOutputFilterByType BROTLI_COMPRESS application/ico
    AddOutputFilterByType BROTLI_COMPRESS text/cache-manifest
</IfModule>

These lines specify the file types that should be compressed using Brotli. You can customize this list based on your specific needs.

Step 3: Restart Apache

After making the necessary changes, save the Apache configuration file and restart Apache to apply the changes. The command to restart Apache varies depending on your operating system. Here are a few examples:

sudo service apache2 restart  # Ubuntu/Debian
sudo systemctl restart apache2  # CentOS/RHEL
sudo apachectl restart         # macOS

Once Apache is restarted, mod_brotli will be enabled and ready to compress the specified file types.

Summary

Setting up Apache with mod_brotli can significantly improve website performance by compressing files and reducing bandwidth usage. By following the steps outlined in this article, you can easily enable and configure mod_brotli on your Apache server. Enjoy faster load times, reduced bandwidth costs, and potentially improved search engine rankings with mod_brotli!

For more information about VPS hosting solutions, visit Server.HK. Server.HK offers reliable and affordable Hong Kong VPS hosting services with top-notch support and performance.