Luarocks是一個Lua包管理器,基於Lua語言開發,提供一個命令行的方式來管理Lua包依賴、安裝第三方Lua包等,社區比較流行的包管理器之一,另還有一個LuaDist,Luarocks的包數量比LuaDist多,更細節的二者對比可參閱這裏php
在作一些openresty的項目的時候,常常會藉助一些第三方包來協助開發,爲了方便管理,咱們能夠使用openresy官方的opm,或者lua的包管理工具luarocks,只不過opm的包數量還不是太多,用的較多的仍是luarocks,如今只能期待opm社區不斷的發展壯大了。c++
編譯安裝git
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
tar zxvf v3.0.0.tar.gz
cd 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
make build
# 安裝須要root權限
sudo make install
安裝參數說明github
--prefix
設定 luarocks
的安裝目錄redis
--with-lua
則是系統中安裝的 lua
的根目錄shell
--lua-suffix
版本後綴,此處由於openresyt的lua解釋器使用的是 luajit
,因此此處得寫 jit
json
--with-lua-include
設置 lua
引入一些頭文件頭文件的目錄segmentfault
查看版本api
luarocks --version
/usr/local/openresty/luajit/bin/luarocks 3.0.0
LuaRocks main command-line interface
提示錯誤bash
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
Could not find 'unzip'.
Make sure it is installed and available in your PATH.
configure failed.
安裝
sudo apt install unzip
執行 luarocks install package
就能夠安裝lua
的包了luarocks install package --tree=path
還能夠指定你安裝的包的存放路徑
下載 rapidjson
包
$ luarocks install rapidjson --tree=/usr/local/openresty/lualib/resty
Installing https://luarocks.org/rapidjson-0.5.1-1.src.rock
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- LUA_RAPIDJSON_VERSION: 0.5.1
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/luarocks_rapidjson-0.5.1-1-fd1Qr3/lua-rapidjson/build.luarocks
Scanning dependencies of target lua-rapidjson
[ 20%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Document.cpp.o
[ 40%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Schema.cpp.o
[ 60%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/rapidjson.cpp.o
[ 80%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/values.cpp.o
[100%] Linking CXX shared module rapidjson.so
[100%] Built target lua-rapidjson
[100%] Built target lua-rapidjson
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1/rapidjson/0.5.1-1/lib/rapidjson.so
rapidjson 0.5.1-1 is now installed in /usr/local/openresty/lualib/resty (license: MIT)
安裝路徑
$/usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1
ls
30log luasocket manifest rapidjson
使用
local rapidjson = require('rapidjson')
rapidjson.encode()
rapidjson.decode()
rapidjson.load()
rapidjson.dump()
》》可能遇到的問題!!!
2018/12/24 10:34:06 [error] 11222#11222: init_by_lua error: init_by_lua:2: module 'resty.auto-ssl' not found:
no field package.preload['resty.auto-ssl']
no file '/usr/local/openresty/lualib/resty/auto-ssl.lua'
no file '/usr/local/openresty/site/lualib/resty/auto-ssl.ljbc'
no file '/usr/local/openresty/site/lualib/resty/auto-ssl/init.ljbc'
no file '/usr/local/openresty/lualib/resty/auto-ssl.ljbc'
no file '/usr/local/openresty/lualib/resty/auto-ssl/init.ljbc'
no file '/usr/local/openresty/site/lualib/resty/auto-ssl.lua'
no file '/usr/local/openresty/site/lualib/resty/auto-ssl/init.lua'
no file '/usr/local/openresty/lualib/resty/auto-ssl.lua'
no file '/usr/local/openresty/lualib/resty/auto-ssl/init.lua'
no file './resty/auto-ssl.lua'
stack traceback:
[C]: in function 'require'
init_by_lua:2: in main chunk
》》解決方式一:切換成 root 帳戶安裝,`luarocks install lua-resty-auto-ssl`。以上問題解決了
》》解決方式二:若是使用 sudo ,則使用絕對路徑解決
$ sudo /usr/local/openresty/luajit/bin/luarocks install lua-resty-auto-ssl
Warning: The directory '/home/www/.cache/luarocks' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing /usr/local/openresty/luajit/bin/luarocks with sudo, you may want sudo's -H flag.
Installing https://luarocks.org/lua-resty-auto-ssl-0.12.0-1.src.rock
mkdir -p /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build
rm -f /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-dehydrated-*
mkdir -p /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin
curl -sSLo /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/dehydrated "https://raw.githubusercontent.com/lukas2511/dehydrated/0bc0bd13d6abdc027c58bec12f7c2d3198d3a677/dehydrated"
chmod +x /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/dehydrated
touch /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-dehydrated-2-0bc0bd13d6abdc027c58bec12f7c2d3198d3a677
rm -f /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-lua-resty-shell-*
curl -sSLo /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/lib/resty/auto-ssl/vendor/shell.lua "https://raw.githubusercontent.com/juce/lua-resty-shell/955243d70506c21e7cc29f61d745d1a8a718994f/lib/resty/shell.lua"
touch /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-lua-resty-shell-955243d70506c21e7cc29f61d745d1a8a718994f
rm -f /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-sockproc-*
mkdir -p /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin
cd /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build && curl -sSLo sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c.tar.gz "https://github.com/juce/sockproc/archive/fc8ad3f15a7b2cf2eaf39663b90010efc55e207c.tar.gz"
cd /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build && tar -xf sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c.tar.gz
cd /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c && make
make[1]: Entering directory '/tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c'
gcc -Wall -Werror -o sockproc sockproc.c
make[1]: Leaving directory '/tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c'
cp /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/sockproc-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c/sockproc /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/sockproc
chmod +x /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/sockproc
touch /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/stamp-sockproc-2-fc8ad3f15a7b2cf2eaf39663b90010efc55e207c
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl
install -m 644 lib/resty/auto-ssl.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl.lua
install -m 644 lib/resty/auto-ssl/init_master.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/init_master.lua
install -m 644 lib/resty/auto-ssl/init_worker.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/init_worker.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/jobs
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/json_adapters
install -m 644 lib/resty/auto-ssl/json_adapters/cjson.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/json_adapters/cjson.lua
install -m 644 lib/resty/auto-ssl/json_adapters/dkjson.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/json_adapters/dkjson.lua
install -m 644 lib/resty/auto-ssl/jobs/renewal.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/jobs/renewal.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/servers
install -m 644 lib/resty/auto-ssl/servers/challenge.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/servers/challenge.lua
install -m 644 lib/resty/auto-ssl/servers/hook.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/servers/hook.lua
install -m 644 lib/resty/auto-ssl/ssl_certificate.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/ssl_certificate.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/ssl_providers
install -m 644 lib/resty/auto-ssl/ssl_providers/lets_encrypt.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/ssl_providers/lets_encrypt.lua
install -m 644 lib/resty/auto-ssl/storage.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/storage.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/storage_adapters
install -m 644 lib/resty/auto-ssl/storage_adapters/file.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/storage_adapters/file.lua
install -m 644 lib/resty/auto-ssl/storage_adapters/redis.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/storage_adapters/redis.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/utils
install -m 644 lib/resty/auto-ssl/utils/shell_execute.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/utils/shell_execute.lua
install -m 644 lib/resty/auto-ssl/utils/start_sockproc.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/utils/start_sockproc.lua
install -m 644 lib/resty/auto-ssl/utils/run_command.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/utils/run_command.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/vendor
install -m 644 lib/resty/auto-ssl/vendor/shell.lua /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/lua/resty/auto-ssl/vendor/shell.lua
install -d /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/bin/resty-auto-ssl
install -m 755 bin/letsencrypt_hooks /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/bin/resty-auto-ssl/letsencrypt_hooks
install -m 755 bin/start_sockproc /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/bin/resty-auto-ssl/start_sockproc
install -m 755 /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/dehydrated /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/bin/resty-auto-ssl/dehydrated
install -m 755 /tmp/luarocks_lua-resty-auto-ssl-0.12.0-1-FdxVn9/lua-resty-auto-ssl/build/bin/sockproc /usr/local/openresty/luajit/lib/luarocks/rocks-5.1/lua-resty-auto-ssl/0.12.0-1/bin/resty-auto-ssl/sockproc
lua-resty-auto-ssl 0.12.0-1 is now installed in /usr/local/openresty/luajit (license: MIT)
》》解決方式三:修改/etc/sudoers這個文件,將要執行的命令所在的目錄添加到後面
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/openresty/luajit/bin"
參考