Nginx部署Lua

李宜然
2023-12-01

Lua是一个小巧的脚本语言,其主要的设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能,它是由标准的C语言编写而成,几乎在所有操作系统和平台上都可以编译,运行,Lua没有提供强大的库,这是由它定位决定的,所以Lua不适合作为开发独立应用程序的语言

先安装nginx
1、挂载镜像
[root@kvm122102 Nginx]# mount /dev/cdrom /mnt/

2、安装依赖包
[root@kvm122102 Nginx]# yum -y install gcc gcc-c++ make autoconf openssl openssl-devel

3、解压
[root@kvm122102 Nginx]# tar xf nginx-1.13.4.tar
[root@kvm122102 Nginx]# tar xf pcre-8.40.tar.gz #让nginx支持正则

4、创建用户
[root@kvm122102 Nginx]# useradd -s /sbin/nologin nginx

5、进入到nginx路径
[root@kvm122102 Nginx]# cd nginx-1.13.4

6、配置
[root@kvm122102 nginx-1.13.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre=/root/Nginx/pcre-8.40

7、编译
[root@kvm122102 nginx-1.13.4]# make

8、安装
[root@kvm122102 nginx-1.13.4]# make install

9、编辑启动配置
[root@kvm122102 nginx-1.13.4]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]

Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]

WantedBy=multi-user.target

10、启用脚本
[root@kvm122102 nginx-1.13.4]# systemctl daemon-reload

11、启动nginx
[root@kvm122102 nginx-1.13.4]# systemctl start nginx

[root@kvm122102 LuaJIT-2.0.5]# systemctl stop nginx

[root@kvm122102 Nginx]# tar xf LuaJIT.2.0.5.tar
[root@kvm122102 Nginx]# cd LuaJIT-2.0.5/
[root@kvm122102 LuaJIT-2.0.5]# make install PREFIX=/usr/local/luajit
[root@kvm122102 LuaJIT-2.0.5]# echo “/usr/local/luajit/lib/” > /etc/ld.so.conf.d/usr_local_luajit_lib.conf
[root@kvm122102 LuaJIT-2.0.5]# ldconfig
[root@kvm122102 LuaJIT-2.0.5]# export LUAJIT_LIB=/usr/local/luajit/lib
[root@kvm122102 LuaJIT-2.0.5]# export
declare -x LUAJIT_LIB="/usr/local/luajit/lib"

[root@kvm122102 ~]# vim hello.lua
print (“Hello World”)

[root@kvm122102 ~]# lua hello.lua
Hello World
[root@kvm122102 ~]#
or
[root@kvm122102 ~]# lua
Lua 5.1.4 Copyright © 1994-2008 Lua.org, PUC-Rio

print (“hello word”)
hello word

^C
[root@kvm122102 ~]#

下载NDK与Lua_module

[root@vlnx251102 Nginx]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
[root@vlnx251102 Nginx]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.10.tar.gz
[root@kvm122102 Nginx]# tar xf v0.3.0.tar.gz
[root@kvm122102 Nginx]# tar xf v0.10.10.tar.gz
[root@kvm122102 Nginx]# tar -xf ngx_devel_kit-0.3.0.tar
[root@kvm122102 Nginx]# tar -xf lua-nginx-module-0.10.10.tar
[root@kvm122102 Nginx]# cd nginx-1.13.4/
[root@kvm122102 nginx-1.13.4]# make clean
rm -rf Makefile objs
[root@kvm122102 nginx-1.13.4]#
[root@kvm122102 nginx-1.13.4]# ./configure --prefix=/usr/local/nginx-lua --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre=/root/Nginx/pcre-8.40 --add-module=/root/Nginx/ngx_devel_kit-0.3.0 --add-module=/root/Nginx/lua-nginx-module-0.10.10

执行后出现以下错误,
checking for LuaJIT library in /usr/local/luajit/lib and /usr/local/luajit/lib (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) … not found
checking for LuaJIT library in /usr/local/luajit/lib and /usr/local/luajit/lib (specified by the LUAJIT_LIB and LUAJIT_INC env) … not found
./configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as /usr/local/luajit/lib and LUAJIT_INC (path for lua.h) /usr/local/luajit/lib, but we cannot find LuaJIT there.

安装lua-devel即可解决:
[root@kvm122102 nginx-1.13.4]# yum -y install lua-devel
[root@kvm122102 nginx-1.13.4]# make && make install
[root@kvm122102 nginx-1.13.4]# vim /usr/local/nginx-lua/conf/nginx.conf

location /test1 {
content_by_lua ’
ngx.say(“Hello word”)
ngx.log(ngx.ERR,“err err”)’;
}

    location /test2 {
            content_by_lua_file /tmp/hello.lua;
    }

[root@kvm122102 nginx-1.13.4]# echo ‘ngx.say(“hello world”)’ > /tmp/hello.lua
[root@kvm122102 nginx-lua]# /usr/local/nginx-lua/sbin/nginx -c /usr/local/nginx-lua/conf/nginx.conf
[root@kvm122103 ~]# curl 192.168.122.102/test1
Hello word
[root@kvm122103 ~]# curl 192.168.122.102/test2
hello world
[root@kvm122103 ~]#

 类似资料: