Instantly share code, notes, and snippets.

@im-kulikov /build.sh Secret
Last active Mar 27, 2017

Embed
What would you like to do?
Сборка nginx + passenger + pagespeed + sticky + lua + lua-imagick + ngx_devkit + ngx_misc + srcache + memc + enhaced_memcache
#!/bin/bash
cd /tmp
mkdir /tmp/nginx
echo
echo 'Clean old-builds:'
find /tmp/nginx* -maxdepth 0 | xargs rm -rf --
find /tmp -type f -name '*.deb' -exec rm {} \;
echo
echo "Resolve PageSpeed Version:"
echo
NGX_PAGESPEED_VER=`git ls-remote --tags "https://github.com/pagespeed/ngx_pagespeed.git" | sort -Vk2 | grep -v '\^{}$' | tail -n1 | cut -d '/' -f 3`
echo "NGX PAGESPEED Version: $NGX_PAGESPEED_VER"
echo $NGX_PAGESPEED_VER > /tmp/pagespeed_version
echo
echo "Resolve Nginx Version:"
NGX_VER=`git ls-remote --tags "https://github.com/nginx/nginx.git" | sort -Vk2 | grep -v '\^{}$' | tail -n1 | cut -d '/' -f 3`
NGX_LOCAL=`nginx -v 2>&1 | awk '{match($0,/[0-9\.]+/,a)}END{print a[0]}'`
NGX_REMOTE=`echo $NGX_VER | awk '{match($0,/[0-9\.]+/,a)}END{print a[0]}'`
echo
echo "Nginx Remote Version: $NGX_REMOTE"
echo "Nginx Local Version: $NGX_LOCAL"
echo $NGX_REMOTE > /tmp/nginx_version
echo
echo "Resolve Passenger Version:"
PASSENGER_VER=`gem list -r passenger | grep '^passenger' | head -n 1 | awk '{match($0,/[0-9\.]+/,a)}END{print a[0]}'`
PASSENGER_LOCAL=`gem list -l passenger | grep '^passenger' | head -n 1 | awk '{match($0,/[0-9\.]+/,a)}END{print a[0]}'`
echo
echo "Passenger Last Version: $PASSENGER_VER"
echo "Passenger Local Version: $PASSENGER_LOCAL"
#if [ "$NGX_REMOTE" == "$NGX_LOCAL" ] && [ "$PASSENGER_VER" == "$PASSENGER_LOCAL" ]; then
# echo
# echo "Newest version of Nginx+Passenger installed $NGX_VER [$PASSENGER_LOCAL]"
# exit 0
#fi
CHANGE_USER=yes
REMOVE_DAV=yes
REMOVE_FLV=yes
REMOVE_MP4=yes
REMOVE_MAIL=yes
ADD_GEOIP=no
rm /etc/apt/sources.list.d/nginx*
RELEASE=`lsb_release -cs`
DISTRIBUTE=`lsb_release -si | tr '[:upper:]' '[:lower:]'`
MAINLINE=`curl -s -L http://nginx.org/packages/mainline/$DISTRIBUTE/pool/nginx/n/nginx/ | awk '{gsub(/<[^>]*>/, " ");$1=$1}1' | grep 'nginx_' | cut -d ' ' -f 1 | awk '{gsub(/[^0-9.]+/," "); $1=$1}1' | cut -d ' ' -f 1 | uniq | grep '[^.]$' | sort -V | tail -1`
STABLE=`curl -s -L http://nginx.org/packages/$DISTRIBUTE/pool/nginx/n/nginx/ | awk '{gsub(/<[^>]*>/, " ");$1=$1}1' | grep 'nginx_' | cut -d ' ' -f 1 | awk '{gsub(/[^0-9.]+/," "); $1=$1}1' | cut -d ' ' -f 1 | uniq | grep '[^.]$' | sort -V | tail -1`
VER_LT=`echo -e "$MAINLINE\n$STABLE" | sort -V | tail -1`
echo ""
if [ "$STABLE" == "$VER_LT" ]; then
echo "Use stable version: $STABLE"
echo "deb http://nginx.org/packages/$DISTRIBUTE/ $RELEASE nginx" > /etc/apt/sources.list.d/nginx.list
echo "deb-src http://nginx.org/packages/$DISTRIBUTE/ $RELEASE nginx" >> /etc/apt/sources.list.d/nginx.list
else
echo "Use mainline version: $MAINLINE"
echo "deb http://nginx.org/packages/mainline/$DISTRIBUTE/ $RELEASE nginx" > /etc/apt/sources.list.d/nginx.list
echo "deb-src http://nginx.org/packages/mainline/$DISTRIBUTE/ $RELEASE nginx" >> /etc/apt/sources.list.d/nginx.list
fi
curl -s http://nginx.org/keys/nginx_signing.key | apt-key add -- > /dev/null 2>&1
if [ "$PASSENGER_VER" == "$PASSENGER_LOCAL" ]; then
echo "Has latest passenger."
else
echo "Install Passenger Phusion:"
gem install passenger
fi
#echo "Add unstable repo for libev4 4.22:"
echo "Update & upgrade apt-get:"
apt-get -qq update >> /dev/null 2>&1
apt-get -qq upgrade -y >> /dev/null 2>&1
echo "Install build-tools && build dependencies..."
apt-get -qq install libcurl4-openssl-dev liblua5.2-dev libluajit-5.1-dev libluajit-5.1-common lua5.1 liblua5.1-0 liblua5.1-0-dev libreadline-dev libncurses5-dev libpcre3-dev -y
apt-get -qq install dpkg-dev libuv-dev build-essential zlib1g-dev libpcre3 libpcre3-dev git libevent-dev unzip -y >> /dev/null 2>&1
apt-get -qq install cmake cmake-data libmagickcore-dev libmagickwand-dev -y >> /dev/null 2>&1
apt-get -qq build-dep nginx -y >> /dev/null 2>&1
echo "Get nginx sources..."
apt-get -qq source nginx >> /dev/null 2>&1
NGX_VER=`dpkg -s nginx | grep Version | cut -d ' ' -f 2 | cut -d \- -f 1`
PSOL_VER=$(echo $NGX_PAGESPEED_VER | cut -d \- -f 1 | cut -d 'v' -f 2)
NGINX_VER=$(cd /tmp; find ./nginx* -maxdepth 0 -type d | sed "s|^\./||")
NGINX_VER_NUM=$(echo $NGINX_VER | cut -d \- -f 2)
NGINX_BUILD_DIR=/tmp/$NGINX_VER
PASSENGER_DIR="$(passenger-config --nginx-addon-dir)"
NGX_PASSENGER_VER=`passenger -v | cut -d ' ' -f 3`
#export USE_VENDORED_LIBEV=no
#export USE_VENDORED_LIBEV=no
echo "Update build rules..."
sed -ie 's/--user=nginx/--user=www-data/g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--group=nginx/--group=www-data/g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-http_dav_module / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-http_flv_module / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-http_mp4_module / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-mail / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-debug / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-mail_ssl_module / /g' $NGINX_BUILD_DIR/debian/rules
# Remove XSLT:
sed -ie 's/--http-xslt-filter / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-http_xslt_module=dynamic / /g' $NGINX_BUILD_DIR/debian/rules
# Remove image_filter:
sed -ie 's/--with-http_image_filter_module=dynamic / /g' $NGINX_BUILD_DIR/debian/rules
# Remove --with-mail
sed -ie 's/--with-mail=dynamic / /g' $NGINX_BUILD_DIR/debian/rules
# Remove...
sed -ie 's/http-headers-more-filter / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--with-http_geoip_module=dynamic / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--add-dynamic-module=$(MODULESDIR)\/nginx-dav-ext-module / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--add-dynamic-module=$(MODULESDIR)\/nginx-auth-pam / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/--add-dynamic-module=$(MODULESDIR)\/ngx-fancyindex / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/http-image-filter / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/http-dav-ext / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/http-geoip / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/http-xslt-filter / /g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's/http-auth-pam / /g' $NGINX_BUILD_DIR/debian/rules
mkdir $NGINX_BUILD_DIR/modules
echo "Get pagespeed..."
wget https://github.com/pagespeed/ngx_pagespeed/archive/$NGX_PAGESPEED_VER.zip -O $NGINX_BUILD_DIR/modules/release-$NGX_PAGESPEED_VER.zip >> /dev/null 2>&1
unzip $NGINX_BUILD_DIR/modules/release-$NGX_PAGESPEED_VER.zip -d $NGINX_BUILD_DIR/modules >> /dev/null 2>&1
rm -f $NGINX_BUILD_DIR/modules/release-$NGX_PAGESPEED_VER.zip >> /dev/null 2>&1
NGX_PAGESPEED_V=`echo $NGX_PAGESPEED_VER | cut -d 'v' -f 2`
mv $NGINX_BUILD_DIR/modules/ngx_pagespeed-$NGX_PAGESPEED_V $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER >> /dev/null 2>&1
echo "Get PSOL..."
wget https://dl.google.com/dl/page-speed/psol/$PSOL_VER-x64.tar.gz -O $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER/$PSOL_VER-x64.tar.gz >> /dev/null 2>&1
tar -C $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER -xzf $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER/$PSOL_VER-x64.tar.gz >> /dev/null 2>&1
rm -f $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER/$PSOL_VER-x64.tar.gz >> /dev/null 2>&1
echo "Get Sticky..."
STICKY_VER=`git ls-remote --tags https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng.git | sed 's/tags\/v//g' | sort -Vk2 | grep -v '\^{}$' | tail -n1 | cut -d '/' -f 3`
curl -sfSL https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/${STICKY_VER}.zip -o /tmp/sticky.zip
unzip -j /tmp/sticky.zip -d $NGINX_BUILD_DIR/modules/sticky >> /dev/null 2>&1
sed -i '/#include <ngx_sha1.h>/a#include <openssl/md5.h>' $NGINX_BUILD_DIR/modules/sticky/ngx_http_sticky_misc.c
sed -i '/#include <ngx_sha1.h>/a#include <openssl/sha.h>' $NGINX_BUILD_DIR/modules/sticky/ngx_http_sticky_misc.c
ln -s $NGINX_BUILD_DIR/modules/ngx_pagespeed-release-$NGX_PAGESPEED_VER $NGINX_BUILD_DIR/modules/pagespeed
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/lua-nginx-module /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/ngx_misc /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/srcache /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/memc /g' $NGINX_BUILD_DIR/debian/rules
# sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/enhaced_memcache /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/sticky /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/pagespeed /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=PASSENGER_DIR /g' $NGINX_BUILD_DIR/debian/rules
sed -i 's/--with-file-aio/--with-file-aio --add-module=modules\/ngx_devel_kit /g' $NGINX_BUILD_DIR/debian/rules
# Replace module path:
sed -ie 's|\--add-module=modules|'--add-module=$NGINX_BUILD_DIR/modules'|g' $NGINX_BUILD_DIR/debian/rules
sed -ie 's|\--add-module=PASSENGER_DIR|'--add-module=$PASSENGER_DIR'|g' $NGINX_BUILD_DIR/debian/rules
# Passenger:
echo "Passenger..."
cd $(passenger-config --root)
export TRACE=1
sed -ie 's/CACHING=false/RELEASE=yes/g' $PASSENGER_DIR/config
sed -ie 's/BUILD_DIRECTIVE="nginx"/BUILD_DIRECTIVE="nginx:clean nginx"/g' $PASSENGER_DIR/config
rake clean nginx:clean clobber > /dev/null 2>&1
passenger-config build-native-support > /dev/null 2>&1
passenger-config download-agent > /dev/null 2>&1
passenger-config compile-agent > /dev/null 2>&1
# Lua:
echo 'Lua...'
cd $NGINX_BUILD_DIR/modules
git clone https://github.com/openresty/lua-nginx-module ./lua-nginx-module > /dev/null 2>&1
cd $NGINX_BUILD_DIR/modules/lua-nginx-module
git apply <(curl -sfSL https://patch-diff.githubusercontent.com/raw/openresty/lua-nginx-module/pull/1017.patch) >> /dev/null 2>&1
cd $NGINX_BUILD_DIR/modules
export LUAJIT_LIB=/usr/lib/x86_64-linux-gnu/
export LUAJIT_INC=/usr/include/luajit-2.0
# NGX-DevKit:
echo "NGX-DevKit..."
cd $NGINX_BUILD_DIR/modules
git clone https://github.com/simpl/ngx_devel_kit ./ngx_devel_kit >> /dev/null 2>&1
# NGX-Misc:
echo "NGX-Misc..."
cd $NGINX_BUILD_DIR/modules
git clone https://github.com/openresty/set-misc-nginx-module.git ./ngx_misc >> /dev/null 2>&1
# SRCache:
echo "SRCache..."
cd $NGINX_BUILD_DIR/modules
git clone https://github.com/openresty/srcache-nginx-module.git ./srcache >> /dev/null 2>&1
# Memc
echo "Memc..."
cd $NGINX_BUILD_DIR/modules
git clone https://github.com/openresty/memc-nginx-module.git ./memc >> /dev/null 2>&1
# Enhaced memcache:
# echo "Enhaced memcache..."
# cd $NGINX_BUILD_DIR/modules
# git clone https://github.com/bpaquet/ngx_http_enhanced_memcached_module.git ./enhaced_memcache >> /dev/null 2>&1
# Lua-imagick:
echo "LUA-imagick..."
git clone https://github.com/isage/lua-imagick.git /tmp/lua-imagick >> /dev/null 2>&1
cd /tmp/lua-imagick
mkdir build
cd build
cmake .. >> /dev/null 2>&1
make >> /dev/null 2>&1
make install >> /dev/null 2>&1
cd /tmp
rm -rf '/tmp/lua-imagick' >> /dev/null 2>&1
echo "PSOL_VER: $PSOL_VER"
echo "NGINX_VER: $NGINX_VER"
echo "NGINX_VER_NUM: $NGINX_VER_NUM"
echo "NGINX_BUILD_DIR: $NGINX_BUILD_DIR"
echo "NGX_PAGESPEED_VER: $NGX_PAGESPEED_VER"
echo ""
echo "Build..."
echo ""
cd $NGINX_BUILD_DIR
dpkg-buildpackage -b -d -j5
dpkg -i $(ls /tmp/nginx_*.deb | head -n 1) >> /dev/null 2>&1
echo "Hold packages nginx nginx-common..."
apt-mark hold nginx >> /dev/null 2>&1
apt-mark hold nginx-common >> /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment