×

Nginx + LuaJIT + Webhook (FFI法)

2021-04-16 19:40:00 Falcon

Install

  • LuaJIT
  • ngx_devel_kit
  • lua-nginx-module
  • Nginx

Base

apt install make gcc git libpcre3 libpcre3-dev zlib1g-dev libssl-dev

Download and DeCompression

cd /usr/local/src

wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar -zxvf LuaJIT-2.0.5.tar.gz
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
tar -zxvf v0.3.1rc1.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.12rc2.tar.gz
tar -zxvf v0.10.12rc2.tar.gz
wget http://nginx.org/download/nginx-1.13.12.tar.gz
tar -zxvf ~/nginx-1.13.12.tar.gz

Install LuaJIT

cd LuaJIT-2.0.5
make install PREFIX=/usr/local/luajit

echo "export LUAJIT_LIB=/usr/local/luajit/lib" > /etc/profile.d/luajit.sh
echo "export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0" >> /etc/profile.d/luajit.sh
source /etc/profile.d/luajit.sh

Install Nginx

cd ../nginx-1.13.12

./configure --add-module=/usr/local/src/ngx_devel_kit-0.3.1rc1 --add-module=/usr/local/src/lua-nginx-module-0.10.12rc2
make -j2 && make install

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Configure

Server

mkdir -p /var/www/.ssh /var/www/html
chown -R www-data:www-data /var/www
sudo -Hu www-data ssh-keygen -t rsa
sudo -Hu www-data git clone git@github.com:yourname/yourrepo.git /var/www/html

add in nginx.conf

location /webhook {
    default_type 'text/plain';
    content_by_lua_file /var/www/webhook.lua;
}

/var/www/webhook.lua

local ffi = require("ffi")
local args = nil
local token = 'anything what your set for github secret'
local request_method = ngx.var.request_method
if "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
    ffi.cdef[[int system(const char * string);]]
    if args['token'] == token and ffi.C.system("cd /var/www/html && git pull") then
        ngx.say("ok")
    else
        ngx.say("fuck")
    end
else
    ngx.say("fuck")
end

Github

Deploy keys

https://github.com/yourname/yourrepo/settings/keys

Add keys what you made just now

/var/www/.ssh/id_rsa.pub

WebHooks

https://github.com/yourname/yourrepo/settings/hooks

Secret is your token for /var/www/webhook.lua

Finish

Enjoy yourself!

本文收录于