centos nginx installation lua module support

Special thanks to zhangyichun for sharing

Installation steps

Install luajit

Download LuaJIT source code

[root@nginx-4 ~] wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20220111.tar.gz
[root@nginx-4 ~] tar -zxvf v2.1-20220111.tar.gz
[root@nginx-4 ~] cd luajit2-2.1-20220111
[root@nginx-4 luajit2-2.1-20220111] make && make install PREFIX=/opt/luajit

Make is relatively slow. If it is a multi-core platform, you can use make - J < n >, and N is the number of cores, which will not be described later

The installation path here is / opt/luajit, or you can choose your own path

After installation, output two environment variables

[root@nginx-4 ~]  export LUAJIT_LIB=/opt/luajit/luajit/lib
[root@nginx-4 ~]  export LUAJIT_INC=/opt/luajit/luajit/include/luajit-2.1

Install ngx_devel_kit

Download ndk source code

[root@nginx-4 ~] wget https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.1.tar.gz
[root@nginx-4 ~] tar -zxvf v0.3.1.tar.gz
[root@nginx-4 ~] cd  ngx_devel_kit-0.3.1
[root@nginx-4 ngx_devel_kit-0.3.1] make && make install PREFIX=/opt/luajit

Download ngx_lua

Download ngx_lua source code

[root@nginx-4 ~] wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.21rc1.tar.gz
[root@nginx-4 ~] tar -zxvf v0.10.21rc1.tar.gz

Installing nginx

Download nginx source code from the official website

[root@nginx-4 ~] wget
[root@nginx-4 ~] tar -zxvf  nginx-1.21.5.tar.gz
[root@nginx-4 ~] cd nginx-1.21.5
[root@nginx-4 nginx-1.21.5] ./configure --prefix=/opt/nginx --add-module=~/ngx_http_auth_pam_module --add-dynamic-module=~/lua-nginx-module-0.10.21rc1 --add-dynamic-module=~/ngx_devel_kit-0.3.1 --with-pcre --with-stream --with-http_ssl_module
[root@nginx-4 nginx-1.21.5] make && make install

The pcre module is dependent on the lua module. Be sure to, otherwise the pcre function will not be found. The problems encountered are listed below

Other modules are installed according to their own conditions. For example, ssl module is also added here

Dynamic module is supported from ng 1.9.11 +. For previous versions, please use add module directly
If you use dynamic module to compile, you need to use nginx Use load in conf file_ Module to add and use:
load_module /opt/nginx/modules/ngx_http_lua_module.so;

Install Lua resty core and Lua resty lrucache

Lua resty core and Lua resty lrucache are ngx_lua is dependent and needs to be installed manually

  • download lua-resty-core

    [root@nginx-4 ~] wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.11.tar.gz
    [root@nginx-4 ~] tar -zxvf v0.11.tar.gz
    [root@nginx-4 ~] cd  lua-resty-core-0.1.23rc1
    [root@nginx-4 lua-resty-core-0.1.23rc1] make install PREFIX=/opt/nginx
    
  • download lua-resty-lrucache

    [root@nginx-4 ~] wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.23rc1.tar.gz
    [root@nginx-4 ~] tar -zxvf v0.1.23rc1.tar.gz
    [root@nginx-4 ~] cd lua-resty-lrucache-0.11
    [root@nginx-4 lua-resty-lrucache-0.11] make install PREFIX=/opt/nginx
    
    

In nginx The http part of conf introduces lua module into

lua_package_path "/opt/nginx/lib/lua/?.lua;;";

Test lua installation

Add a lua script

cat >> /opt/nginx/lua/hell_lua.lua << EOF
ngx.say("hello lua");
EOF

In nginx Add an http block in conf

location /lua {    
    default_type 'text/html';    
    content_by_lua_file lua/hell_lua.lua; #Relative to nginx installation directory
}

Execute nginx -s reload to reload the configuration, open the browser, enter ip/lua, and the page responds to hello lua, which indicates that the installation is successful

common problem

  • Start nginx and report an error libluajit-5.1 so. 2: cannot open shared object file: No such file or directory
    Choose to install luakit manually. The lib library link used during installation is in the way of environment variables:

    [root@nginx-4 nginx]# export LUAJIT_LIB=/opt/luajit/lib
    [root@nginx-4 nginx]# export LUAJIT_INC=/opt/luajit/include/luajit-2.1
    

    Therefore, there is no problem in compiling nginx, but the libluajit library cannot be found when running nginx -t check:

    [root@nginx-4 nginx]# ./sbin/nginx -tnginx: [emerg] dlopen() "/usr/local/nginx/modules/ngx_http_lua_module.so" failed (libluajit-5.1.so.2: cannot open shared object file: No such file or directory) in /usr/local/nginx/conf/nginx.conf:14
    

    solve
    Using id to manually link lib can solve the problem.

    [root@nginx-4 nginx]# touch /etc/ld.so.conf.d/luajit.conf[
    root@nginx-4 nginx]# echo "/opt/luajit/lib" >> /etc/ld.so.conf.d/luajit.conf
    [root@nginx-4 ld.so.conf.d]# ldconfig
    
  • Start nginx and report an error / usr/local/nginx/modules/ngx_http_lua_module.so: undefined symbol: pcre_malloc
    pcre not found_ Malloc function. The name depends on the pcre library. Check the installed pcre Library:

    [root@nginx-4 ~]# rpm -qa | grep pcrepcre-8.32-17.el7.aarch64
    pcre-devel-8.32-17.el7.aarch64
    pcre2-10.23-2.el7.aarch64pcre2-utf16-10.23-2.el7.aarch64
    openresty-pcre-8.44-2.el7.aarch64
    pcre2-utf32-10.23-2.el7.aarch64
    pcre2-devel-10.23-2.el7.aarch64
    

    If not, you can install PCRE devel using Yum - y install PCRE devel

    If you cannot install using yum, you can install pcre offline

    Recompile and install nginx:

    [root@nginx-4 nginx-1.21.5] ./configure --add-module=/root/ngx_http_auth_pam_module --add-dynamic-module=/root/lua-nginx-module-0.10.21rc1 --add-dynamic-module=/root/ngx_devel_kit-0.3.1 --with-pcre --with-stream --with-http_ssl_module
    [root@nginx-4 nginx-1.21.5]# make -j2 && make install
    

    If the pcre library is installed manually, the above -- with pcre must be changed to -- with pcre = < pcre installation path >

    Note that the luakit library path must be configured when adding lua library installation. In the installation instructions, there are two environment variables: LUAJIT_LIB and LUAJIT_INC

Keywords: CentOS Nginx lua

Added by Mr.Shawn on Fri, 28 Jan 2022 12:25:38 +0200