Skip to content

Instantly share code, notes, and snippets.

@wall-street-dev
Last active September 12, 2024 12:11
Show Gist options
  • Save wall-street-dev/4302210c45926dd44475708c02717faa to your computer and use it in GitHub Desktop.
Save wall-street-dev/4302210c45926dd44475708c02717faa to your computer and use it in GitHub Desktop.
Compile Google's ngx_brotli modules with your nginx installation
#!/bin/bash
# Helper function for error checking and user-friendly messages
exit_if_fail() {
local exit_code=$?
local message=$1
if [ $exit_code -ne 0 ]; then
echo "Error: $message"
exit $exit_code
fi
}
# Create a temporary directory
TEMP_DIR=$(mktemp -d)
exit_if_fail "Failed to create a temporary directory."
# Install dependencies
echo "Installing dependencies..."
sudo apt install -y git gcc cmake libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libbrotli-dev >/dev/null 2>&1
exit_if_fail "Failed to install required dependencies."
# Extract Nginx version
echo "Extracting Nginx version..."
NGINX_VERSION=$(nginx -v 2>&1 | awk -F '/' '{print $2}' | awk '{print $1}')
exit_if_fail "Failed to extract NGINX version or NGINX is not installed."
# Extract Nginx modules path
echo "Extracting Nginx modules path..."
NGINX_MODULES_PATH=$(nginx -V 2>&1 | grep -oP '(?<=--modules-path=)[^\s]+')
exit_if_fail "Failed to extract the Nginx modules path."
# Change to the temporary directory and confirm change
cd $TEMP_DIR
exit_if_fail "Failed to change to the temporary directory."
# Download and unpack Nginx source code
echo "Downloading Nginx source code..."
wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz >/dev/null 2>&1
exit_if_fail "Failed to download NGINX source code."
echo "Unpacking Nginx source code..."
tar zxvf nginx-$NGINX_VERSION.tar.gz >/dev/null 2>&1
exit_if_fail "Failed to unpack Nginx source code."
# Clone Brotli module from the official Google repository
echo "Cloning ngx_brotli module..."
git clone https://github.com/google/ngx_brotli.git --recursive >/dev/null 2>&1
exit_if_fail "Failed to clone ngx_brotli from the official repository."
# Configure and build Brotli module with Nginx
echo "Configuring and building ngx_brotli module with Nginx..."
cd nginx-$NGINX_VERSION
sudo ./configure --with-compat --add-dynamic-module=$TEMP_DIR/ngx_brotli >/dev/null 2>&1
exit_if_fail "Failed to configure Nginx with the ngx_brotli module."
sudo make modules >/dev/null 2>&1
exit_if_fail "Failed to build ngx_brotli module."
# Copy ready module *.so files to the Nginx modules folder
echo "Copying ngx_brotli modules to Nginx modules path..."
sudo cp objs/ngx_http_brotli_filter_module.so objs/ngx_http_brotli_static_module.so $NGINX_MODULES_PATH
exit_if_fail "Failed to copy ngx_brotli module files to Nginx modules path."
# Change back to the original directory
cd $OLDPWD
# Clean up the temporary directory
echo "Cleaning up temporary files..."
rm -rf $TEMP_DIR
if [ $? -ne 0 ]; then
echo "Warning: Failed to remove the temporary directory."
fi
echo "ngx_brotli module installation completed successfully. Don't forget to restart nginx"
@wall-street-dev
Copy link
Author

These files should be generated as the result of the compilation. What's the compilation error you get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment