Last active
December 11, 2024 13:21
-
-
Save kanna5/e0b84e1034742ca6757d6719b8974f5b to your computer and use it in GitHub Desktop.
A simple script to build nginx with lua support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.26.2' | |
NGX_DEVEL_KIT_VERSION='0.3.3' | |
LUA_NGINX_MODULE_VERSION='0.10.27' | |
STREAM_LUA_NGINX_MODULE_VERSION='0.0.15' | |
# We use openresty's version of luajit here. | |
LUAJIT_VERSION='2.1-20241113' | |
LUAJIT_MAJOR_VERSION='2.1' | |
LUA_RESTY_CORE_VERSION='0.1.30' | |
LUA_RESTY_LRUCACHE_VERSION='0.15' | |
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" ] | |
} | |
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" \ | |
b22485ac7fa10dbfb410e0cda2e091d0d28d7634895a05cce4b26243a9ca95d5 | |
dl luajit "https://github.com/openresty/luajit2/archive/v${LUAJIT_VERSION}.tar.gz" \ | |
"luajit-${LUAJIT_VERSION}.tar.gz" \ | |
3b269f3a55c420e5a286bbd6b8ef8a5425dbcb4194fa2beb9e22eea277cd6638 | |
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" \ | |
a0a5e616c4a0a32e48899d12242fed5a371f69a85f11ff274a87a2f02f419876 | |
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" \ | |
ecf5c2afd345149cef19bf2e3e196bf1c514ca85e778f853f80a379284b70de1 | |
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" \ | |
adc7781ddaeab9341b82033a6c06b0d190d4c6d1c2dddd46f6965ce9e57a0310 | |
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" \ | |
8cf1a22e0d5b8f35cb0b2e14c58fcb3aa505a8fb6e956817f0cdb1f06593f072 | |
# 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
wow thanks for the great script
for anyone coming to this i also created an installer script for nginx with more things to be added
https://github.com/Elyasnz/NginxInstaller/