使用luarocks管理lua第三方包

343次阅读
没有评论

共计 2439 个字符,预计需要花费 7 分钟才能阅读完成。

使用luarocks管理lua第三方包

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

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

luarocks部署

# 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/

安装包命令

[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)

安装到自定义路径

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

openresty加载使用luarock的包

此处使用MD5包

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

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

[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/*;
}

加载模块

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"))
        ';
    }
}

测试访问

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

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2021-05-26发表,共计2439字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)