步骤
1.nginx集成lua
2.通过访问nginx请求去调用lua
3.通过lua访问c
第一步先下载OpenResty
OpenResty是啥就不说了,直接上官网https://openresty.org/en/download.html
我们先下载openresty-1.19.3.1.tar.gz
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
然后解压openresty
tar -vxzf openresty-1.19.3.1.tar.gz
进到openresty-1.19.3.1文件夹下
cd openresty-1.19.3.1
进行编译
#可以指定参数如安装路径,具体可参照官网https://openresty.org/cn/installation.html 这儿就全都默认配置
./configure
make #如果您的电脑支持多核 make 工作的特性, 可以用make -j2
make install
ps:编译需要依赖,没有的话需要提前安装好
yum -y install gcc pcre pcre-devel zlib zlib-devel make readline-devel
第二步通过nginx访问lua
openresty默认路径为/usr/local/openresty
cd /usr/local/openresty/nginx/conf
然后修改配置文件
vim nginx.conf
http {
.....
lua_package_cpath "/usr/local/luajit/lib/?.so;;"; #c模块
lua_package_path "/usr/local/lib/lua/?.lua;;";
.....
server {
......
#直接写nginx-lua脚本语言方式
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#lua文件方式
#在server 中添加一个localtion
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #nginx安装目录conf下
}
}
}
保存退出,然后到/usr/local/openresty/nginx/conf下创建lua目录,并创建一个test.lua文件
cd /usr/local/openresty/nginx/conf
mkdir lua
vim test.lua
test.lua:
ngx.say("hello, world")
保存退出,然后启动nginx;访问刚刚配置的路径
第三步lua访问c
首先还是在lua目录下创建一个test.c文件,写入以下内容:
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int isquare(lua_State *L){
float rtrn = lua_tonumber(L, -1);
printf("Top of square(), nbr=%f\n",rtrn);
lua_pushnumber(L,rtrn*rtrn);
return 1;
}
int luaopen_power(lua_State *L){
lua_register(L,"square",isquare);
return 0;
}
编译c文件
gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 test.c
修改刚刚的test.lua文件
vim test.lua
#!/usr/bin/lua
require("power")
ngx.say(square(3.5345353453))
最后用浏览器访问路径,成功返回。
如果报500错误查看nginx下的logs/error.log
2021/01/06 15:22:41 [error] 24260#0: *2 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/conf/lua/test.lua:7: module 'power' not found:
no field package.preload['power']
no file '/usr/local/lib/lua/power.lua'
no file '/usr/local/openresty/site/lualib/power.ljbc'
no file '/usr/local/openresty/site/lualib/power/init.ljbc'
no file '/usr/local/openresty/lualib/power.ljbc'
no file '/usr/local/openresty/lualib/power/init.ljbc'
no file '/usr/local/openresty/site/lualib/power.lua'
no file '/usr/local/openresty/site/lualib/power/init.lua'
no file '/usr/local/openresty/lualib/power.lua'
no file '/usr/local/openresty/lualib/power/init.lua'
no file './power.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/power.lua'
no file '/usr/local/share/lua/5.1/power.lua'
no file '/usr/local/share/lua/5.1/power/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/power.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/power/init.lua'
no file '/usr/local/luajit/lib/power.so'
no file '/usr/local/openresty/site/lualib/power.so'
no file '/usr/local/openresty/lualib/power.so'
no file './power.so'
no file '/usr/local/lib/lua/5.1/power.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/power.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
coroutine 0:
[C]: in function 'require'
/usr/local/openresty/nginx/conf/lua/test.lua:7: in main chunk, client: 192.168.2.243, server: localhost, request: "GET /luareq?input_str=16515 HTTP/1.1", host: "192.168.2.50:8913"
如果是这个错,把so文件拷贝到/usr/local/lib/lua/5.1/下,成功访问
噢,忘记贴LuaJIT环境变量配置了
vim /etc/profile
增加
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
export LD_LIBRARY_PATH=/usr/local/luajit/lib:$LD_LIBRARY_PATH
最后
source /etc/profile