非openresty方式安装Nginx + Lua + Redis 环境

一、功能实现介绍:

使用Redis做分布式缓存;使用lua API来访问redis缓存;使用nginx向客户端提供服务,ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。url请求nginx服务器,然后lua查询redis,返回json数据。

二、服务器环境介绍:

系统:CentOS release 6.8 (Final) x86_64位 最小化安装
服务器型号:PowerEdge R630 磁盘是ssd盘
服务器上已存在服务: 是一件安装包lnmp环境
服务版本:nginx/1.10.0 PHP/5.5.36 mysql/5.6.20
lnmp一键安装包下的nginx编译安装的模块如下:

[root@localhost ~]# nginx -V
nginx version: nginx/1.10.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

三、编译安装前先下载相关的nginx第三方模块并且yum安装相关的依赖包:

yum -y install pcre pcre-devel openssl openssl-devel lua-devel

下载相关的nginx第三方模块:
3.1.模块存放目录:/opt/soft
3.2.下载相关的软件包和模块

3.2.1下载ngx_devel_kit

(NDK(nginx development kit)模块,是一个拓展nginx服务器核心功能的模块,第三方模块开发可以基于它来快速实现。

 cd /opt/soft   -C /opt
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar xf v0.3.0.tar.gz   -C /opt

3.2.2 下载lua-nginx-module模块

可在 Nginx 中嵌入 Lua 语言,让 Nginx 可以支持 Lua 强大的语法。

cd /opt/soft
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz
tar xf v0.10.7.tar.gz   -C /opt 

3.2.3redis2-nginx-module下载

是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。

cd /opt/soft
wget https://github.com/openresty/redis2-nginx-module/archive/v0.13.tar.gz
tar  xf v0.13.tar.gz   -C /opt

3.2.4set-misc-nginx-module下载

是标准的HttpRewriteModule指令的扩展,提供更多的功能,如URI转义与非转义、JSON引述,Hexadecimal、MD5、SHA1、Base32、Base64编码与解码、随机数等等

cd /opt/soft
wget https://github.com/openresty/set-misc-nginx-module/archive/v0.31.tar.gz
tar  xf v0.31.tar.gz   -C /opt 

3.2.5echo-nginx-module下载

是一个 Nginx 模块,提供直接在 Nginx 配置使用包括 "echo", "sleep", "time" 等指令。

cd /opt/soft
wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
tar xf v0.60.tar.gz  -C /opt 

3.2.6Nginx下载

cd /opt/soft
wget http://nginx.org/download/nginx-1.10.0.tar.gz
tar xf nginx-1.10.0.tar.gz -C /opt

3.3.8pcre-8.40.tar.gz下载

如果 服务器没有安装pcre,在编译完nginx后执行make install时会报错,所以要在编译nginx时,提前编译安装好pcre-8.40.tar.gz

 cd /opt/soft
https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
tar xf pcre-8.40.tar.gz  -C /opt/

3.3.9redis-3.2.8.tar.gz下载并编译安装完成

四.安装软件:

4.1查看所有下载完的包

[root@localhost vhost]# ll /opt/soft/
总用量 306328
-rw-r--r--  1 root root    847615 5月  15 2015 LuaJIT-2.0.4.tar.gz
-rw-r--r--  1 root root    908954 4月  26 2016 nginx-1.10.0.tar.gz
-rw-r--r--  1 root root   2065161 1月  22 2017 pcre-8.40.tar.gz
-rw-r--r--  1 root root   1547237 2月  12 2017 redis-3.2.8.tar.gz
-rw-r--r--  1 root root    605171 6月   1 10:19 v0.10.7.tar.gz
-rw-r--r--  1 root root     27259 6月   1 10:19 v0.13.tar.gz
-rw-r--r--  1 root root     66455 6月   1 10:18 v0.3.0.tar.gz
-rw-r--r--  1 root root     29359 6月   1 10:20 v0.31.tar.gz
-rw-r--r--  1 root root     52771 6月   1 10:20 v0.60.tar.gz

安装包解压后的路径:
[root@localhost opt]# ll /opt/

总用量 7416
drwxrwxr-x   5 root root    4096 7月   1 2016 echo-nginx-module-0.60
drwxr-xr-x   6 1000 1000    4096 5月  15 2015 LuaJIT-2.0.4
drwxrwxr-x  10 root root    4096 11月  5 2016 lua-nginx-module-0.10.7
drwxr-xr-x   9 1001 1001    4096 6月   1 10:42 nginx-1.10.0
-rwxr-xr-x   1 root root 6302513 6月   1 09:54 nginx.bak
drwxrwxr-x   9 root root    4096 5月  10 2016 ngx_devel_kit-0.3.0
drwxr-xr-x   9 1169 1169   12288 6月   1 10:59 pcre-8.40
drwxrwxr-x   6 root root    4096 5月  16 2016 redis2-nginx-module-0.13
drwxrwxr-x   6 root root    4096 2月  12 2017 redis-3.2.8
drwxrwxr-x   5 root root    4096 8月  16 2016 set-misc-nginx-module-0.31
drwxr-xr-x   2 root root    4096 6月   1 10:53 soft
[root@localhost opt]# 

4.2安装luajit:

cd /opt/soft
wget  http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xf  LuaJIT-2.0.4.tar.gz  -C /opt/
cd  /opt/LuaJIT-2.0.4
make &&  make install PREFIX=/usr/local/luajit  
cp /usr/local/luajit/bin/luajit-2.0.4 /usr/local/bin/luajit 

下面需要配置一下 luajit的环境变量(Nginx编译时需要)

[root@localhost bin]# tail -2 /etc/profile
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
[root@localhost bin]# source /etc/profile

4.3是安装pcre:

cd /opt/soft
wget    https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
tar -zxvf   pcre-8.40.tar.gz -C  /opt/
cd pcre-8.40
./configure
make
make install

4.4编译安装nginx:

[root@localhost bin]# cd /opt/nginx-1.10.0/
./configure  --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module  --with-http_addition_module --with-http_realip_module  --with-pcre=/opt/pcre-8.40  --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
--add-module=../ngx_devel_kit-0.3.0 \
--add-module=../echo-nginx-module-0.60 \
--add-module=../lua-nginx-module-0.10.7 \
--add-module=../redis2-nginx-module-0.13 \
--add-module=../set-misc-nginx-module-0.31
make -j2 
make install  ####切记在make install 之前一定记得把原来 /usr/local/nginx/sbin/nginx 的nginx做备份

4.5安装lua-redis-parser

lua-resty-redis是openresty(1.9.15.1)的一个组件,简单来说,它提供一个lua语言版的redis API,使用socket(lua sock)和redis通信。
下载源码包:

cd /opt
git clone https://github.com/openresty/lua-resty-redis.git
mkdir  /usr/local/nginx/lua/  -p 
移动该源码包到/usr/local/nginx/lua/ 这里去
mv lua-resty-redis /usr/local/nginx/lua/

把lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;"; 加入到 nginx.conf配置文件

[root@localhost opt]# grep lua_package_path /usr/local/nginx/conf/nginx.conf
        lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";

到此处,环境已经部署完成,小伙伴们期待接下来的演示吧

五、常见的误区解释说明

5.1redis2-nginx-module和lua-resty-redis

redis2-nginx-module是一个openresty(1.9.15.1)自带的模块。它能够把请求转发给upstream(redis2_pass)。注意它和lua-resty-redis不同,lua-resty-redis是一个lua语言版的redis API,使用socket(lua sock)和redis通信。而redis2-nginx-module是把请求转发给别的upstream。

5.2编译安装报错之一:

objs/addon/src/ngx_http_lua_regex.o: In function `ngx_http_lua_regex_free_study_data':
/opt/nginx-1.10.0/../lua-nginx-module-0.10.7/src/ngx_http_lua_regex.c:1959: undefined reference to `pcre_free_study'
objs/addon/src/ngx_http_lua_regex.o: In function `ngx_http_lua_ffi_destroy_regex':
/opt/nginx-1.10.0/../lua-nginx-module-0.10.7/src/ngx_http_lua_regex.c:2353: undefined reference to `pcre_free_study'
collect2: ld 返回 1
make[1]: *** [objs/nginx] 错误 1
make[1]: Leaving directory `/opt/nginx-1.10.0'
make: *** [build] 错误 2

解决问题参考地址:
https://www.ctolib.com/topics-100588.html

于是安装pcre
wget    https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
tar -zxvf   pcre-8.40.tar.gz
cd pcre-8.40
./configure
make
make install

重新编译安装nginx,问题解决

猜你喜欢

转载自blog.51cto.com/wujianwei/2123177