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

Vic ShΓ³stak - Jun 26 '20 - - Dev Community

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 koddr / useful-playbooks

🚚 Useful Ansible playbooks for easily deploy your website or webapp to absolutely fresh remote virtual server and automation many processes. 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>"
Enter fullscreen mode Exit fullscreen mode
  • <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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The folder structure should look like this at the moment:

.
β”œβ”€β”€ nginx-<VERSION>/
β”‚   └── ...
└── ngx_brotli/
    └── ...
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

☝️ 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
Enter fullscreen mode Exit fullscreen mode

βœ… 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;
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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 articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

❗️ You can support me on Boosty, both on a permanent and on a one-time basis. All proceeds from this way will go to support my OSS projects and will energize me to create new products and articles for the community.

support me on Boosty

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My main projects that need your help (and stars) πŸ‘‡

  • πŸ”₯ gowebly: A next-generation CLI tool that makes it easy to create amazing web applications with Go on the backend, using htmx, hyperscript or Alpine.js and the most popular CSS frameworks on the frontend.
  • ✨ create-go-app: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.

Other my small projects: yatr, gosl, json2csv, csv2api.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player