#!/bin/bash ################ # To be run after an nginx upgrade.(it will automatically get the right version of the GeoIP2 NGINX module) # Build against a specific nginx.org NGINX version (nginx -v). # Intended to run inside an Ubuntu container # matching the target distribution. # Compiles GeoIP2 as a dynamic module (.so file) # # Must be run as root if ! id | grep -q "uid=0(root)"; then echo "Must be run as root" exit 1 fi # Make executable ## # cd /opt/nginx-upgrade # chmod +x nginx_geoip_update.sh ## Run ## # ./nginx_geoip_update.sh ## cd /opt/ if [ ! -d /opt/nginx-upgrade ]; then mkdir -p /opt/nginx-upgrade fi cd /opt/nginx-upgrade # Set log file scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" logname=`basename "$0"` logname+=".log" logfile=$scriptdir/$logname touch "$logfile" # Functions logger(){ echo "$(date): $@" >> $logfile echo "## $@" } logger "Starting script" # Best practice settings for running bash scripts: # Exit the script when an error is encountered set -o errexit # Exit the script when a pipe operation fails set -o pipefail # Exit the script when there are undeclared variables set -o nounset # Uncomment this to see a log to the screen of each command run in the script # set -o xtrace # Credit: nginx.org 2020-10-05 https://gist.github.com/nginx-gists/bdc7da70b124c4f3e472970c7826cccc # Back up Your Current Configuration cp -r /etc/nginx /etc/nginx_backup # Prerequisites apt-get -qq update apt-get -qq install -y \ libpcre2 \ libpcre2-dev \ libssl-dev \ zlib1g-dev \ libmaxminddb0 \ libmaxminddb-dev \ mmdb-bin # Install the module ## Get installed Nginx version number NGINX_VER=$(nginx -v |& sed 's/nginx version: nginx\///' | sed 's/\s.*$//') # Create a workspace and download the exact matching Nginx ${NGINX_VER} source code: cd /opt/ if [ ! -d /opt/nginx-upgrade ]; then mkdir -p /opt/nginx-upgrade fi cd /opt/nginx-upgrade if [ ! -f /opt/nginx-upgrade/nginx-${NGINX_VER}/configure ]; then curl -O https://nginx.org/download/nginx-${NGINX_VER}.tar.gz tar -zxf nginx-${NGINX_VER}.tar.gz logger "nginx-${NGINX_VER} source downloaded" fi # Clone Module Repositories # Download the source code for the Brotli GeoIP2 ModSecurity-nginx modules into your workspace. # delete previous clone directory if [ -d /opt/nginx-upgrade/ngx_http_geoip2_module ]; then rm -r /opt/nginx-upgrade/ngx_http_geoip2_module fi # Clone GeoIP2 module git clone https://github.com/leev/ngx_http_geoip2_module.git cd ngx_http_geoip2_module git submodule update --init --recursive cd /opt/nginx-upgrade logger "GeoIP2 module cloned" # Configure and Compile Dynamic Module cd /opt/nginx-upgrade/nginx-${NGINX_VER} # Remove previous configs if [ -d /opt/nginx-upgrade/nginx-${NGINX_VER}/objs ]; then make clean logger "make clean invoked" fi # Run configure with the --add-dynamic-module flags appended to your configuration: ./configure --with-compat --add-dynamic-module=../ngx_http_geoip2_module logger "GeoIP2 dynamic module completed" # Compile only the modules: make modules logger "GeoIP2 module compiled" # Copy the freshly compiled .so files into your system’s Nginx modules directory: if [ ! -d /etc/nginx/modules/ ]; then mkdir -p /etc/nginx/modules fi cp objs/ngx_http_geoip2_module.so /etc/nginx/modules/ logger "GeoIP2 dynamic module copied to /etc/nginx/modules/" # Verify # nginx -t if [ "$(command nginx -t) | grep 'ok'" ]; then logger "Nginx configuration ok, reloading.." systemctl reload nginx else logger "Nginx errors" fi