实战:Nginx集成Lua脚本并调用memcached

    xiaoxiao2022-07-14  161

    之前在阿里云的centos系统上已经安装过nginx,但是没有安装lua所需的模块,因此重新编译nginx来扩展lua模块。

    ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本。这里主要是示范一下,如何在Nginx下安装lua-nginx-module模块

     

    首先先安装好:

    yum -y install openssl openssl-devel       注意:ubuntu系统安装openssl-devel 命令:sudo apt-get install libssl-dev

    yum install lua-devel

    1)首先下载安装Lua脚本解释器 LuaJIT

    wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz

    tar -zxvf  LuaJIT-2.0.2.tar.gz

    cd LuaJIT-2.0.2

    make install PREFIX=/usr/local/LuaJIT

    ==>在/etc/profile 文件中加入环境变量

    #luajit export LUAJIT_LIB=/usr/local/LuaJIT/lib

    export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

    2) 下载ngx_devel_kit(NDK)模块 :https://github.com/simpl/ngx_devel_kit/tags,不需要安装

    cd /usr/local/src wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz tar -xzvf v0.2.19.tar.gz

    3)下载最新的lua-nginx-module 模块 :https://github.com/openresty/lua-nginx-module/tags,不需要安装 

    cd /usr/local/src wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz tar -xzvf v0.10.2.tar.gz

    4)通过nginx -V 获得之前编译的模块信息,拷贝下来。

    --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' 

    5)下载nginx的源码包

    wget http://nginx.org/download/nginx-1.16.0.tar.gz

    tar -zxvf nginx-1.16.0.tar.gz

    cd nginx-1.16.0  

    6)加入lua模块,重新编译安装nginx

    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'  --add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.9rc7  

    7)make & make install

    8)  加载lua库,加入到ld.so.conf文件

    9)  echo "/usr/local/lib" >> /etc/ld.so.conf

    10) ldconfig

    11)  nginx -V查看

    至此nginx加载lua完成;测试一下:

    配置如下

    访问:


    接下来就是lua调用memcached数据库;

    首先,安装memcached:yum install memcached

    然后,在memchched设置一个key为 “183.6.159.136”,value为1的键值对,方便后面测试。

    下载lua-resty-memchached,此模块是lua链接memcached数据库的开发包。下图中复制的路径自己决定;

    在nginx中配置:

    上图红色框框是添加库文件路径,参考 ttps://blog.csdn.net/langeldep/article/details/8289199

    当我们访问host/luatest时,nginx会去执行dep.lua脚本文件

    对应的dep.lua文件:

    在lua脚本中,我们链接memcached,获取键值,打印输出给客户端;

    当我们在浏览器访问:

    可以正确显示,至此nginx运行lua脚本,lua脚本连接memcached的实战完成

     

     

    参考:

    当然,如果你之前没有安装过Nginx,而且嫌安装麻烦,可直接下载openresty安装简单快捷,http://openresty.org/cn/installation.html(阿里的大牛章亦春的作品,膜拜~~~)

    https://blog.csdn.net/qq_25551295/article/details/51744815

    http://www.imooc.com/article/19597

    https://www.imooc.com/article/67583

    最新回复(0)