Skip to content

Instantly share code, notes, and snippets.

@riskiwah
Forked from kanna5/build_nginx_with_lua.sh
Last active April 7, 2023 06:07
Show Gist options
  • Save riskiwah/b80d61541eea0094027cf31ae86b13f1 to your computer and use it in GitHub Desktop.
Save riskiwah/b80d61541eea0094027cf31ae86b13f1 to your computer and use it in GitHub Desktop.
A simple script to build nginx with lua support
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libgeoip-dev

Location:

  • /lib/systemd/system/nginx.service
#!/bin/sh
# Script to build nginx with lua support.
NGINX_VERSION='1.18.0'
NGX_DEVEL_KIT_VERSION='0.3.1'
LUA_NGINX_MODULE_VERSION='0.10.16rc5'
STREAM_LUA_NGINX_MODULE_VERSION='0.0.8rc3'
# we use openresty's version of luajit here.
LUAJIT_VERSION='2.1-20200102'
LUAJIT_MAJOR_VERSION='2.1'
LUA_RESTY_CORE_VERSION='0.1.18rc4'
LUA_RESTY_LRUCACHE_VERSION='0.10rc1'
NGINX_PREFIX='/usr/local/nginx'
LUAJIT_PREFIX="${NGINX_PREFIX}/luajit-${LUAJIT_VERSION}"
_WORKING_DIR="$(pwd)"
_DOWNLOAD_DIR="${_WORKING_DIR}/downloads"
_BUILD_DIR="${_WORKING_DIR}/build"
_INSTALL_DIR="${_WORKING_DIR}/install"
die() {
[ -n "$*" ] && printf "%b" "\033[31;1m${*}\033[0m\n" >&2
exit 1
}
notice() {
printf "%b" "\033[37;1m${*}\033[0m\n"
}
mkdir -pv "$_DOWNLOAD_DIR" || die "Cannot create download directory"
mkdir -pv "$_BUILD_DIR" || die "Cannot create build directory"
mkdir -pv "$_INSTALL_DIR" || die "Cannot create install directory"
# Download
verify_sha256() {
h="$(sha256sum "$1" 2> /dev/null | head -c64)"
[ "$h" = "$2" ]
return "$?"
}
dl () {
_name="$1"
_url="$2"
_filename="$3"
_sha256="$4"
[ -n "${_sha256}" ] && verify_sha256 "${_filename}" "${_sha256}" && return
rm -fv "${_filename}"
notice "Downloading ${_name}"
wget -c "${_url}" -O "${_filename}" || die "Failed to download ${_name}"
[ -n "${_sha256}" ] && ! verify_sha256 "${_filename}" "${_sha256}" && die "${_name} downloaded but sha256 mismatch."
}
cd "$_DOWNLOAD_DIR" || die
dl nginx "https://github.com/nginx/nginx/archive/release-${NGINX_VERSION}.tar.gz" \
"nginx-${NGINX_VERSION}.tar.gz" \
ea2a54fd81c1c32c0f589800423e14a69161e682c7227354c00e63edf4c3d6e4
dl luajit "https://github.com/openresty/luajit2/archive/v${LUAJIT_VERSION}.tar.gz" \
"luajit-${LUAJIT_VERSION}.tar.gz" \
8a54d5a8b089536b8e62e72dd1b57a684ccb85dfe2160f360ec45b869b446248
dl ngx_devel_kit "https://github.com/vision5/ngx_devel_kit/archive/v${NGX_DEVEL_KIT_VERSION}.tar.gz" \
"ngx_devel_kit-${NGX_DEVEL_KIT_VERSION}.tar.gz" \
0e971105e210d272a497567fa2e2c256f4e39b845a5ba80d373e26ba1abfbd85
dl lua-nginx-module "https://github.com/openresty/lua-nginx-module/archive/v${LUA_NGINX_MODULE_VERSION}.tar.gz" \
"lua-nginx-module-${LUA_NGINX_MODULE_VERSION}.tar.gz" \
3c51ae3299dba35f93924e482842d3617accd51cb80acda8774f7c9d70d0f60b
dl stream-lua-nginx-module "https://github.com/openresty/stream-lua-nginx-module/archive/v${STREAM_LUA_NGINX_MODULE_VERSION}.tar.gz" \
"stream-lua-nginx-module-${STREAM_LUA_NGINX_MODULE_VERSION}.tar.gz" \
fce6214ad4828e75f03151a77334b6e7c2e7da04a8371c4f613ff9960b52919b
dl lua-resty-core "https://github.com/openresty/lua-resty-core/archive/v${LUA_RESTY_CORE_VERSION}.tar.gz" \
"lua-resty-core-${LUA_RESTY_CORE_VERSION}.tar.gz" \
ab5402763d93a3f4318e75462a46f67805818ba5f803db65b90f7b9a215c3e46
dl lua-resty-lrucache "https://github.com/openresty/lua-resty-lrucache/archive/v${LUA_RESTY_LRUCACHE_VERSION}.tar.gz" \
"lua-resty-lrucache-${LUA_RESTY_LRUCACHE_VERSION}.tar.gz" \
f6b57d83a937899f97a98372c1e2631dd1ab8f580fc0ffeac0b27b4d42225a99
# build & install (to temporary dir)
cd "$_BUILD_DIR" || die
notice "Building luajit..."
tar -xf "${_DOWNLOAD_DIR}/luajit-${LUAJIT_VERSION}.tar.gz" || die
cd "luajit2-${LUAJIT_VERSION}" || die
sed -i -E "s|^(export PREFIX=\\s*).*$|\\1${LUAJIT_PREFIX}|g" Makefile || die "failed to patch luajit prefix"
make -j"$(nproc)" && make DESTDIR="${_INSTALL_DIR}" install || die "failed to build luajit"
cd ..
notice "Building nginx..."
tar -xf "${_DOWNLOAD_DIR}/nginx-${NGINX_VERSION}.tar.gz" || die
tar -xf "${_DOWNLOAD_DIR}/ngx_devel_kit-${NGX_DEVEL_KIT_VERSION}.tar.gz" || die
_NGX_DEVEL_KIT_PATH="${_BUILD_DIR}/ngx_devel_kit-${NGX_DEVEL_KIT_VERSION}"
tar -xf "${_DOWNLOAD_DIR}/lua-nginx-module-${LUA_NGINX_MODULE_VERSION}.tar.gz" || die
_LUA_NGINX_MODULE_PATH="${_BUILD_DIR}/lua-nginx-module-${LUA_NGINX_MODULE_VERSION}"
tar -xf "${_DOWNLOAD_DIR}/stream-lua-nginx-module-${STREAM_LUA_NGINX_MODULE_VERSION}.tar.gz" || die
_STREAM_LUA_NGINX_MODULE_PATH="${_BUILD_DIR}/stream-lua-nginx-module-${STREAM_LUA_NGINX_MODULE_VERSION}"
cd "nginx-release-${NGINX_VERSION}" || die
export LUAJIT_LIB="${_INSTALL_DIR}${LUAJIT_PREFIX}/lib"
export LUAJIT_INC="${_INSTALL_DIR}${LUAJIT_PREFIX}/include/luajit-${LUAJIT_MAJOR_VERSION}"
./auto/configure \
--prefix="${NGINX_PREFIX}" \
--conf-path="${NGINX_PREFIX}/conf/nginx.conf" \
--sbin-path="${NGINX_PREFIX}/sbin/nginx" \
--pid-path="${NGINX_PREFIX}/run/nginx.pid" \
--lock-path="${NGINX_PREFIX}/run/nginx.lock" \
--user=www-data \
--group=www-data \
--http-log-path="${NGINX_PREFIX}/logs/access.log" \
--error-log-path=stderr \
--http-client-body-temp-path="${NGINX_PREFIX}/tmp/client-body" \
--http-proxy-temp-path="${NGINX_PREFIX}/tmp/proxy" \
--http-fastcgi-temp-path="${NGINX_PREFIX}/tmp/fastcgi" \
--http-scgi-temp-path="${NGINX_PREFIX}/tmp/scgi" \
--http-uwsgi-temp-path="${NGINX_PREFIX}/tmp/uwsgi" \
--with-compat \
--with-debug \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_degradation_module \
--with-http_flv_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-pcre-jit \
--with-stream \
--with-stream_geoip_module \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-threads \
--with-ld-opt="-Wl,-z,origin -Wl,-rpath,'\$\$ORIGIN'/../luajit-${LUAJIT_VERSION}/lib" \
--add-module="${_NGX_DEVEL_KIT_PATH}" \
--add-module="${_LUA_NGINX_MODULE_PATH}" \
--add-module="${_STREAM_LUA_NGINX_MODULE_PATH}" \
&& make -j"$(nproc)" && make DESTDIR="${_INSTALL_DIR}" install \
|| die "failed to build nginx"
strip -s "${_INSTALL_DIR}${NGINX_PREFIX}/sbin/nginx"
cd ..
# install essential lua modules (required by lua-nginx-module)
install_lua_module()
{
_name=$1
_version=$2
tar -xf "${_DOWNLOAD_DIR}/${_name}-${_version}.tar.gz" || die
cd "${_name}-${_version}" || die
LUA_VERSION="5.1" PREFIX="${LUAJIT_PREFIX}" make DESTDIR="${_INSTALL_DIR}" install \
|| die "failed to install lua module ${_name}"
cd ..
}
install_lua_module lua-resty-core "${LUA_RESTY_CORE_VERSION}"
install_lua_module lua-resty-lrucache "${LUA_RESTY_LRUCACHE_VERSION}"
# patch nginx.conf to include lua_package_path
cd "${_INSTALL_DIR}${NGINX_PREFIX}/conf" || die
patch -p0 << EOF
--- nginx.conf
+++ nginx.conf
@@ -14,7 +14,13 @@
}
+stream {
+ lua_package_path '${LUAJIT_PREFIX}/lib/lua/5.1/?.lua;;';
+}
+
http {
+ lua_package_path '${LUAJIT_PREFIX}/lib/lua/5.1/?.lua;;';
+
include mime.types;
default_type application/octet-stream;
EOF
notice "Build complete. Now you can manually move ${_INSTALL_DIR}${NGINX_PREFIX} to ${NGINX_PREFIX}"
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment