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. Whether you're hosting a personal project or managing a large-scale website, optimizing your Nginx installation can significantly improve load times and user experience. In this tutorial, we will guide you through the process of compiling Nginx from source on Ubuntu 24.04, with the added benefit of integrating Brotli compression and Google PageSpeed modules. These enhancements can boost your website's speed and efficiency, making your server more competitive and reliable.
Prerequisites
Before we begin, ensure you have a fresh Ubuntu 24.04 VPS. Our servers in Frankfurt, Germany, are perfect for hosting high-performance applications. You will also need some essential build tools and dependencies installed on your server.
Update your system
sudo apt update && sudo apt upgrade -yInstall build essentials and dependencies
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev git wgetThese packages include compilers, libraries, and tools necessary for compiling Nginx and its modules.
Downloading Nginx Source Code and Modules
Next, download the latest stable version of Nginx source code from the official website. We also need to clone the Brotli and PageSpeed modules from their repositories.
Download Nginx source
NGINX_VERSION=1.24.0
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
tar -xzvf nginx-$NGINX_VERSION.tar.gz
cd nginx-$NGINX_VERSIONClone the modules
- Brotli module: We will use the official Brotli module maintained by Google.
- PageSpeed module: Google's PageSpeed module enhances your site’s performance by optimizing resources.
git clone https://github.com/apache/incubator-pagespeed-ngx.gitgit clone --recursive https://github.com/google/ngx_brotli.gitCompiling Nginx with Brotli and PageSpeed Modules Enabled
Now, we configure the build with the necessary modules and options. We will compile Nginx with support for SSL, Brotli, and PageSpeed.
Configure build options
./configure \
--with-http_ssl_module \
--add-module=../ngx_brotli \
--add-module=../incubator-pagespeed-ngx \
--with-compatEnsure you replace the paths with the correct locations if you cloned modules elsewhere.
Compile and install
make -j$(nproc)
sudo make installThis process may take a few minutes depending on your server's hardware. Once completed, your custom Nginx build is ready.
Installing the Custom Nginx Build
After compilation, verify the installation by checking the Nginx version:
nginx -vYou should see your custom version. To ensure your system uses this version, replace the default Nginx binary if necessary:
sudo ln -sf /usr/local/nginx/sbin/nginx /usr/sbin/nginxConfiguring Nginx for Optimal Performance
Now, edit your Nginx configuration to enable Brotli compression and optimize settings for speed.
Enable Brotli
Add the following to your nginx.conf inside the http block:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json image/svg+xml;
Configure PageSpeed
Follow Google's documentation to set up the PageSpeed module properly. Typically, you add:
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
Ensure the cache directory exists:
sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R www-data:www-data /var/ngx_pagespeed_cacheVerifying the Installation and Module Activation
Check that Nginx is running with your custom modules:
nginx -V 2>&1 | grep -o with-http_ssl_module
nginx -V 2>&1 | grep -o ngx_brotli
If the output confirms the modules are included, your setup is successful. You can also test Brotli compression by inspecting response headers with browser developer tools or command-line tools like curl.
Troubleshooting Build and Configuration Issues
If you encounter errors during compilation, ensure all dependencies are installed and paths are correct. For configuration issues, review your nginx.conf syntax and module directives. The Nginx error logs can provide valuable insights.
Conclusion
Compiling Nginx from source on Ubuntu 24.04 allows you to customize your web server with advanced modules like Brotli and PageSpeed. At ByteHosting, we recommend this approach for those seeking maximum performance and control over their server environment. With our reliable VPS plans, you can easily deploy and manage your optimized Nginx setup. Follow this guide to enhance your website’s speed and deliver a better experience to your visitors.