OpenResty使用-安装及开发环境

 原创地址:http://jinnianshilongnian.iteye.com/blog/2190344

 

常用命令

# vi /usr/local/nginx/conf/nginx.conf

# vi /usr/server/example/example.conf

# /usr/local/nginx/sbin/nginx  -s reload &  tail -f /usr/local/nginx/logs/error.log

 

目录:

1.OpenRestry(Nginx+Lua)开发环境

 

2.OpenRestry开发入门

 

3.Lua模块开发

3.1 常用Lua开发库1-redis、mysql、http客户端 附-Redis/SSDB+Twemproxy安装与使用

3.2 常用Lua开发库2-JSON库、编码转换、字符串处理

3.3 常用Lua开发库3-模板渲染

 

4.实战

4.1 Web开发实战1——HTTP服务

4.2 Web开发实战2——商品详情页

4.3 流量复制/AB测试/协程

 

一.OpenRestry(Nginx+Lua)开发环境

1.Openrestry是什么

Openrestry由Nginx核心加很多第三方模块组成,默认集成了Lua开发环境,使得Nginx可以作为一个WebServer使用.

 

2.特点

(1)借助Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序

(2)OpenResty提供了大量组件如Mysql、Redis、Memcached等等,可以在Nginx上更方便更简单的开发Web应用

(3)目前在京东如实时价格、秒杀、动态服务、单品页、列表页等都在使用Nginx+Lua架构,其他公司如淘宝、去哪儿网等

 

3.安装环境

注:原创作者的环境是乌班图,我使用的是centos6.6,所以步骤和原创稍微有些区别

 

1.创建目录/usr/local/,以后我们把所有软件安装在此目录

# mkdir -p /usr/local 

# cd /usr/local/ 

 

2.安装依赖

# yum -y install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

 

3.下载ngx_openresty-1.7.7.2.tar.gz并解压

# wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz  

# tar -xzvf ngx_openresty-1.7.7.2.tar.gz 

 

4.安装LuaJIT

ngx_openresty-1.7.7.2/bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和LuaJIT

# ls ./ngx_openresty-1.7.7.2/bundle

 

# cd ./ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120/ 

# make clean && make && make install 

***********************************************************************************

可能报错:gcc: Command not found,需要先判断gcc是否安装:whereis gcc 

安装gcc:

Centos(Rehat系列)系统下载gcc命令:yum -y install gcc

Ubuntu系统下载gcc命令:apt-get inatall gcc

***********************************************************************************

# ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit  

 

5.下载ngx_cache_purge模块,该模块用于清理nginx缓存

# cd /usr/local/ngx_openresty-1.7.7.2/bundle  

# wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz  

# tar -xvf 2.3.tar.gz  

 

6.下载nginx_upstream_check_module模块,该模块用于ustream健康检查

# cd /usr/local/ngx_openresty-1.7.7.2/bundle  

# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz  

# tar -xvf v0.3.0.tar.gz   

 

7.安装ngx_openresty

针对可能的组件缺失,安装一些环境:

# yum install -y gcc gcc-c++

# yum -y install openssl-devel

(1) 上传pcre-8.34.tar.gz到/user/local (2) # cd /usr/local (3)# tar -xf pcre-8.34.tar.gz  (4)# cd pcre-8.34 (5)# ./configure  (6) # make  (7) # make install

 

# cd /usr/local/ngx_openresty-1.7.7.2  

# ./configure --prefix=/usr/local --with-http_realip_module  --with-pcre  --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2

# make && make install 

 

*********************************************************************

可能报错:

checking for PCRE library ... not found

checking for PCRE library in /usr/local/ ... not found

checking for PCRE library in /usr/include/pcre/ ... not found

checking for PCRE library in /usr/pkg/ ... not found

checking for PCRE library in /opt/local/ ... not found

说明:

PCRE库是实现Perl式正则表达式的基础。如果系统中缺少此库需要编译安装。可以从著名的开源软件网站sourceforge上下载:http://sourceforge.net/projects/pcre/files/pcre/。目前最新版本是8.20。

解决方案:

(1) 上传pcre-8.34.tar.gz到/user/local (2) #cd /user/local (3)# tar -xf pcre-8.34.tar.gz  (4)# cd pcre-8.34 (5)./configure  (6) make  (7) sudo make install 

**************

在.configure:

---error: You need a C++ compiler for C++ support 

yum install -y gcc gcc-c++

---checking for OpenSSL library ... not found

RedHat Fedora 平台:yum -y install openssl-devel

Debian ,ubunu 平台:apt-get install libssl-dev

*************

 

若出现:undefined reference to `pcre_free_study',注意下面红字,要定义正确的路径

./configure --prefix=/usr/local --with-http_realip_module  --with-pcre=../pcre-8.34 --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2

 

 

 *********************************************************************

--with***                安装一些内置/集成的模块

--with-http_realip_module  取用户真实ip模块

-with-pcre               Perl兼容的达式模块

--with-luajit              集成luajit模块

--add-module            添加自定义的第三方模块,如此次的ngx_che_purge

 

8.到/usr/local目录下 z

# cd /usr/local/  

# ll  

若发现多出来了如下目录,说明安装成功

/usr/local/luajit  :luajit环境,luajit类似于java的jit,即即时编译,lua是一种解释语言,通过luajit可以即时编译lua代码到机器代码,得到很好的性能;

/usr/local/lualib :要使用的lua库,里边提供了一些默认的lua库,如redis,json库等,也可以把一些自己开发的或第三方的放在这;

/usr/local/nginx :安装的nginx;

 

通过/usr/local/nginx/sbin/nginx  -V 查看nginx版本和安装的模块

执行以上命令报错:/usr/servers/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决方案:ln -s /usr/local/lib/libpcre.so.1 /lib64

 

9.启动nginx

/usr/local/nginx/sbin/nginx

 

 

 

4.配置nginx+lua开发环境

配置及Nginx HttpLuaModule文档在可以查看http://wiki.nginx.org/HttpLuaModule。

 

1.编辑nginx.conf配置文件

# vi /usr/local/nginx/conf/nginx.conf 

 

2.在http部分添加如下配置:引入Lua库信息

#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/local/nginx下找  

lua_package_path   "/usr/local/lualib/?.lua;;";  #lua 模块  

 lua_package_cpath "/usr/local/lualib/?.so;;";  #c模块   

 

3.为了方便开发我们在/usr/local/nginx/conf目录下创建一个lua.conf 

# vi /usr/local/nginx/conf/lua.conf

# lua.conf  

server {  

    listen       80;  

    server_name  _;  

}  

 

4.在nginx.conf中的http部分添加include lua.conf包含此文件片段 

    include lua.conf;  

 

5.测试是否正常 

# /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

 

HelloWorld

(1)在lua.conf中server部分添加如下配置 

    location /lua {  

    default_type 'text/html';  

    content_by_lua 'ngx.say("hello world")';  

    }  

 

测试配置是否正确 

/usr/servers/nginx/sbin/nginx  -t  

 

(2) 重启nginx 

/usr/servers/nginx/sbin/nginx  -s reload  

 

(3) 访问如http://192.168.1.108/lua(自己的机器根据实际情况换ip),可以看到如下内容 

hello world

 

 

5.lua代码文件

(1) 问题描述:将lua代码放在nginx-lua配置中,当lua的代码量变多时,配置文件难以维护,因此我们应该把lua代码移到外部文件中存储。 

# mkdir -p /usr/local/nginx/conf/lua/;

# vi /usr/local/nginx/conf/lua/test.lua 

#添加如下内容  

ngx.say("hello world");   

 

(2)修改lua.conf为

location /lua {  

default_type 'text/html';  

content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录  

}   

此处conf/lua/test.lua也可以使用绝对路径/usr/servers/nginx/conf/lua/test.lua。

 

 

6.lua_code_cache 

默认情况下lua_code_cache  是开启的,即缓存lua代码,即每次lua代码变更必须reload nginx才生效,如果在开发阶段可以通过lua_code_cache  off;关闭缓存,这样调试时每次修改lua代码不需要reload nginx;但是正式环境一定记得开启缓存。 

 

location /lua {  

default_type 'text/html';  

lua_code_cache off;  

content_by_lua_file conf/lua/test.lua;  

}  

 

开启后reload nginx会看到如下报警

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/servers/nginx/conf/lua.conf:8

 

7.错误日志

 

如果运行过程中出现错误,请不要忘记查看错误日志。 

tail -f /usr/servers/nginx/logs/error.log  

到此我们的基本环境搭建完毕。

 

 

8.nginx+lua项目构建

问题描述:随着nginx lua开发文件会越来越多,我们应该把其项目化,已方便开发。

 

 8.1 项目目录结构如下所示:

# mkdir -p /usr/server/example

# cd /usr/server/example

# mkdir lua lualib

 

example

    example.conf     ---该项目的nginx 配置文件

    lua              ---我们自己的lua代码

      test.lua

    lualib            ---lua依赖库/第三方依赖

      *.lua

      *.so

 

8.2 将lualib放置到项目中

好处:以后部署的时候可以一起部署,防止有的服务器忘记复制依赖而造成缺少依赖的情况。

我们将项目放到到/usr/server/example/lualib目录下

# cd /usr/server/example

# cp -r /usr/local/lualib/* ./lualib

 

8.3 编辑Nginx配置文件

# vi /usr/local/nginx/conf/nginx.conf

内容如下:(此处我们最小化了配置文件)

    #user  nobody;  

    worker_processes  2;  

    error_log  logs/error.log;  

    events {  

        worker_connections  1024;  

    }  

    http {  

        include       mime.types;  

        default_type  text/html; 

        #lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/local/nginx下找  

        lua_package_path "/usr/example/lualib/?.lua;;";  #lua 模块  

        lua_package_cpath "/usr/example/lualib/?.so;;";  #c模块  

        include /usr/example/example.conf;  

    }  

通过绝对路径包含我们的lua依赖库和nginx项目配置文件。

 

8.4  vi /usr/server/example/example.conf

/usr/server/example/example.conf配置文件如下 

    server {  

        listen       80;  

        server_name  _;  

      

        location /lua {  

            default_type 'text/html';  

            lua_code_cache off;  

            content_by_lua_file /usr/server/example/lua/test.lua;  

        }  

    }  

 

lua文件我们使用绝对路径/usr/server/example/lua/test.lua。 

 

8.5配置lua文件

# vi /usr/server/example/lua/test.lua

内容:

ngx.say("hello world");

 

8.6 到此我们就可以把example扔svn上了。/usr/local/nginx/sbin/nginx

或者

/usr/local/nginx/sbin/nginx  -s reload 

 

http://192.168.1.106/lua

http://192.168.1.107/lua

http://192.168.1.108/lua

===================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自zjjndnr.iteye.com/blog/2386270