执行shell命令
安装 sockproc
# 安装sockprocgit clone https://github.com/juce/sockproc.gitcd sockproc/gcc -o sockproc ./sockproc.c# 创建Unix套接字,提供给lua的shell库连接./sockproc /tmp/shell.sockchmod 0666 /tmp/shell.sock
安装 lua-resty-shell
git clone https://github.com/juce/lua-resty-shell# 复制库到openresty下cp lua-resty-shell/lib/resty/shell.lua /usr/local/openresty/lualib/resty/
修改nginx配置文件
vi /usr/local/openresty/nginx/conf/nginx.conf ,在 server 节点下加入如下内容:
location /shell_test { content_by_lua_block { local shell = require("resty.shell") local args = { socket = "unix:/tmp/shell.sock", } local status, out, err = shell.execute("uname -a", args) ngx.header.content_type = "text/plain" ngx.say("Hello from:\n" .. out) } }
- 重启
openresty - /usr/local/openresty/bin/openresty -s reload - 访问地址
/shell_test
