將luarocks整合進openresty


緣由

隨着功能需求的深入, openresty領域的包已經不夠用了, 需要lua領域本身累積的庫, 也就是luarocks.
本文講解了windows 10桌面和ubuntu server兩套系統的方法

Ubuntu Server

比較簡單, 無腦敲命令即可

$ wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz
$ tar zxpf luarocks-2.4.1.tar.gz
$ cd luarocks-2.4.1
./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 && make install

安裝luasql-postgres

apt-get install libpq-dev
luarocks PGSQL_INCDIR=/usr/include/postgresql/ install luasql-postgres

mysql同理. 安裝之后
luarocks install luasql-mysql MYSQL_INCDIR=/usr/include/mysql

禁用ssh密碼方式登錄

vim /etc/ssh/sshd_config
PasswordAuthentication no

Windows 10

這個就比較復雜了.

安裝MinGW

其實這個是可選項, 如果你的系統路徑能找到MSVC的編譯器, 那么這一步可以忽略.
為了讓以后windwos進行linux相關的作業簡單點, 長痛不如短痛, 干脆就裝了MinGW吧.

  1. 點擊此處下載:https://sourceforge.net/projects/mingw/files/latest/download?source=files
  2. 雙擊mingw-get-setup.exe, 記得勾選圖形界面(默認是勾選的), 這樣會讓你選擇具體安裝哪些東西時比較方便
  3. 在MinGW Installer圖形界面勾選mingw32-base, 然后開始在線安裝.

國內網速不是很好,斷斷續續的, 可能要等半天. 但最終我還是安裝上了.
按默認路徑安裝你會發現C:\MinGW\bin\mingw32-gcc.exe這個玩意出現, 它就是luarocks要的東西.最后把C:\MinGW\bin加入windows環境變量

下載luarocks

以2.4.2為例, windows版的下載鏈接:http://luarocks.github.io/luarocks/releases/luarocks-2.4.2-win32.zip
下載完解壓,這里注意下, 以管理員權限打開cmd窗口再cd進luarocks-2.4.2-win32文件夾

配置並安裝luarocks

這里, 假設windows上openresty文件夾的路徑為:%USERPROFILE%\Desktop\openresty

開始安裝

在cmd依次敲下面兩個命令

set PREFIX=%USERPROFILE%\Desktop\openresty
install /P %PREFIX%\luarocks /SELFCONTAINED /INC %PREFIX%\include\luajit-2.1 /LIB %PREFIX% /BIN %PREFIX% /MW

/P 表示把luarocks安裝在openresty的luarocks文件夾
/SELFCONTAINED 表示/TREE和/CONFIG選項的值和/P一致. 這樣就所有玩意都在luarocks這個文件里,方便查找.
/INC 表示luajit的include庫所在的文件夾
/LIB 表示lua51.dll所在的文件夾
/BIN表示luajit.exe所在的文件夾
/MW表示用mingw32-gcc.exe編譯器.

完成安裝

正常情況下. 在命令行看到如下輸出:

C:\Users\xn\Desktop\luarocks-2.4.2-win32>install /P %PREFIX%\luarocks /SELFCONTAINED /INC %PREFIX%\include\luajit-2.1 /LIB %PREFIX% /BIN %PREFIX% /MW

C:\Users\xn\Desktop\luarocks-2.4.2-win32>rem=rem --[[
LuaRocks 2.4.x installer.


========================
== Checking system... ==
========================


Admin privileges available for installing
Looking for Lua interpreter
       Found luajit.exe, testing it...
    checking for C:\Users\xn\Desktop\openresty\lua5.1.lib
    checking for C:\Users\xn\Desktop\openresty\lua51.lib
    checking for C:\Users\xn\Desktop\openresty\lua5.1.dll
    checking for C:\Users\xn\Desktop\openresty\lua51.dll
       Found lua51.dll
    checking for C:\Users\xn\Desktop\openresty\include\luajit-2.1\lua.h
       Found lua.h
    C:\Users\xn\Desktop\openresty\luajit.exe uses MSVCRT.DLL as runtime
Runtime check completed.

==========================
== System check results ==
==========================

Will configure LuaRocks with the following paths:
LuaRocks        : C:\Users\xn\Desktop\openresty\luarocks
Config file     : C:\Users\xn\Desktop\openresty\luarocks\config-5.1.lua
Rocktree        : C:\Users\xn\Desktop\openresty\luarocks\systree

Lua interpreter : C:\Users\xn\Desktop\openresty\luajit.exe
    binaries    : C:\Users\xn\Desktop\openresty
    libraries   : C:\Users\xn\Desktop\openresty
    includes    : C:\Users\xn\Desktop\openresty\include\luajit-2.1
    architecture: x86
    binary link : lua51.dll with runtime MSVCRT.dll

Compiler        : MinGW (make sure it is in your path before using LuaRocks)

Press <ENTER> to start installing, or press <CTRL>+<C> to abort. Use install /? for installation options.


============================
== Installing LuaRocks... ==
============================


Installing LuaRocks in C:\Users\xn\Desktop\openresty\luarocks...
Created LuaRocks command: C:\Users\xn\Desktop\openresty\luarocks\luarocks.bat
Created LuaRocks command: C:\Users\xn\Desktop\openresty\luarocks\luarocks-admin.bat

Configuring LuaRocks...
Created LuaRocks site-config file: C:\Users\xn\Desktop\openresty\luarocks\lua\luarocks\site_config_5_1.lua
Created LuaRocks config file: C:\Users\xn\Desktop\openresty\luarocks\config-5.1.lua

Creating rocktrees...
Created system rocktree    : "C:\Users\xn\Desktop\openresty\luarocks\systree"
Local user rocktree exists : "C:\Users\xn\AppData\Roaming\LuaRocks"

============================
== LuaRocks is installed! ==
============================


You may want to add the following elements to your paths;
Lua interpreter;
  PATH     :   C:\Users\xn\Desktop\openresty
  PATHEXT  :   .LUA
LuaRocks;
  PATH     :   C:\Users\xn\Desktop\openresty\luarocks
  LUA_PATH :   C:\Users\xn\Desktop\openresty\luarocks\lua\?.lua;C:\Users\xn\Desktop\openresty\luarocks\lua\?\init.lua
Local user rocktree (Note: %APPDATA% is user dependent);
  PATH     :   %APPDATA%\LuaRocks\bin
  LUA_PATH :   %APPDATA%\LuaRocks\share\lua\5.1\?.lua;%APPDATA%\LuaRocks\share\lua\5.1\?\init.lua
  LUA_CPATH:   %APPDATA%\LuaRocks\lib\lua\5.1\?.dll
System rocktree
  PATH     :   C:\Users\xn\Desktop\openresty\luarocks\systree\bin
  LUA_PATH :   C:\Users\xn\Desktop\openresty\luarocks\systree\share\lua\5.1\?.lua;C:\Users\xn\Desktop\openresty\luarocks\systree\share\lua\5.1\?\init.lua
  LUA_CPATH:   C:\Users\xn\Desktop\openresty\luarocks\systree\lib\lua\5.1\?.dll

Note that the %APPDATA% element in the paths above is user specific and it MUST be replaced by its actual value.
For the current user that value is: C:\Users\xn\AppData\Roaming.

提示得很貼心了.把%USERPROFILE%\Desktop\openresty\luarocks加入系統路徑, 再試試luarocks install luafilesystem
如果一切順利,你會發現%USERPROFILE%\Desktop\openresty\luarocks\systree\lib\lua\5.1下出現了lfs.dll, 這就說明安裝成功了.

配置nginx路徑

包是成功安裝好了, 那么怎么讓nginx能找到它呢, 在配置文件里這樣搞:

http {
    lua_package_path "C:\Users\xn\Desktop\openresty\luarocks\systree\share\lua\5.1\?.lua;C:\Users\xn\Desktop\openresty\luarocks\systree\share\lua\5.1\?\init.lua;;";  
    lua_package_cpath "C:\Users\xn\Desktop\openresty\luarocks\systree\lib\lua\5.1\?.dll;;";
}

Hello world

最后lua里面require('lfs'), 大功告成

參考

https://github.com/luarocks/luarocks/wiki/Installation-instructions-for-Windows
http://www.tuicool.com/articles/fQZVJjQ
https://luarocks.org/
https://stackoverflow.com/questions/43189975/how-to-install-luasql-on-linux-ubuntu-16


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM