How to Compile and Install Nginx from Source with Brotli and PageSpeed Modules on Ubuntu 24.04
Introduction
At ByteHosting, we understand the importance of a high-performance web server, especially when handling high traffic volumes. One of the best ways to optimize Nginx for speed and efficiency is by compiling it from source with custom modules like Brotli and PageSpeed. In this guide, we’ll walk you through the process of compile nginx from source on Ubuntu 24.04, ensuring you get a tailored, powerful setup for your website or application.
Prerequisites: Ubuntu 24.04 VPS setup and required dependencies
Before we begin, make sure you have a fresh Ubuntu 24.04 VPS. Our cost-effective KVM-based VPS plans in Frankfurt am Main are perfect for this purpose. You’ll also need root or sudo access to install dependencies and configure your server.
Next, install the essential build tools and dependencies:
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
These packages include compilers, libraries, and headers necessary for compiling Nginx and its modules.
Downloading Nginx source code and modules
1. Download Nginx source code
Visit the official Nginx website to find the latest stable version. For example, to download version 1.25.1:
wget http://nginx.org/download/nginx-1.25.1.tar.gz
tar -xzvf nginx-1.25.1.tar.gz
cd nginx-1.25.1
2. Download Brotli module
Brotli compression is a modern, efficient algorithm. We’ll use the ngx_brotli module:
git clone --recursive https://github.com/google/ngx_brotli.git
3. Download PageSpeed module
Google’s PageSpeed module optimizes your site’s assets. Clone the module from its repository:
git clone https://github.com/apache/incubator-pagespeed-ngx.git
Note: You may need to download additional dependencies for PageSpeed, such as the PSOL library. Follow the instructions in the repository for full setup.
Configuring build options and compiling Nginx from source
Now, configure the build with the modules and options you need. Here’s an example configuration:
./configure \
--with-http_ssl_module \
--with-http_v2_module \
--with-cc-opt='-O2' \
--add-module=../ngx_brotli \
--add-module=../incubator-pagespeed-ngx
Replace the paths with the actual locations of your modules if different. After configuration, compile and install:
make -j$(nproc)
sudo make installThis process may take a few minutes depending on your server’s hardware.
Installing the compiled Nginx and setting up systemd service
Once installed, you can start Nginx manually:
sudo /usr/local/nginx/sbin/nginxTo enable Nginx to start on boot, create a systemd service file:
sudo nano /etc/systemd/system/nginx.serviceInsert the following content:
[Unit]
Description=nginx - high performance web server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PIDFile=/usr/local/nginx/logs/nginx.pid
[Install]
WantedBy=multi-user.targetEnable and start Nginx:
sudo systemctl enable nginx
sudo systemctl start nginxVerifying the installation and module integration
Check the Nginx version and modules:
/usr/local/nginx/sbin/nginx -VYou should see the Brotli and PageSpeed modules listed among the compile options. To verify Brotli is active, you can test your server’s headers or use online tools.
Troubleshooting common build and runtime issues
- Compilation errors: Ensure all dependencies are installed and paths are correct.
- Modules not loading: Confirm you used the correct
--add-moduleflags during configuration. - Service not starting: Check logs at
/usr/local/nginx/logs/error.logand ensure the systemd service points to the correct binary.
Conclusion
Compiling Nginx from source on Ubuntu 24.04 allows you to customize your web server with advanced modules like Brotli and PageSpeed, optimizing performance for high-traffic sites. While it requires some initial setup, the benefits of a tailored, high-performance server are well worth the effort. At ByteHosting, we’re committed to providing reliable, affordable hosting solutions—whether you choose our VPS plans or set up your own custom environment. Follow this guide to get your optimized Nginx up and running today!