用lua+redis实现一个简单的计数器功能 (一)

时间:2023-01-13 07:34:12

首先安装环境

依赖环境有

luajit             http://luajit.org
ngx_devel_kit https://github.com/simpl/ngx_devel_kit
echo-nginx-module https://github.com/agentzh/echo-nginx-module
lua-nginx-module https://github.com/chaoslawful/lua-nginx-module
lua cjson http://www.kyne.com.au/~mark/software/lua-cjson.php

安装luajit

wget http://luajit.org/download/LuaJIT-2.0.0-beta9.tar.gz
tar zxvf LuaJIT-2.0.-beta9.tar.gz
cd LuaJIT-2.0.-beta9
make
make install PREFIX=/usr/local/luajit
ln -sf luajit-2.0.0-beta9 /usr/local/bin/luajit(根据安装提示加上)

安装ngx_devel_kit,echo-nginx-module,lua-nginx-module

yum install -y git
cd /usr/local
git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/chaoslawful/lua-nginx-module.git
git clone https://github.com/agentzh/echo-nginx-module.git

重新编译nginx依赖

export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
cd nginx-1.2.7/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 \
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" --add-module=/usr/local/ngx_devel_kit  --add-module=/usr/local/echo-nginx-module   --add-module=/usr/local/lua-nginx-module
make -j2
make install

修改nginx.conf文件并重启nginx

location /echo {
default_type 'text/plain';
echo 'hello echo';
}
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}

访问http://localhost/lua 输出hello, lua

安装lua cjson并重启nginx服务

cp -r * /usr/local/bin/luajit /usr/local/include #复制cjson依赖的 lua.h lauxlib.h的包
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
tar zxvf lua-cjson-2.1.0.tar.gz
make
make install
cp cjson.so /usr/local/luajit/lib #复制sjson.so文件到lua lib中