我前面我们介绍了nginx+lua环境的搭建,在此我们再来了解下lua开发环境的安装。
目前lua版本已经更新到lua5.3,但是我们在此安装lua5.1,因为5.1运行了好多年,有好多模块都是基于此版本的,例如lua_gd。
安装:
1.先安装lua的相关依赖
yum install readline-dev readline-devel
2.安装lua5.1
wget http://www.lua.org/ftp/lua-5.1.0.tar.gz
tar -zxvf lua-5.1.0.tar.gz
make linux test
make install
lua有一个模块管理工具luarocks,类似于yum,我们可以使用luarocks来安装相关模块。
1.安装
wget http://luarocks.org/releases/luarocks-2.2.1.tar.gz
tar zxpf luarocks-2.2.1.tar.gz
cd luarocks-2.2.1
./configure会出现以下提示:
Lua interpreter found: /usr/bin/lua...
Lua version detected: 5.1
lua found in $PATH: /usr/bin
Checking Lua includes... lua.h not found (looked in /usr/include, /usr/include/lua/5.1, /usr/include/lua5.1)
You may want to use the flag --with-lua or --with-lua-include. See --help.
configure failed.
提示找不到lua.h,默认情况下会从/usr下寻找,因为我们需要指定lua.h的目录
find / -name lua.h
/usr/local/luajit/include/luajit-2.0/lua.h
/usr/local/src/lua/lua-5.1.5/src/lua.h
/usr/local/src/lua/LuaJIT-2.0.4/src/lua.h
/usr/local/include/luajit-2.0/lua.h
/usr/local/include/lua.h
再重新编译安装:
./configure --with-lua=/usr/local --with-lua-include=/usr/local/include
make bootstrap我们就将luarocks安装到/usr/local下了,加入以下环境变量,就可以直接使用了:
export PATH=/usr/local/bin:$PATH
lua可用模块列别,可以在此查看http://luarocks.org/repositories/rocks/,我们通过以下即可直接安装:
[[email protected] local]# luarocks install luafilesystem
Installing https://rocks.moonscript.org/luafilesystem-1.6.3-1.src.rock...
Using https://rocks.moonscript.org/luafilesystem-1.6.3-1.src.rock... switching to 'build' mode
gcc -O2 -fPIC -I/usr/local/include -c src/lfs.c -o src/lfs.o
gcc -shared -o lfs.so -L/usr/local/lib src/lfs.o
Updating manifest for /usr/local/lib/luarocks/rocks
luafilesystem 1.6.3-1 is now built and installed in /usr/local (license: MIT/X11)安装完成后,会在/usr/local/lib/lua/5.1/下生成一个lfs.so的文件,我们只需要将此文件拷贝到nginx中自定义的LUA_PATH中,就可以引用该库,调用其中的函数。
例如在nginx的配置文件中引用:
lua_package_path ‘/usr/local/nginx/lua/?.lua;;’
lua_package_cpath ‘/usr/local/nginx/lua/lib/?.?;;’;
#其中”;;”代表原先查找范围。注:路径自己定义,也可定义成默认路径。
原文:http://blog.csdn.net/yanggd1987/article/details/46681687