【Lua】Mac下brew安装的nginx添加lua扩展

背景说明

突发奇想写个lua脚本玩玩,我自己使用的是Mac pro m1版本的电脑,这里就不说brew啦,版本不够就update,
nginx是使用brew安装的v1.21.6,安装过程波折不断,下面是我成功后梳理的过程,写写画画。

模块下载准备

luaJIT下载

直接brew 安装即可

brew install openresty

openresty下集成了LuaJit,我也尝试了直接 brew install luajit,最后启动nginx的时候会报错,会提醒让从https://openresty.org/en/download.html下载。

安装成功之后需要设置LuaJit环境变量(有坑,后面会说~):

export PATH="/opt/homebrew/opt/luajit-openresty/bin:$PATH" //自己使用用的
export LUAJIT_LIB=/opt/homebrew/opt/luajit-openresty/lib //参与nginx 编译使用的 
export LUAJIT_INC=/opt/homebrew/opt/luajit-openresty/include/luajit-2.1 //参与nginx 编译使用的

echo $LUAJIT_LIB //测试是否生效

lua-nginx-module下载

下载 lua-nginx-module-0.10.22.tar.gz 解压,后期需要解压后的源码目录:
github下载地址: https://github.com/openresty/lua-nginx-module/
我下载的 v0.10.22,尝试过v0.10.19,启动的时候提醒我版本低,让不低于v0.10.22,应该是相互有依赖。

ngx_devel_kit下载

下载 v0.3.0.tar.gz 解压,后期需要解压后的源码目录:
github下载地址: https://github.com/simpl/ngx_devel_kit/
这个我随便下的v0.3.0版本,过程中没出问题

nginx 源码下载

一定要下载当前brew安装的nginx版本源码(brew info nginx ),防止有啥问题出现

~> nginx -V //找到当前nginx编译信息

组装编译信息

./configure .....之前的配置.....(最后保持之前的配置不变,编译后各种配置文件还能对上)

//重点是后面追加下面这两句
--add-module=/Users/laochentou/Downloads/lua-nginx-module-0.10.22 \ //解压后的lua-nginx-module目录
--add-module=/Users/laochentou/Downloads/ngx_devel_kit-0.3.0 \ //解压后的ngx_devel_kit目录

编译安装

sudo ./configure ....上面提到的编译信息..... 

报错:

在这里插入图片描述
上面提到的坑!! 说没有找到安装的LuaJIT、折腾后发现其实是安装根本没去找配置的环境变量,这里找到的解决办法是:

~> vi {解压后的lua-nginx-module目录}/config

在这里插入图片描述
直接在安装配置首部写上这俩变量,这样下面的程序就能找到了,这样这个问题句就得以解决啦。

~> sudo make  //开始编译

报错:

在这里插入图片描述
pcre.h找不到,正常要是你装过nginx,应该你已经装过pcre,没有的话可以通过brew install pcre 安装,我这里是装过的,为啥呢??
./configure 结束后的提示
这是./configure 结束后的提示,原来mac 电脑默认走的是pcre2,这里需要走的是pcre才可以。

解决办法:

下载pcre源码: https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download

--with-pcre=/Users/laochentou/Downloads/pcre-8.45 //./configure 后面 把这个with-pcre指到下载的源码目录里,注意是源码目录,不是安装目录

./configure 后面把这个**–with-pcre**指到下载的pcre源码目录里,注意是源码目录,不是安装目录。

重新./configure ,然后重新make,然后就可以顺利通过了。

make之后 会在编译目录的 objs目录下生成nginx可执行文件 ,可以尝试启动
会发现如下问题:

在这里插入图片描述
需要加载 resty.core,据说之前可以通过配置参数屏蔽这个加载,不过现在因为安全问题废弃啦。

需要下载:
https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.24.tar.gz
https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.13.tar.gz

安装:

~> tar zxf lua-resty-core-0.1.24.tar.gz
~> cd lua-resty-core-0.1.24
~> make install prefix=/opt/homebrew/Cellar/luajit-openresty/2.1-20230119/core //目录我在luajit-openresty安装目录下创建了core 
~> tar zxf lua-resty-lrucache-0.13.tar.gz
~> cd lua-resty-lrucache-0.13
~> make install prefix=/opt/homebrew/Cellar/luajit-openresty/2.1-20230119/core //目录和上面的一样就行

安装好之后,在之前nginx 的配置文件nginx.conf的http里添加一下代码:

lua_package_path "/opt/homebrew/Cellar/luajit-openresty/2.1-20230119/core/lib/lua/?.lua;;"; 这样nginx 就可以加载上resty.core 啦

再次尝试启动nginx,启动成功!,最后:

sudo make install //我是直接安装的,没有问题,或者可以直接复制objs/nginx 替换之前的nginx执行文件,注意先备份之前的,别得不偿失。

测试lua脚本

在nginx.conf 的localhost server 中添加:

location /lua {
  default_type 'text/plain';
  content_by_lua 'ngx.say("hello, lua")';
}

浏览器访问: http://localhost/lua 可以啦!!!

在这里插入图片描述

其他

重装之后我这 brew services start nginx 不能用了,可以尝试:

launchctl unload -w /Users/chenpengfei/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w /Users/chenpengfei/Library/LaunchAgents/homebrew.mxcl.nginx.plist

刷下就好啦。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值