nginx安装lua+redis_nginx lua redis-程序员宅基地

技术标签: nginx  lua  

1.安装lua

1.1安装LuaJIT

[root@localhost ~]# yum install readline-devel
[root@localhost ~]# mkdir -p lua-file
[root@localhost ~]# cd lua-file/
[root@localhost lua_file]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
[root@localhost lua_file]# tar -zxvf LuaJIT-2.0.5.tar.gz 
[root@localhost lua_file]# ls
LuaJIT-2.0.5  LuaJIT-2.0.5.tar.gz
[root@localhost lua_file]# cd LuaJIT-2.0.5
[root@localhost LuaJIT-2.0.5]# make && make install PREFIX=/usr/local/luajit

1.2配置LuaJIT环境变量

[root@localhost lua-file]# vim /etc/profile

#/etc/profile 文件中加入环境变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

[root@localhost lua-file]# source /etc/profile

1.3 lua安装测试

[root@localhost lua-file]# vim hello.lua
print('hello world lua');

[root@localhost lua-file]# lua hello.lua 
hello world lua

2.ngx_devel_kit和lua-nginx-module

ngx_devel_kit简称NDK,提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量。
lua-nginx-module是nginx的lua模块

[root@localhost ~]# mkdir -p /usr/local/src/nginx
[root@localhost ~]# cd /usr/local/src/nginx/
[root@localhost nginx]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
[root@localhost nginx]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
[root@localhost nginx]# ls
v0.10.9rc7.tar.gz  v0.3.0.tar.gz
[root@localhost nginx]# tar -zxvf v0.10.9rc7.tar.gz 
[root@localhost nginx]# tar -zxvf v0.3.0.tar.gz 

3.查看已安装好的nginx

3.1查看原安装

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module

在这里插入图片描述
3.2 安装依赖

[root@localhost nginx]# yum -y install openssl openssl-devel zlib zlib-devel pcre-devel

3.3 进入nginx解压目录,安装扩展

[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# 

3.3.1.清空之前的编译文件

[root@localhost nginx-1.14.2]# make clean

3.3.2 执行 nginx -V 查看之前的编译参数

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module

3.3.3 添加扩展执行编译

[root@localhost nginx-1.14.2]# ls /usr/local/src/nginx/
lua-nginx-module-0.10.9rc7  ngx_devel_kit-0.3.0  v0.10.9rc7.tar.gz  v0.3.0.tar.gz
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx/lua-nginx-module-0.10.9rc7/ --add-module=/usr/local/src/nginx/ngx_devel_kit-0.3.0/

3.3.4.执行make (不能执行 make install)

[root@localhost nginx-1.14.2]# make

3.3.5.(打开新连接窗口操作)重命名 nginx 旧版本二进制文件,即 sbin 目录下的 nginx(期间 nginx 并不会停止服务)

客户端2
[root@localhost nginx-1.14.2]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx.old
[root@localhost sbin]# ls
nginx.old

3.3.6.拷贝一份新编译的二进制文件到安装目录

[root@localhost nginx-1.14.2]# pwd
/root/nginx-1.14.2
[root@localhost nginx-1.14.2]# cd objs/
[root@localhost objs]# ll
total 9124
drwxr-xr-x. 3 root root      17 Nov 16 22:55 addon
-rw-r--r--. 1 root root   18837 Nov 16 22:55 autoconf.err
-rw-r--r--. 1 root root   68709 Nov 16 22:55 Makefile
-rwxr-xr-x. 1 root root 9132336 Nov 16 22:57 nginx
-rw-r--r--. 1 root root    5341 Nov 16 22:57 nginx.8
-rw-r--r--. 1 root root    7493 Nov 16 22:55 ngx_auto_config.h
-rw-r--r--. 1 root root     657 Nov 16 22:55 ngx_auto_headers.h
-rw-r--r--. 1 root root    6209 Nov 16 22:55 ngx_modules.c
-rw-r--r--. 1 root root   86808 Nov 16 22:57 ngx_modules.o
drwxr-xr-x. 9 root root      91 Nov 16 22:55 src
[root@localhost objs]# cp nginx /usr/local/nginx/sbin/
[root@localhost objs]# ls /usr/local/nginx/sbin/
nginx  nginx.old

在这里插入图片描述在这里插入图片描述
3.3.7.在源码目录执行 make upgrade 开始升级

[root@localhost nginx-1.14.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

###查看是否安装成功
[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx/lua-nginx-module-0.10.9rc7/ --add-module=/usr/local/src/nginx/ngx_devel_kit-0.3.0/

在这里插入图片描述在这里插入图片描述
启动nginx,如果有这个报错:nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory在这里插入图片描述

#解决方案
[root@localhost nginx-1.14.2]# vim /etc/ld.so.conf
#新增一条
 /usr/local/luajit/lib

#保存文件,执行ldconfig 	
[root@localhost nginx-1.14.2]# ldconfig

4 nginx引入lua调试

4.1 nginx.conf引入lua

[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf

	#关闭代码缓存 。修改lua脚本不需要重启
    lua_code_cache off;

	 #测试lua
    server {
        listen 81;
        server_name localhost;

        #设置变量
        set $test "hello world lua";
        location /lua_test {
                content_by_lua '
                        ngx.header.content_type="text/plain";
                        ngx.say(ngx.var.test);
                ';
        }
    }

在这里插入图片描述

4.2 重启nginx,执行访问

[root@localhost nginx]# killall nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:22

在这里插入图片描述
浏览器执行访问:
在这里插入图片描述

5.实现lua连接redis

redis集群扩展包:https://github.com/onlonely/lua-redis-cluster
对于lua来说,它是一个非常轻量级的脚本语言,而它本身也与php的composer是一样的,是可以添加扩展,只是他们的扩展我们需要自己找对应的模块库
官方组件:https://openresty.org/cn/components.html

5.1 下载lua-redis-cluster(连接redis-cluster集群) 客户端

[root@localhost 5.1]# pwd
/usr/local/luajit/share/lua/5.1
[root@localhost 5.1]# git clone https://github.com/onlonely/lua-redis-cluster.git
[root@localhost 5.1]# ls
lua-redis-cluster

5.2 下载快速JSON编码/解析

[root@localhost 5.1]# git clone https://github.com/openresty/lua-cjson.git
[root@localhost 5.1]# ls
lua-cjson  lua-redis-cluster
[root@localhost 5.1]# 

5.3 下载 lua-resty-redis (单机版redis)客户端

[root@localhost 5.1]# git clone https://github.com/openresty/lua-resty-redis
[root@localhost 5.1]# ls
lua-cjson  lua-redis-cluster  lua-resty-redis

5.4 lua连接单机版本redis

资料地址:https://github.com/openresty/lua-resty-redis

5.4.1编辑nginx.conf脚本

	#关闭代码缓存 。修改lua脚本不需要重启
    lua_code_cache off;
    #引入lua-reds 扩展库(此处可以在lua脚本里面引入,公共的也可以在此引入)
    lua_package_path "/usr/local/luajit/share/lua/5.1/lua-resty-redis/lib/?.lua;;";

    #连接单机版本的redis
    server {
        listen 82;
        server_name localhost;
        location /lua_redis {
             default_type 'application/x-javascript;charset=utf-8';
             #引入redis
             #content_by_lua_file /usr/local/nginx/conf/lua/lua_test.lua;               
             content_by_lua_file /usr/local/nginx/conf/lua/lua_redis.lua;
        }
    }

截图nginx.conf:
在这里插入图片描述

5.4.2 编辑连接redis的lua脚本

[root@localhost conf]# vim lua/lua_redis.lua 

--引入redis扩展包
package.path ="/usr/local/luajit/share/lua/5.1/lua-resty-redis/lib/?.lua;;"

local redis = require "resty.redis"
local red = redis:new()

red:set_timeouts(1000, 1000, 1000) -- 1 sec

--lua 连接redis
local ok,err = red:connect("127.0.0.1", 6379)

if not ok then
       ngx.say("failed to connect: ", err)
       return
end


--设置数据
local date=os.date("%Y-%m-%d %H:%M:%S")
ok,err = red:set("dog", date.."-数据")
if not ok then
       ngx.say("failed to set dog: ", err)
       return
end


--获取设置的值
local res,err=red:get("dog")
if not res then
	ngx.say("failed to get dog",err)
	return
end

ngx.say(res)

截图lua_redis.lua :
在这里插入图片描述

5.4.3 重启nginx,访问nginx,数据成功写入redis,并读取

[root@localhost lua]# /usr/local/nginx/sbin/nginx -s reload

#(重启报错,自行查看nginx 的 error.log文件,具体根据自行的错误日志输错文件目录查看)
[root@localhost lua]# ls /usr/local/nginx/logs/
access.log  error.log

在这里插入图片描述

5.5 lua连接redis-cluster 集群

资料地址:
https://github.com/onlonely/lua-redis-cluster (以下demo使用该资源)
https://github.com/steve0511/resty-redis-cluster

5.5.1自行准备redis-cluster集群

5.5.2 编写连接redis-cluster的lua脚本

[root@localhost conf]# vim lua/lua_redis_cluster.lua

--引入redis-cluster扩展包
--package.path ="/usr/local/luajit/share/lua/5.1/lua-redis-cluster/?.lua;;/usr/local/luajit/share/lua/5.1/lua-resty-redis/lib/?.lua;;"
package.path ="/usr/local/luajit/share/lua/5.1/lua-redis-cluster/?.lua;;"


ngx.header.content_type="text/plain"
-- 构建redis集群
local function redis_cluster_init ()
    local redis_cluster = require "resty.redis_cluster"
    local cluster_id = "redis_cluster"
    local startup_nodes = {
        {"192.168.11.124",6391},
		{"192.168.11.124",6392},
		{"192.168.11.124",6393},
		{"192.168.11.124",6394},
		{"192.168.11.124",6395},
		{"192.168.11.124",6396},
    }
    local opt = {
    	  timeout = 1000,--执行超时时间
    	  keepalive_size = 50,--长连接数量
        keepalive_duration = 60000 --长连接保持时间
    }
    rc = redis_cluster:new(cluster_id, startup_nodes, opt)
    return rc
end
local redis = redis_cluster_init();
--调用API获取数据
--设置商品库存
local key = "goods_stock_12"
local res, err = redis:set(key,100)
if not res then
    ngx.say("set msg error : ", err)
end

--获取商品库存
local stock, err = redis:get(key)
if not stock then
    ngx.say("get msg error : ", err)
end
--得到的数据为空处理
if stock == ngx.null then
    stock = 0  --比如默认值
end
ngx.say("goods stock : ", stock)

截图lua_redis_cluster.lua:
在这里插入图片描述

5.5.3 lua-redis-cluster 模块存在问题,需要修改底层源码

[root@localhost resty]# pwd
/usr/local/luajit/share/lua/5.1/lua-redis-cluster/resty
#修改此文件
[root@localhost resty]# vim redis_cluster.lua

#修改的方法
function _M.initialize(self)
		....
		--替换此处代码
				 if addr_str == ":0" then
                    addr = { node[1],
                             tonumber(node[2]),
                             node[1] .. ":" .. tostring(node[2]) }
                else
                    local host_port = string_split(addr_str, ":", 2)
                    local ports = string_split(host_port[2], "@", 2)
                    addr = { host_port[1],
                             tonumber(ports[1]),
                             addr_str }
                end
		....
end

截图提示redis_cluster.lua:
在这里插入图片描述

5.5.4 重启nginx,浏览器访问,数据成功写入redis-cluster集群
在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yinjinshui/article/details/109738580

智能推荐

解决win10/win8/8.1 64位操作系统MT65xx preloader线刷驱动无法安装_mt65驱动-程序员宅基地

文章浏览阅读1.3w次。转载自 http://www.miui.com/thread-2003672-1-1.html 当手机在刷错包或者误修改删除系统文件后会出现无法开机或者是移动定制(联通合约机)版想刷标准版,这时就会用到线刷,首先就是安装线刷驱动。 在XP和win7上线刷是比较方便的,用那个驱动自动安装版,直接就可以安装好,完成线刷。不过现在也有好多机友换成了win8/8.1系统,再使用这个_mt65驱动

SonarQube简介及客户端集成_sonar的客户端区别-程序员宅基地

文章浏览阅读1k次。SonarQube是一个代码质量管理平台,可以扫描监测代码并给出质量评价及修改建议,通过插件机制支持25+中开发语言,可以很容易与gradle\maven\jenkins等工具进行集成,是非常流行的代码质量管控平台。通CheckStyle、findbugs等工具定位不同,SonarQube定位于平台,有完善的管理机制及强大的管理页面,并通过插件支持checkstyle及findbugs等既有的流..._sonar的客户端区别

元学习系列(六):神经图灵机详细分析_神经图灵机方法改进-程序员宅基地

文章浏览阅读3.4k次,点赞2次,收藏27次。神经图灵机是LSTM、GRU的改进版本,本质上依然包含一个外部记忆结构、可对记忆进行读写操作,主要针对读写操作进行了改进,或者说提出了一种新的读写操作思路。神经图灵机之所以叫这个名字是因为它通过深度学习模型模拟了图灵机,但是我觉得如果先去介绍图灵机的概念,就会搞得很混乱,所以这里主要从神经图灵机改进了LSTM的哪些方面入手进行讲解,同时,由于模型的结构比较复杂,为了让思路更清晰,这次也会分开几..._神经图灵机方法改进

【机器学习】机器学习模型迭代方法(Python)-程序员宅基地

文章浏览阅读2.8k次。一、模型迭代方法机器学习模型在实际应用的场景,通常要根据新增的数据下进行模型的迭代,常见的模型迭代方法有以下几种:1、全量数据重新训练一个模型,直接合并历史训练数据与新增的数据,模型直接离线学习全量数据,学习得到一个全新的模型。优缺点:这也是实际最为常见的模型迭代方式,通常模型效果也是最好的,但这样模型迭代比较耗时,资源耗费比较多,实时性较差,特别是在大数据场景更为困难;2、模型融合的方法,将旧模..._模型迭代

base64图片打成Zip包上传,以及服务端解压的简单实现_base64可以装换zip吗-程序员宅基地

文章浏览阅读2.3k次。1、前言上传图片一般采用异步上传的方式,但是异步上传带来不好的地方,就如果图片有改变或者删除,图片服务器端就会造成浪费。所以有时候就会和参数同步提交。笔者喜欢base64图片一起上传,但是图片过多时就会出现数据丢失等异常。因为tomcat的post请求默认是2M的长度限制。2、解决办法有两种:① 修改tomcat的servel.xml的配置文件,设置 maxPostSize=..._base64可以装换zip吗

Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字-程序员宅基地

文章浏览阅读1k次,点赞17次,收藏22次。Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字

随便推点

ESXi 快速复制虚拟机脚本_exsi6.7快速克隆centos-程序员宅基地

文章浏览阅读1.3k次。拷贝虚拟机文件时间比较长,因为虚拟机 flat 文件很大,所以要等。脚本完成后,以复制虚拟机文件夹。将以下脚本内容写入文件。_exsi6.7快速克隆centos

好友推荐—基于关系的java和spark代码实现_本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。-程序员宅基地

文章浏览阅读2k次。本文主要实现基于二度好友的推荐。数学公式参考于:http://blog.csdn.net/qq_14950717/article/details/52197565测试数据为自己随手画的关系图把图片整理成文本信息如下:a b c d e f yb c a f gc a b dd c a e h q re f h d af e a b gg h f bh e g i di j m n ..._本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。

南京大学-高级程序设计复习总结_南京大学高级程序设计-程序员宅基地

文章浏览阅读367次。南京大学高级程序设计期末复习总结,c++面向对象编程_南京大学高级程序设计

4.朴素贝叶斯分类器实现-matlab_朴素贝叶斯 matlab训练和测试输出-程序员宅基地

文章浏览阅读3.1k次,点赞2次,收藏12次。实现朴素贝叶斯分类器,并且根据李航《统计机器学习》第四章提供的数据训练与测试,结果与书中一致分别实现了朴素贝叶斯以及带有laplace平滑的朴素贝叶斯%书中例题实现朴素贝叶斯%特征1的取值集合A1=[1;2;3];%特征2的取值集合A2=[4;5;6];%S M LAValues={A1;A2};%Y的取值集合YValue=[-1;1];%数据集和T=[ 1,4,-1;..._朴素贝叶斯 matlab训练和测试输出

Markdown 文本换行_markdowntext 换行-程序员宅基地

文章浏览阅读1.6k次。Markdown 文本换行_markdowntext 换行

错误:0xC0000022 在运行 Microsoft Windows 非核心版本的计算机上,运行”slui.exe 0x2a 0xC0000022″以显示错误文本_错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行-程序员宅基地

文章浏览阅读6.7w次,点赞2次,收藏37次。win10 2016长期服务版激活错误解决方法:打开“注册表编辑器”;(Windows + R然后输入Regedit)修改SkipRearm的值为1:(在HKEY_LOCAL_MACHINE–》SOFTWARE–》Microsoft–》Windows NT–》CurrentVersion–》SoftwareProtectionPlatform里面,将SkipRearm的值修改为1)重..._错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行“slui.ex