Skip to content

Instantly share code, notes, and snippets.

@concatime
Last active July 12, 2018 13:27
Show Gist options
  • Save concatime/04127487894c522cb240e0f95bdd92e7 to your computer and use it in GitHub Desktop.
Save concatime/04127487894c522cb240e0f95bdd92e7 to your computer and use it in GitHub Desktop.
Pretty Useless Bleeding Edge OpenResty — Latest NGiИX, OpenSSL, LuaJIT & PCRE + LuaRocks & Lapis webframework » bash <(curl -s https://gist.githubusercontent.com/concatime/04127487894c522cb240e0f95bdd92e7/raw)
OPENSSL=openssl-1.0.2o
OPENRESTY=openresty-1.13.6.2
LUAROCKS=2.4.4
LUAJIT=LuaJIT-2.1.0-beta3
NGX=nginx-1.15.1
PCRE=8.42
# OpenSSL 1.1 & PCRE2 (10.0) aren't _yet_ supported.
# https://github.com/openresty/lua-nginx-module/issues/852
# https://stackoverflow.com/a/34795745
# LuaJIT is already well updated in OpenRssty
# See https://github.com/openresty/openresty/issues/261
CFLAGS+=-pipe
MAKEFLAGS+=-j`nproc`
export CFLAGS MAKEFLAGS
set -e
# Check if c compiler, otherwse exit due to previous line
cc --version
# https://serverfault.com/a/177764
function ubuntu {
apt-get update
apt-get upgrade --yes
apt-get autoremove --yes
apt-get install git -y wget make unzip --no-install-recommends
}
export -f ubuntu
. /etc/os-release
if [ $ID == ubuntu ]
then su --command ubuntu
fi
pushd $(mktemp --directory)
for k in www.openssl.org/source/$OPENSSL superb-sea2.dl.sourceforge.net/project/pcre/pcre/$PCRE/pcre-$PCRE
do curl https://$k.tar.gz --silent | tar xz &
done
git clone git://github.com/google/ngx_brotli --recursive --quiet &
wget https://openresty.org/download/$OPENRESTY.tar.gz --quiet --no-clobber --show-progress
tar xf $OPENRESTY.tar.gz
pushd $OPENRESTY
cd bundle
rm nginx-* ngx_lua-* echo-nginx-module-* --force --recursive
# Use -0 only to fool the installer
# See https://github.com/openresty/lua-nginx-module/pull/1017
git clone git://github.com/openresty/lua-nginx-module ngx_lua-0 --quiet &
# See https://github.com/openresty/echo-nginx-module/issues/64
git clone git://github.com/openresty/echo-nginx-module echo-nginx-module-0 --quiet &
curl https://nginx.org/download/$NGX.tar.gz --silent | tar xz &
# See https://github.com/openresty/openresty/issues/259
echo ' ~ Patching redis-nginx-module for NGINX'
cd redis-nginx-module-0.3.7
sed --in-place '1008i #if (nginx_version < 1011006)' ngx_http_redis_module.c
sed -i '1015i #endif' ngx_http_redis_module.c
cd ../..
echo ' ~ Downloading dependencies'
wait
echo ' ~ Configuring & installing OpenResty'
./configure \
--prefix=/opt/openresty \
--add-module=../ngx_brotli \
--with-openssl=../$OPENSSL \
--with-pcre=../pcre-$PCRE \
--with-pcre-jit \
--with-stream \
--with-threads \
--with-file-aio \
--with-http_v2_module \
--with-http_ssl_module \
--without-http_gzip_module
make
sudo make install
popd
pushd /opt/openresty/bin
sudo ln ${LUAJIT,,} ../luajit/bin/luajit --symbolic
sudo ln ../nginx/sbin/nginx nginx --symbolic
sudo ln nginx openresty --force --symbolic
popd
pushd /etc/systemd/system
sudo curl https://gist.githubusercontent.com/concatime/04127487894c522cb240e0f95bdd92e7/raw/openresty.service --remote-name
popd
curl https://luarocks.github.io/luarocks/releases/$LUAROCKS.tar.gz | tar xz
pushd $LUAROCKS
# See https://github.com/luarocks/luarocks/issues/267
./configure \
--prefix=/opt/openresty/luajit \
--with-lua=/opt/openresty/luajit \
--lua-suffix=jit \
--with-lua-include=/opt/openresty/luajit/include/luajit-2.1
sudo make bootstrap
popd
pushd $OPENSSL
make distclean
./config shared
make
popd
git clone git://github.com/openresty/lua-cjson
pushd lua-cjson
# See https://github.com/leafo/lapis/issues/539
sudo /opt/openresty/luajit/bin/luarocks make
popd
OPENSSL=`pwd`/$OPENSSL
pushd /opt/openresty/luajit/bin
sudo ./luarocks install luasec OPENSSL_DIR=$OPENSSL OPENSSL_LIBDIR=$OPENSSL
sudo ./luarocks install luacrypto OPENSSL_DIR=$OPENSSL OPENSSL_LIBDIR=$OPENSSL
sudo ./luarocks install lapis
popd
rm `pwd` --recursive
popd
echo …done
# Token from https://www.digitalocean.com/community/tutorials/how-to-use-the-openresty-web-framework-for-nginx-on-ubuntu-16-04
#
# Stop dance for OpenResty
# A modification of the Nginx systemd script
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the Nginx process.
# If, after 5s (--retry QUIT/5) OpenResty is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if OpenResty is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# Nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A dynamic web platform based on Nginx and LuaJIT.
After=network.target
[Service]
Type=forking
PIDFile=/opt/openresty/nginx/logs/nginx.pid
ExecStartPre=/opt/openresty/bin/openresty -g 'daemon on; master_process on;' -t -q
ExecStart=/opt/openresty/bin/openresty -g 'daemon on; master_process on;'
ExecReload=/opt/openresty/bin/openresty -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /opt/openresty/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment