Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save taarimalta/95327bab1378e2a270b668c9d0b5db91 to your computer and use it in GitHub Desktop.
Save taarimalta/95327bab1378e2a270b668c9d0b5db91 to your computer and use it in GitHub Desktop.
Lua dynamic module installation onto Nginx (CentOS 7)

Install Nginx

Follow instructions here

Installing Nginx on CentOS 7

Get all packages

Start with a temporary location to place files

rm -rf /tmp/nginx
mkdir -p /tmp/nginx
cd /tmp/nginx

Use this to get nginx version, Get Nginx source. It must be the same as the nginx on your system

nginx -v
NGINX_VERSION=$(nginx -v 2>&1 | awk -F/ '{print $2}')
wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz

Get OpenResty Packages

# Downloading Lua
wget https://github.com/openresty/luajit2/archive/v2.1-20200102.tar.gz

# Downloading Nginx development kit
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz

# Downloading Nginx Lua Module
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz

# Downloading Resty Core
wget https://github.com/openresty/lua-resty-core/archive/v0.1.17.tar.gz

# Downloading Resty LRU Cache
wget https://github.com/openresty/lua-resty-lrucache/archive/v0.09.tar.gz

Untar

find . -type f -name '*.tar.gz' -exec tar -xzf {} \;

Build lua

cd /tmp/nginx

# Change directory to the lua language library. Not the one above
cd luajit*
make & make install


export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1

Install Resty Core

cd /tmp/nginx/lua-resty-core-0.1.17
make install

Install Resty LRU Cache

cd /tmp/nginx/lua-resty-lrucache-0.09
make install

Nginx dynamic modules

Configure and build dynamic module

cd ../nginx-1.17.6/

NGINX_CONFIGURE_ARGUMENTS=$(echo $(nginx -V 2>&1) | sed -nr '/configure arguments:/ s/.*configure arguments:([^"]+).*/\1/p')

eval "./configure --with-ld-opt="-Wl,-rpath,/usr/local/lib" --add-dynamic-module=../ngx_devel_kit-0.3.1 --add-dynamic-module=../lua-nginx-module-0.10.15 $NGINX_CONFIGURE_ARGUMENTS"

make modules

Move modules to modules-path

# The target path can be found using `nginx -V`
mv objs/*.so /usr/lib64/nginx/modules

# Create symbolic link to modules
ln -s /usr/lib64/nginx/modules /etc/nginx/modules

Configure to load these modules configuration

# Note my prefix is /etc/nginx. This can be obtained using `nginx -V`
mkdir -p /etc/nginx/modules-enabled/

echo -e "load_module modules/ndk_http_module.so;\nload_module modules/ngx_http_lua_module.so;" > /etc/nginx/modules-enabled/lua.conf

Place this around the top of your nginx.conf

include /etc/nginx/modules-enabled/*.conf;

OpenResty-core

Place the following inside the http block of nginx.conf

# Source: OpenResty Core Readme
# you do NOT need to configure the following line when you
# are using the OpenResty bundle 1.4.3.9+.
lua_package_path "/usr/local/lib/lua/?.lua;;";

init_by_lua_block {
    require "resty.core"
    collectgarbage("collect")  -- just to collect any garbage
} 


Restart Nginx

systemctl restart nginx

Good luck

References

Thanks to the following

Building Nginx with Lua
Installing Nginx on CentOS 7
Dynamic Modules
Dynamic Modules Compiling
Compatibility Issue
OpenResty Core not found Issue
Replace Chars after string in Bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment