1.安装openresty

yum install yum-utils -y

#安装yum工具箱

yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

#添加openresty源

yum install openresty -y

#安装openresty,这个软件代替Nginx的

​echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile

source /etc/profile

定义openresty的启动脚本:

vim /etc/init.d/openresty​

#!/bin/sh
#
# openresty - this script starts and stops the openresty daemin
#
# chkconfig: - 85 15
# description: OpenResty is a full-fledged web platform that integrates \
# the standard Nginx core, LuaJIT, many carefully written Lua libraries, \
# lots of high quality 3rd-party Nginx modules, and most of their external dependencies.
# processname: openresty
# config: /usr/local/openresty/nginx/conf/nginx.conf
# pidfile: /usr/local/openresty/nginx/logs/nginx.pid
# Url http://www.cnblogs.com/wushuaishuai/
# Last Updated 2018.07.15

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/openresty/nginx/sbin/nginx"
#程序,可以看出本身就是一个nginx
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"
#配置文件,记住这个路径,需要把原来nginx配置复制过来的
NGINX_PID="/usr/local/openresty/nginx/logs/nginx.pid"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
#service php-fpm start
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
$nginx -s stop
echo_success
retval=$?
echo
#service php-fpm stop
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
$nginx -s reload
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

2.安装 sockproc

mkdir  -pv /southtv/openresty

cd /southtv/openresty

#创建文件夹

yum install git -y

#安装工具

git clone https://github.com/juce/sockproc

#克隆软件包

yum install gcc -y

#安装编译环境

make

#编译

nginx 调用shell 脚本_nginx

./sockproc /var/run/shell.sock

chmod 0666 /var/run/shell.sock

3.安装lua-resty-shell模块

git clone https://github.com/juce/lua-resty-shell

#克隆软件包

cp lib/resty/shell.lua  /usr/local/openresty/lualib/resty/

#下载完成解压,把lib/resty/shell.lua 这个文件复制给openresty,

#其实上面的操作只是为了拉取这个shell.lua脚本文件,我们只要将下面的代码复制到 vim /usr/local/openresty/lualib/resty/shell.lua 这里面

-- Copyright (C) 2014 Anton Jouline (juce)


local format = string.format
local match = string.match
local find = string.find
local tcp = ngx.socket.tcp
local tonumber = tonumber


local shell = {
_VERSION = '0.02'
}

local default_socket = "unix:/tmp/shell.sock"


function shell.execute(cmd, args)
local timeout = args and args.timeout
local input_data = args and args.data or ""
local socket = args and args.socket or default_socket

local is_tcp
if type(socket) == 'table' then
if socket.host and tonumber(socket.port) then
is_tcp = true
else
error('socket table must have host and port keys')
end
end

local sock = tcp()
local ok, err
if is_tcp then
ok, err = sock:connect(socket.host, tonumber(socket.port))
else
ok, err = sock:connect(socket)
end
if ok then
sock:settimeout(timeout or 15000)
sock:send(cmd .. "\r\n")
sock:send(format("%d\r\n", #input_data))
sock:send(input_data)

-- status code
local data, err, partial = sock:receive('*l')
if err then
return -1, nil, err
end
local code = match(data,"status:([-%d]+)") or -1

-- output stream
data, err, partial = sock:receive('*l')
if err then
return -1, nil, err
end
local n = tonumber(data) or 0
local out_bytes = n > 0 and sock:receive(n) or nil

-- error stream
data, err, partial = sock:receive('*l')
if err then
return -1, nil, err
end
n = tonumber(data) or 0
local err_bytes = n > 0 and sock:receive(n) or nil

sock:close()

return tonumber(code), out_bytes, err_bytes
end
return -2, nil, err
end


return shell

4.配置openresty内置的nginx

首先可以通过whereis nginx 查看nginx安装的位置,按本文步骤下的ngxin在/usr/local/openresty/nginx/sbin/nginx目录下,所以也可以通过nginx reload命令单独reload nginx:

/usr/local/openresty/nginx/sbin/nginx -s reload

编辑nginx作为测试:

vim /usr/local/openresty/nginx/conf/nginx.conf

具体内容如下

#增加一个localtion 配置
location = /test {
content_by_lua_file /server/tools/remoteshell/test.lua;
}

lua文件可以在任意位置,具体demo如下:

vim /server/tools/remoteshell/test.lua    
local shell = require "resty.shell"
local args = {
socket = "unix:/var/run/shell.sock",
}
local status, out, err = shell.execute("ls", args)
ngx.header.content_type = "text/plain"
ngx.say("Result:\n" .. out)

systemctl restart openresty

重启openresty,访问http://IP/test应该就可以看到date的输出

nginx 调用shell 脚本_lua_02

#效果