在zerobranestudio中调试openresty lua脚本

本文介绍如何通过ZeroBrane Studio配置Lua环境并利用OpenResty实现项目的部署流程,包括Lua及其相关组件的安装、配置独立的Nginx配置文件、以及如何设置调试模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置过程也挺简单的,记录下

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模式

 

 

转载于:https://my.oschina.net/u/1178805/blog/807275

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值