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.
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>"
-
<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! 😎
📚 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, like1.17.10
. You can check version on your remote server by runningnginx -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+
.
💬 Questions for better understanding
- What is Ansible used for? Please read the official docs.
- What does it take to work with Ansible playbook?
- What version of Nginx should your configure the Brotli module with?
- Why should you install the
gcc
andcmake
packages to build the Brotli module?
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.
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.
Top comments (7)
Thanks a bunch! This worked fine, but not without few clithces (on ubuntu 20.04) in DO.
After cloning ngx_brotli, I had to go in the cloned folder and run
git submodule update --init
because without it I received an error when trying to build modules.Then the path of modules was different for my install and was /usr/lib/nginx/modules (not /usr/share).
Hi, Alan. Great addition to the article! 👍
Unfortunately, (in my practice) it was not necessary to do the actions you described.
I got the same error with exactly the same solution: to init submodules:
Once I did that everything went well. It might be that in other tutorials they suggest cloning brotli with
git clone --recursive
command. Thank you for the article!Thanks for such a detailed tutorial (as usual)
You're welcome (as usual) 🥰
Tried on Ubuntu 22.04, It works like charm!
only error is we need to update the sunmodule to compile the folders.
error details:
`Please make sure that the git submodule has been checked out:
`
root@localhost:~/nginx-1.18.0# sudo make modules
make: *** No rule to make target 'modules'. Stop.