转载专用:
读到了好文章,用于分享收藏,侵权删。
转发自大佬:wx5a98a78793203,https://blog.51cto.com/u_13620944/5254763
目录
一、安装openresty
1、安装yum工具箱
2、添加openresty源
3、安装openresty(代替Nginx)
4、配置环境变量
5、定义openresty的启动脚本:
二、安装 sockproc
1、创建文件夹并进入
2、安装工具
3、克隆软件包
4、安装编译环境
5、编译与处理
三、安装lua-resty-shell模块
1、克隆软件包
2、下载完成解压,把lib/resty/shell.lua 这个文件复制给openresty
四、配置openresty内置的nginx
1、热加载nginx配置文件
2、修改nginx配置文件,为调用Shell脚本做准备
增加一个localtion 配置:
lua文件可以在任意位置,具体demo如下:
重启openresty,访问http://ip/test可以看到ls的输出
yum install yum-utils -y
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty -y
#/usr/local/openresty/nginx/sbin其中/usr/local/openresty/nginx为yum的默认安装目录
echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile
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
mkdir -pv /southtv/openresty
cd /southtv/openresty
yum install git -y
git clone https://github.com/juce/sockproc
yum install gcc -y
cd 软件包所在路径
make
./sockproc /var/run/shell.sock
chmod 0666 /var/run/shell.sock
git clone https://github.com/juce/lua-resty-shell
cp lib/resty/shell.lua /usr/local/openresty/lualib/resty/
#其实上面的操作只是为了拉取这个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
首先可以通过whereis nginx 查看nginx安装的位置,上文安装的默认的ngxin在/usr/local/openresty/nginx/sbin/nginx目录下,常用命令:
/usr/local/openresty/nginx/sbin/nginx -s reload
vim /usr/local/openresty/nginx/conf/nginx.conf
location = /test {
content_by_lua_file /lyg/tools/test.lua;
}
vim /lyg/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