-
-
Save kakopappa/c2f63b5b5f058346482d3313989d3f80 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 result might work, but it was not thoroughly tested. Use it at your | |
# own risk. If you really need nginx with LUA in production, you should use | |
# OpenResty instead. | |
NGINX_VERSION='1.20.2' | |
NGX_DEVEL_KIT_VERSION='0.3.1' | |
LUA_NGINX_MODULE_VERSION='0.10.21' | |
STREAM_LUA_NGINX_MODULE_VERSION='0.0.11' | |
# We use openresty's version of luajit here. | |
LUAJIT_VERSION='2.1-20220411' | |
LUAJIT_MAJOR_VERSION='2.1' | |
LUA_RESTY_CORE_VERSION='0.1.23' | |
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" \ | |
41936071d032e8173ea3119509a13739688045ca8d04d63d04a69ef5331b3bd4 | |
dl luajit "https://github.com/openresty/luajit2/archive/v${LUAJIT_VERSION}.tar.gz" \ | |
"luajit-${LUAJIT_VERSION}.tar.gz" \ | |
d3f2c870f8f88477b01726b32accab30f6e5d57ae59c5ec87374ff73d0794316 | |
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" \ | |
9db756000578efaecb43bea4fc6cf631aaa80988d86ffe5d3afeb9927895ffad | |
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" \ | |
c7924f28cb014a99636e747ea907724dd55f60e180cb92cde6e8ed48d2278f27 | |
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" \ | |
efd6b51520429e64b1bcc10f477d370ebed1631c190f7e4dc270d959a743ad7d | |
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" \ | |
da70ba4b84292a862e845e10be10095c6ce78ec75ec9bba0e3b5b7987c5e11df | |
# 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}\"" |
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
[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 |
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
To install http | |
cd /usr/local/nginx/luajit-2.1-20220411/lib/lua/5.1/resty | |
sudo curl https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http.lua -o http.lua | |
sudo curl https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http_connect.lua -o http_connect.lua | |
sudo curl https://raw.githubusercontent.com/ledgetech/lua-resty-http/v0.16.1/lib/resty/http_headers.lua -o http_headers.lua |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment