配置过程也挺简单的,记录下
1.ZeroBraneStudio
当前使用1.30版本,具体使用请自己摸索
2.openresty
编译安装请参照openresty官方:http://openresty.org/en/installation.html
或者http://xuewb.com/openresty/install.html
需要注意在编译参数中需要添加 :--with-luajit安装luajit
3.安装lua
可以使用homebrew进行安装,但需要指定版本:
> brew search lua
> brew install lua51
4.编译安装luarocks
下载luarocks(lua包管理器):https://github.com/luarocks/luarocks/wiki/Download
./configure --prefix=/usr/local/luarocks-VERSION --with-lua=/lua/path --lua-version=5.1
注意:这个版本需要和安装的lua版本一致,要不然会出现下载的lua库版本不一致
5.使用luarocks安装luasocket
在ZeroBraneStudio中提供的mobdebug.lua中需要require luasockets库,所以需要安装此库
只要之前的版本指定对了,这里就没问题了:brew install luasockets
6.配置独立的nginx配置文件:example.conf
前面安装好openresty之后,会在你指定的prefix下出现lualib/luajit/nginx三个目录
nginx目录就是平时使用的nginx目录,nginx.conf配置文件也在改目录下的conf下,为了不污染原始的nginx.conf,一般新建一个工作目录,比如 ~/lua_project/example/,这个example就是一个完整的项目目录,一般在这个目录下还会建立一些子目录,比如lualib/lua等,lualib用来放置项目中需要使用的lua库,lua用于放置项目的源代码文件,同时在example下新建example.conf,并填入如下内容:
server {
listen 80;
server_name _;
location ~ /test_debug {
content_by_lua_file /Users/xxx/lua_project/example/lua/test_debug.lua;
# xxx替换为你自己的目录
}
}
6.配置nginx.conf
example.conf建立好之后,需要在nginx.conf中进行引用,在http上下文中使用include指令进行引入:
include /Users/xxx/lua_project/example/example.conf;
设置lua_path和lua_cpath:
lua_package_path "/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/lualibs/?/?.lua;/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/lualibs/?.lua;;"; #lua模块
lua_package_cpath "/usr/local/luarocks-2.4.1/lib/lua/5.1/?.so;;"; #c模块
注意:
- lua中使用require时会去lua_path下查找相应的.lua库文件,找不到再去lua_cpath下查找.so文件
- /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/lualibs下使用mobdebug.lua
- ;;表示当前目录查找
- ?表示require('aa'):aa.lua或aa.so
7.开发test_debug.lua
在~/lua_project/example/lua下建立test_debug.lua 文件,写入如下代码:
require("mobdebug").start()
local name = ngx.var.arg_name or "Anonymous"
ngx.say("Hello, ", name, "!")
ngx.say("Done debugging.")
require('mobdebug').done()
8.设置example为当前项目目录
9.启动nginx测试debug
进入nginx目录:cd /usr/local/openresty-VERSION/nginx
启动nginx: ./sbin/nginx -c ./conf/nginx.conf
访问浏览器:http://localhost/test_debug?name=wilson
会自动进入debug模式