lua也有类似composer/mvn/pip的的包管理器,那就是luarocks

博主这里已安装过openresty,今日要安装下luarocks包管理来使用第三方包

luarocks部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# luarocks下载
[root@nginx-cluster src]# wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
[root@nginx-cluster src]# tar -zvxf v3.0.0.tar.gz
[root@nginx-cluster src]# cd luarocks-3.0.0
# luarocks编译安装
[root@nginx-cluster luarocks-3.0.0]# ./configure --prefix=/usr/local/openresty/luajit \
> --with-lua=/usr/local/openresty/luajit/ \
> --lua-suffix=jit \
> --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
--lua-suffix is no longer necessary.
The suffix is automatically detected.

Configuring LuaRocks...

Lua version detected: 5.1
Lua interpreter found: /usr/local/openresty/luajit/bin/luajit
lua.h found: /usr/local/openresty/luajit/include/luajit-2.1/lua.h
unzip found in PATH: /usr/bin

Done configuring.

LuaRocks will be installed at......: /usr/local/openresty/luajit
LuaRocks will install rocks at.....: /usr/local/openresty/luajit
LuaRocks configuration directory...: /usr/local/openresty/luajit/etc/luarocks
Using Lua from.....................: /usr/local/openresty/luajit
Lua include directory..............: /usr/local/openresty/luajit/include/luajit-2.1

* Type make and make install:
to install to /usr/local/openresty/luajit as usual.
* Type make bootstrap:
to install LuaRocks into /usr/local/openresty/luajit as a rock.

# 安装luarocks
[root@nginx-cluster luarocks-3.0.0]# make build
[root@nginx-cluster luarocks-3.0.0]# make install

# 查看版本
[root@nginx-cluster lua]# luarocks --version
/usr/bin/luarocks 3.0.0
LuaRocks main command-line interface

lua第三方仓库地址:https://luarocks.org/

安装包命令

1
2
3
4
5
[root@nginx-cluster lua]# luarocks install dkjson
Warning: falling back to curl - install luasec to get native HTTPS support
Installing https://luarocks.org/dkjson-2.6-1.src.rock

dkjson 2.6-1 is now installed in /usr/local/openresty/luajit (license: MIT/X11)

安装到自定义路径

1
[root@nginx-cluster lua]# luarocks install dkjson --tree=./lua_pkg

openresty加载使用luarock的包

此处使用MD5包

1
[root@nginx-cluster lua]# luarocks install md5

在nginx主配置文件的http块中定义lua_package_path

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@nginx-cluster conf]# cat nginx.conf
worker_processes 4;
events {
worker_connections 65535;
}
error_log logs/error.log info;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_package_path '$prefix/lua/?.lua;/blah/?.lua;;';
include conf.d/*;
}

加载模块

1
2
3
4
5
6
7
8
9
10
11
server {
listen 8084;
location = /luarocks {
content_by_lua '
local rocks = require "luarocks.loader"
local md5 = require "md5"
ngx.say("hello, luarocks!")
ngx.say(md5.sumhexa("xadocker"))
';
}
}

测试访问

1
2
3
[root@nginx-cluster ~]# curl 127.0.0.1:8084/luarocks
hello, luarocks!
660277a884f0c5bacffd3a0ed5c148ba