Skip to content

Instantly share code, notes, and snippets.

@pujianto
Last active August 10, 2022 04:33
Show Gist options
  • Save pujianto/795e858f11ffc3073a24f60217fcba1a to your computer and use it in GitHub Desktop.
Save pujianto/795e858f11ffc3073a24f60217fcba1a to your computer and use it in GitHub Desktop.
Auto-update Nginx Brotli dynamic modules after apt upgrade on Ubuntu & CentOS https://devopsan.com/how-to-install-auto-upgrade-nginx-brotli-module-on-ubuntu/
# Ubuntu & Debian
# Store this file as /etc/apt/apt.conf.d/100-nginx-post-update
DPkg::Post-Invoke {"/usr/local/bin/ngx_brotli"; };
# CentOS & RHEL
# To enable the post-transaction hook on Yum installation-based (Centos, Fedora < 22, etc),
# one has to install `yum-plugin-post-transaction-actions` package.
# Then add or create the following lines to /etc/yum/post-actions/nginx_update.action
nginx:update:bash /usr/local/bin/ngx_brotli
nginx:install:bash /usr/local/bin/ngx_brotli init
#!/bin/bash
# store this file as /usr/local/bin/ngx_brotli
ngx_brotli() {
trap 'printf "Aborted by user. exiting...\n";exit' INT
ngx_brotli_install() {
local TEMP_DIR_INSTALL
TEMP_DIR_INSTALL="$(mktemp -d -t ngx_brotli.XXXXXXXXXX)"
printf "Temp dir: %s\n" "$TEMP_DIR_INSTALL"
trap 'printf "Cleanup...\n Removing %s\n" "${TEMP_DIR_INSTALL}";rm -rf "${TEMP_DIR_INSTALL}";exit' EXIT
local NGX_BROTLI_GIT_URL="https://github.com/google/ngx_brotli.git"
local NGINX_VERSION
local NGINX_LIB_LOCATION
NGINX_VERSION=$(nginx -v 2>&1 | awk '{print $3}' | sed -e 's#/#-#g')
NGINX_LIB_LOCATION=$(nginx -V 2>&1 | grep "modules-path" | sed -e 's#.*--modules-path=\(.[^ ]*\).*#\1#g')
printf "Preparing brotli modules...\n"
mkdir -p "${TEMP_DIR_INSTALL}/nginx"
printf "Downloading nginx source code... (%s) \n" "${NGINX_VERSION}"
curl -s -L "https://nginx.org/download/${NGINX_VERSION}.tar.gz" | tar -xzC "${TEMP_DIR_INSTALL}/nginx" --strip-components 1
printf "Downloading brotli source code...\n"
git clone "${NGX_BROTLI_GIT_URL}" "${TEMP_DIR_INSTALL}/ngx_brotli"
cd "${TEMP_DIR_INSTALL}/ngx_brotli" && git submodule update --recursive --init
printf "Compiling modules...\n"
cd "${TEMP_DIR_INSTALL}/nginx" && ./configure --with-compat --add-dynamic-module=../ngx_brotli && make modules
printf "Installing modules...\n"
cp -vf "${TEMP_DIR_INSTALL}/nginx/objs/ngx_http_brotli"*.so "${NGINX_LIB_LOCATION}/"
}
local REQUIRED_PROGRAMS=("nginx" "git" "curl" "tar" "make")
check_package() {
command -v "${1}" >/dev/null 2>&1 || {
printf >&2 "%s is required but not installed. Aborting.\n" "${1}"
return 1
}
}
for program in "${REQUIRED_PROGRAMS[@]}"; do
check_package "${program}" || return 1
done
# Pass init argument if you want to install brotli modules for the first time.
if [ "${1}" == "init" ]; then
ngx_brotli_install && return 0
fi
if nginx -t 2>&1 | grep -q brotli; then
printf "Upgrading ngx_brotli...\n"
ngx_brotli_install && systemctl restart nginx.service
else
printf "ngx_brotli is already up to date.\n"
fi
}
return 2>/dev/null || ngx_brotli "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment