mac下Nginx+lua模块编译安装

Nginx的nb之处就不说了,lua也是一个小巧的脚本语言,由标准C编写而成,几乎可以运行在所有的平台上,也非常强大,其他特性请自行度娘。
nginx_lua_module是由淘宝的工程师清无(王晓哲)和春来(章亦春)所开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力 http://wiki.nginx.org/HttpLuaModule

下面说说mac下Nginx如何编译集成nginx_lua_module模块

1. 下载nginx需要的模块源码

lua-nginx-module-0.10.5,LuaJIT,ngx_devel_kit-0.3.0,openssl,zlib
还有主要的nginx源码,具体下载地址就搜索一下吧,版本随时也会变化

2. 编译安装


2.1. 下载安装PCRE库


PCRE是为了重写URL的rewrite
如果本机安装了brew就比较方便了,直接 brew install PCRE 即可自动下载安装。否则请下载源码

./configure 
make
make install

2.2. 下载安装zlib库


zlib是为了gzip压缩使用。Brew上好像没有,所以还是源码安装。

cd /Users/hecom/Downloads/zlib-1.2.8
./configure 
make
make install

2.3. 下载安装ssl库


下载源码,解压

cd /Users/hecom/Downloads/openssl-1.0.1t
./configure 
make
make install

2.4. 下载安装luajit库


使用源码安装,http://luajit.org/download.html

cd /Users/hecom/Downloads/ LuaJIT-2.0.4t

./configure 

make
make install

lib和include的默认安装位置在/usr/local/lib和usr/local/include文件夹下面,这个很重要,需要导入环境变量使用。

2.5. 下载lua-nginx-module并解压


 我的本地路径:/Users/hecom/Downloads/lua-nginx-module-0.10.5
注意版本号

2.6. 下载ngx_devel_kit并解压


我的本地路径:/Users/hecom/Downloads/ngx_devel_kit-0.3.0
注意版本号


2.7. 下载nginx并解压


我的本地路径:/Users/hecom/Downloads/nginx-1.11.1
注意版本号


2.8. 导入环境变量(很重要)

export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0/

如果nginx无法启动,报下面错误,请检测是否执行该步骤。
[error] failed to initialize Lua VM in /usr/local/nginx/conf/nginx.conf:44


2.9. 编译安装nginx

cd /Users/hecom/Downloads/nginx-1.11.1
./configure --prefix=/usr/local/nginx #nginx安装位置 #--with-openssl=/Users/hecom/Downloads/openssl-1.0.1t #openssl的源码路径,如果不是自己编译,则不需要这个参数。
--with-http_ssl_module #开启ssl模块
--with-http_stub_status_module --with-zlib=/Users/hecom/Downloads/zlib-1.2.8 #zlib源码路径 --add-module=/Users/hecom/Downloads/lua-nginx-module-0.10.5 #源码路径 --add-module=/Users/hecom/Downloads/ngx_devel_kit-0.3.0 #ngx_devel源码路径
make make install

上面命令请在root命令下执行,即在前面加上sudo, 各个源码路径请根据自己的位置修改

自己编译openssl上走了很多弯路,一直报错:ld: symbol(s) not found for architecture x86_64,编译openssl时加 ./Configure darwin64-x86_64-cc 也不行,最后没有搞定,使用系统自带的版本通过了


3. 测试nginx


如果幸运不出错的话,nginx变安装成功了。输入下面命令启动nginx并测试,sudo /usr/local/nginx/sbin/nginx,然后在浏览器中输入:http://localhost/,如果看到下面画面恭喜你,成功了!

4. 测试lua


打开/usr/local/nginx/conf/nginx.conf文件,添加lua脚本。

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

然后执行 sudo /usr/local/nginx/sbin/nginx –s reload
在浏览器中输入 http://localhost/hello,看到输出 “hello,lua”
否则根据错误日志排查原因

原文地址:https://www.cnblogs.com/freeton/p/5573207.html