150 lines
4.9 KiB
Plaintext
150 lines
4.9 KiB
Plaintext
# ==========
|
|
# Build dynamic Nginx module Brotli
|
|
# ==========
|
|
|
|
backup_date="$(date +%Y-%m-%d-%H%M)"
|
|
|
|
builddir=/usr/local/src/nginx
|
|
mkdir -p /usr/local/src/nginx
|
|
cd "${builddir}"
|
|
|
|
# Install needed development packages if not yet installed in the system
|
|
#
|
|
apt -y install git libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
|
|
|
|
|
|
# Find installed version of nginx
|
|
#
|
|
# nginx -v
|
|
#
|
|
# output like:
|
|
#
|
|
# nginx version: nginx/1.18.0
|
|
#
|
|
# For automated detection of currently installed NGINX version (not to be used
|
|
# for auto-updating, see hooks in post), use:
|
|
#
|
|
ngver=$(nginx -v 2>&1 | grep -o '[0-9\.]*')
|
|
|
|
# Get configure parameters of installed NGINX. Not needed if NGINX was configured '--with-compat'.
|
|
# Uncomment one of the lines below if the script sucessfully builds modules but NGINX throws
|
|
# a "not binary compatible" error.
|
|
confparams=$(nginx -V 2>&1 | grep -o -- '--prefix='.* | sed -e 's/ --add-dynamic-module=[^[:space:]]*//g')
|
|
[[ -z "${confparams}" ]] && confparams=$(nginx -V 2>&1 | grep -o -- '--[^with]'.* \
|
|
| sed -e 's/ --add-dynamic-module=[^[:space:]]*//g')
|
|
[[ -z "${confparams}" ]] && confparams=$(nginx -V 2>&1 | grep -- '--' | sed "s/.*' //" \
|
|
| sed -e 's/ --add-dynamic-module=[^[:space:]]*//g')
|
|
[[ -z "${confparams}" ]] && confparams=$(nginx -V 2>&1 | grep -o -- "--prefix='.*'$" \
|
|
| sed -e 's/ --add-dynamic-module=[^[:space:]]*//g')
|
|
|
|
|
|
# To automatically select NGINX modules directory:
|
|
#
|
|
[ -d /usr/share/nginx/modules ] && moddir=/usr/share/nginx/modules
|
|
[ -d $(nginx -V 2>&1 | grep -o 'prefix=[^ ]*' | sed 's/prefix=//')/modules ] && moddir="$(nginx -V 2>&1 | grep -o 'prefix=[^ ]*' | sed 's/prefix=//')/modules"
|
|
[ -d $(nginx -V 2>&1 | grep -o 'modules-path=[^ ]*' | sed 's/modules-path=//') ] && moddir="$(nginx -V 2>&1 | grep -o 'modules-path=[^ ]*' | sed 's/modules-path=//')"
|
|
|
|
[ "${moddir}" ] || { echo '!! missing modules directory, exiting...'; exit 1; }
|
|
|
|
|
|
# ---
|
|
# Download and unpack NGINX
|
|
# ---
|
|
[ -d "nginx-${ngver}" ] && mv "nginx-${ngver}" "nginx-${ngver}.${backup_date}"
|
|
wget https://nginx.org/download/nginx-${ngver}.tar.gz
|
|
|
|
# Umpack nginx souce code archive
|
|
gunzip < nginx-${ngver}.tar.gz | tar -xf -
|
|
|
|
# Remove Downloadwed archive
|
|
rm nginx-${ngver}.tar.gz
|
|
|
|
|
|
# ---
|
|
# Download, initialize, and make Brotli dynamic modules
|
|
# ---
|
|
cd /usr/local/src
|
|
[ -d "ngx_brotli" ] && mv ngx_brotli ngx_brotli.${backup_date}
|
|
git clone https://github.com/google/ngx_brotli.git
|
|
|
|
cd ngx_brotli
|
|
|
|
# download (clone) also submodules
|
|
#
|
|
#git submodule update --init --recursive
|
|
git submodule update --init
|
|
|
|
|
|
|
|
cd /usr/local/src/nginx/nginx-${ngver}
|
|
|
|
if [[ -z "${confparams}" ]] ; then
|
|
nice -n 19 ionice -c 3 ./configure --with-compat --add-dynamic-module=/usr/local/src/ngx_brotli
|
|
else
|
|
nice -n 19 ionice -c 3 ./configure --add-dynamic-module=/usr/local/src/ngx_brotli ${confparams}
|
|
fi
|
|
|
|
# Build modules
|
|
#
|
|
nice -n 19 ionice -c 3 make modules
|
|
|
|
|
|
# Replace Brotli in modules directory
|
|
#
|
|
[ -f "${moddir}/ngx_http_brotli_filter_module.so" ] && mv "${moddir}/ngx_http_brotli_filter_module.so" "${moddir}/ngx_http_brotli_filter_module.${backup_date}"
|
|
cp -a "/usr/local/src/nginx/nginx-${ngver}/objs/ngx_http_brotli_filter_module.so" \
|
|
"${moddir}/ngx_http_brotli_filter_module.so"
|
|
|
|
|
|
[ -f "${moddir}/ngx_http_brotli_static_module.so" ] && mv "${moddir}/ngx_http_brotli_static_module.so" "${moddir}/ngx_http_brotli_static_module.so.${backup_date}"
|
|
cp -a "/usr/local/src/nginx/nginx-${ngver}/objs//ngx_http_brotli_static_module.so" \
|
|
"${moddir}/ngx_http_brotli_static_module.so"
|
|
|
|
# cp objs/*.so "${moddir}/"
|
|
|
|
chmod 644 "${moddir}/ngx_http_brotli_filter_module.so"
|
|
chmod 644 "${moddir}/ngx_http_brotli_static_module.so"
|
|
|
|
|
|
# Create load_module statements for the two brotli modules
|
|
#
|
|
cat <<EOF > /etc/nginx/modules-available/http-brotli-filter-module.conf
|
|
load_module ${moddir}/ngx_http_brotli_filter_module.so;
|
|
EOF
|
|
|
|
cat <<EOF > /etc/nginx/modules-available/http-brotli-static-module.conf
|
|
load_module ${moddir}/ngx_http_brotli_static_module.so;
|
|
EOF
|
|
|
|
|
|
# Eanable Modules
|
|
#
|
|
ln -s ../modules-available/http-brotli-filter-module.conf /etc/nginx/modules-enabled/60-http-brotli-filter-module.conf
|
|
ln -s ../modules-available/http-brotli-static-module.conf /etc/nginx/modules-enabled/60-http-brotli-static-module.conf
|
|
|
|
|
|
# Configure Brotli
|
|
#
|
|
cat <<EOF > /etc/nginx/conf.d/http-brotli.conf
|
|
##
|
|
# Brotli Settings
|
|
##
|
|
|
|
brotli on;
|
|
brotli_comp_level 6;
|
|
brotli_static on;
|
|
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;
|
|
|
|
# brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
|
EOF
|
|
|
|
# Verify if all is fine
|
|
#
|
|
nginx -t
|
|
|
|
|
|
# Restart Nginx webservice
|
|
#
|
|
systemctl restart nginx.service
|
|
|