loading...
Cover image for βš™οΈ How to install Brotli module for Nginx on Ubuntu 20.04+

βš™οΈ How to install Brotli module for Nginx on Ubuntu 20.04+

koddr profile image Vic ShΓ³stak ・3 min read

Introduction

Welcome back, DEV people! πŸ’¬ How about to find out once and for all how to install the Brotli module for Nginx on Ubuntu 20.04+?

Fasten your seatbelts, because we're starting! πŸš€ I will show you a fast automated way (which I use myself) and more detailed (and manual).

πŸ“ Table of contents

⚑️ Quick guide

Check, that you have Python (at least version 3.5) and Ansible (I recommend the 2.9 version) on your local machine.

Next, download ZIP archive (or git clone) Useful playbooks GitHub repository and go to the downloaded (or cloned) folder.

GitHub logo truewebartisans / useful-playbooks

🚚 Useful Ansible playbooks for easily deploy your website or webapp to absolutely fresh remote virtual server. Only 3 minutes from the playbook run to complete setup server and start it.

Select Ansible playbook called install_brotli-playbook.yml (see docs) and run it with --user and --extra-vars arguments, like this:

ansible-playbook \
                  install_brotli-playbook.yml \
                  --user <USER> \
                  --extra-vars "host=<HOST>"
  • <USER> is the remote user's username (for example, root)
  • <HOST> is the host name in your inventory (from /etc/ansible/hosts file)

This playbook will determine the installed version of Nginx itself, download the necessary dependencies, build and configure Brotli module and add the load_module section to Nginx configuration (/etc/nginx/nginx.conf).

Enjoy! 😎

↑ Table of contents

πŸ“š A long, but detailed guide

If you like longer and more detailed stories, as well as I do, let's start with a simple step: login to your remote virtual server running on Ubuntu 20.04+ (via SSH, for example).

And we're ready! πŸ˜‰

βœ… Install dependencies

sudo apt install \
                  git gcc cmake libpcre3 libpcre3-dev \
                  zlib1g zlib1g-dev openssl libssl-dev

If you already have some packages (from the list above) installed, you do not need to install them again.

βœ… Download the source code

First, download and unpack Nginx:

wget https://nginx.org/download/nginx-<VERSION>.tar.gz
tar zxvf nginx-<VERSION>.tar.gz

Where <VERSION> is the Nginx version, like 1.17.10. You can check version on your remote server by running nginx -v command.

Second, git clone Brotli module from official Google repository:

git clone https://github.com/google/ngx_brotli.git

The folder structure should look like this at the moment:

.
β”œβ”€β”€ nginx-<VERSION>/
β”‚   └── ...
└── ngx_brotli/
    └── ...

βœ… Build Brotli module with Nginx

Go to ./nginx-<VERSION> folder and run:

# Configure dynamic module
sudo ./configure --with-compat --add-dynamic-module=../ngx_brotli

# Make
sudo make modules

☝️ Please wait, this may take some time!

Finally, copy ready module *.so files from ./nginx-<VERSION>/objs to the Nginx modules folder:

sudo cp ./objs/*.so /usr/share/nginx/modules

βœ… Add Brotli config

Add load_module section to the start of Nginx config:

# /etc/nginx/nginx.conf

# Load module section
load_module "modules/ngx_http_brotli_filter_module.so";
load_module "modules/ngx_http_brotli_static_module.so";

# ...

events {
  # ...
}

http {
  # ...

  # Include configs folders
  include /etc/nginx/conf.d/*.conf;
  include /usr/share/nginx/modules/*.conf;
}

Now, add the Brotli config:

# /etc/nginx/conf.d/brotli.conf

# Enable Brotli
brotli            on;
brotli_static     on;
brotli_comp_level 6;

# File types to compress
brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

Restart Nginx service and we're done. It just works! πŸŽ‰

Congratulations! We managed to install Brotli module for Nginx web server on Ubuntu 20.04+.

↑ Table of contents

πŸ’¬ Questions for better understanding

  1. What is Ansible used for? Please read the official docs.
  2. What does it take to work with Ansible playbook?
  3. What version of Nginx should your configure the Brotli module with?
  4. Why should you install the gcc and cmake packages to build the Brotli module?

↑ Table of contents

Photos/Images by

  • Jacob Miller (link: 1)

P.S.

If you want more β€” write a comment below & follow me. Thx! 😘

Posted on Jun 26 by:

koddr profile

Vic ShΓ³stak

@koddr

Hey! πŸ‘‹ I'm founder and full stack web developer (Go, JavaScript, Docker & automation) at True web artisans. Golang lover, UX evangelist, DX philosopher & UI Dreamer with over 12+ years of experience.

Discussion

markdown guide
 

Thanks for such a detailed tutorial (as usual)

 

You're welcome (as usual) πŸ₯°