Skip to content

Instantly share code, notes, and snippets.

@kanna5
Last active July 4, 2024 11:11
Show Gist options
  • Save kanna5/e0b84e1034742ca6757d6719b8974f5b to your computer and use it in GitHub Desktop.
Save kanna5/e0b84e1034742ca6757d6719b8974f5b to your computer and use it in GitHub Desktop.
A simple script to build nginx with lua support
#!/bin/sh
# Script to build nginx with lua support.
# XXX: The resulting binary might work, but it was not thoroughly tested. Use it at your
# own risk. If you really need nginx with LUA in production, please consider using
# OpenResty instead.
# On Debian 12, install these build dependencies: libpcre3-dev libgeoip-dev libssl-dev zlib1g-dev build-essential
NGINX_VERSION='1.24.0'
NGX_DEVEL_KIT_VERSION='0.3.3'
LUA_NGINX_MODULE_VERSION='0.10.26'
STREAM_LUA_NGINX_MODULE_VERSION='0.0.14'
# We use openresty's version of luajit here.
LUAJIT_VERSION='2.1-20240626'
LUAJIT_MAJOR_VERSION='2.1'
LUA_RESTY_CORE_VERSION='0.1.28'
LUA_RESTY_LRUCACHE_VERSION='0.13'
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"
}
# Build with multiple threads
nprocs=$(nproc) || nprocs=1
notice "Building with $nprocs threads."
export MAKEFLAGS="$MAKEFLAGS -j $nprocs"
# Create temporary directories
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}"
curl --proto '=https' --tlsv1.3 -Lf "${_url}" > "${_filename}"
# 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" \
c3fa8604fd40a13653fbeb67c99bf342751c6d08c442a566a527e18df47f7114
dl luajit "https://github.com/openresty/luajit2/archive/v${LUAJIT_VERSION}.tar.gz" \
"luajit-${LUAJIT_VERSION}.tar.gz" \
1e53822a1105df216b9657ccb0293a152ac5afd875abc848453bfa353ca8181b
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" \
faa2fcd5168b10764d35081356511d5f84db5c526a1aa4b6add2db94b6853b2b
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" \
a75983287a2bdc5e964ace56a51b215dc2ec996639d4916cd393d6ebba94b565
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" \
8e2ff6ad5f91127da3c01757e7e654f1addf9769450d9159601d2cc153953c47
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" \
62230dee287edcabb2dcb9c3b44ad162a6cc7ad2b8f508bc52e592f0137aa6a1
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" \
573184006b98ccee2594b0d134fa4d05e5d2afd5141cbad315051ccf7e9b6403
# 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 && 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 && make DESTDIR="${_INSTALL_DIR}" install \
|| die "failed to build nginx"
strip -s "${_INSTALL_DIR}${NGINX_PREFIX}/sbin/nginx" \
"${_INSTALL_DIR}${LUAJIT_PREFIX}/bin/luajit" \
"${_INSTALL_DIR}${LUAJIT_PREFIX}/lib/"*.so
# Some files are not required at run time
rm -rf "${_INSTALL_DIR}${LUAJIT_PREFIX}/lib/"*.a \
"${_INSTALL_DIR}${LUAJIT_PREFIX}/lib/pkgconfig"
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}"
notice "Remember to run \`chown root\` on \"${NGINX_PREFIX}\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment