Mac 下 Lua 框架 Lapis 运行环境搭建

前言

Mac系统中,如果利用Homebrew安装LuaRocks,默认只会安装最新版本的Lua。鉴于lapisluajit都只兼容lua@5.1版本,所以就需要自行安装lua@5.1

兼容

第一步:利用Homebrew安装luarocks

1
brew install luarocks

第二步:利用Homebrew安装lua@5.1

1
brew install lua@5.1

第三步:查看lua@5.1的安装目录

1
2
3
brew info lua@5.1

/usr/local/Cellar/lua@5.1/5.1.5_8

第四步:利用参数--lua-dir以及--lua-version使用5.1版本,两个参数可以同时设置,也可以只设置一个

1
2
3
luarocks --lua-dir=/usr/local/Cellar/lua@5.1/5.1.5_8 --lua-version=5.1 install lapis

luarocks --lua-version=5.1 install lapis

通过上面设置就能兼容不同版本的Lua

错误

安装luaossl时出现以下错误:

1
2
3
4
5
6
7
8
Installing https://luarocks.org/luaossl-20200709-0.src.rock

Error: Failed installing dependency: https://luarocks.org/luaossl-20200709-0.src.rock - Could not find header file for CRYPTO
No file openssl/crypto.h in /usr/local/include
No file openssl/crypto.h in /usr/include
No file openssl/crypto.h in /include
You may have to install CRYPTO in your system and/or pass CRYPTO_DIR or CRYPTO_INCDIR to the luarocks command.
Example: luarocks install luaossl CRYPTO_DIR=/usr/local

解决如下:
设置OPENSSL_DIR以及CRYPTO_DIR

1
luarocks --lua-version=5.1 OPENSSL_DIR=/usr/local/Cellar/openssl@1.1/1.1.1k/ CRYPTO_DIR=/usr/local/Cellar/openssl@1.1/1.1.1k/ install lapis

最后

配置LUA_PATHLUA_CPATH以及PATH,在终端中输入

1
luarocks --lua-version=5.1 path --bin

取得LUA_PATHLUA_CPATH写入到~/.zshrc

1
2
export LUA_PATH=''
export LUA_CPATH=''

最后再将.luarocks/bin导入PATH

1
export PATH="$HOME/.luarocks/bin:$PATH"

如果不做上面操作就会出现下面错误

1
lua entry thread aborted: runtime error: content_by_lua(nginx.conf.compiled:22):2: module 'lapis' not found: