Ø   简介

Open_resty是由nginx核心+第三方模块组成,它集成了lua开发环境,让nginx从一个代理服务器变成一个web服务器,nginx是基于事件驱动的模型并且是异步非阻塞(AIO)模型,所以可以应付高并发量的请求数据,并且它还提供了许多诸如mysql、memchaed和redis等组件,让nginx开发web应用更加easy。

Ø   下载openresty-1.11.2.1.tar.gz

上传至主目录下(提前建立一个software文件夹来存放文件);

#cd /home/aspire/sorfware
#tar –zxvf openresty-1.11.2.1.tar.gz

 

 

Ø   安装依赖

Openresty的依赖包有:

libreadline-dev + libncurses5-dev + libpcre3-dev+ libssl-dev + perl

注意版本的兼容性:我用的是suse linux10.1,公司的破电脑试了几百遍终于找对版本。

①  readline-6.3.tar.gz
②  perl-5.24.0.tar.gz
③  pcre-8.39.tar.gz
④  openssl-0.9.8zg.tar.gz
⑤  ncurses-5.6.tar.gz

 

安装基本都是三部曲:

#./configure && make &&make install
注意:#ldconfig
#chmod -v 755 /lib/lib{readline,history}.so*(readline安装会出现一些权限问题,用此解决)

以上都安装好以后进行下一步操作:

Ø  安装openresty-1.11.2.1

#cd openresty-1.11.2.1
#./configure--prefix=/usr/local/openresty-1.11.2.1/ --with-luajit  --with-http_ssl_module  --with-http_iconv_module --with-openssl=/home/aspire/sorfware/openssl-0.9.8zg--with-pcre=/home/aspire/sorfware/pcre-8.39
# make && make install

 

如果想查看nginx都安装了哪些模块使用以下命令:

# /usr/local/openresty-1.11.2.1/nginx/sbin/nginx–V

或者进入

/usr/local/openresty-1.11.2.5/bin

执行:

./openresty -V

openresty升级对应的nginx openresty/1.11.2.5_openresty升级对应的nginx

看下bin目录结构:

openresty升级对应的nginx openresty/1.11.2.5_perl_02

最核心的可执行文件 openresty,它其实是 nginx.的一个软链接。至于目录里面其他的一些工具,没有任何悬念,它们都是perl脚本。

  1. opm:包管理工具,可以通过它来管理各种第三方包
  2. restydoc:是openresty提供的文档查看工具,可以 通过它来查看nginx和openresty的使用文档,这里执行比如查看ngx.say的文档: ./restydoc -s ngx.say

根目录:

openresty升级对应的nginx openresty/1.11.2.5_openresty升级对应的nginx_03

  • prod:其实根目录下的prod文件夹下存放的其实是OpenResty、 NGINX、lua-resty-*、LuaJIT的文档,这样跟上边的restydoc就李恩熙在一起了。
  • nginx和luajit文件夹主要存放nginx和luaJit的可执行文件和依赖。是openresty的基石,因为openresty是基于luajit的。
  • lualib主要存放是openresty使用到的lua库,主要分为resty和ngx两个子目录。

Openresty安装成功后,启动ngin

# /usr/local/openresty-1.11.2.1/nginx/sbin/nginx

在浏览器访问linux对应ip,如果出现欢迎界面表示nginx运行正常:

openresty升级对应的nginx openresty/1.11.2.5_openresty升级对应的nginx_04

 

 

Ø  配置lua开发环境

1 编译nging.conf文件:

# cd /usr/local/openresty-1.11.2.1/nginx/conf/nginx.conf

2 在http块内添加以下配置:

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

lua_package_path "/usr/local/openresty-1.11.2.1/lualib/?.lua;;";  #lua 模块 
lua_package_cpath "/usr/local/openresty-1.11.2.1/lualib/?.so;;";  #c模块

 

Ø   创建并管理lua工程

# cd /home/aspire/goodscenter/
创建lua开发空间文件夹:
# make nginx_lua_dev

进入开发空间并建立我们的第一个lua工程example

# cd nginx_lua_dev
# mkdir example

在example下建立example.conf并在同级别目录下创建lua文件夹

# cd lua

创建example.lua文件。

example.conf和example.lua创建完毕,我们先往example.conf里边添加内容:

server{ 
    listen      80; 
    server_name _; 
     location /example { 
        default_type 'text/html'; 
lua_code_cache off; 
       # content_by_lua 'ngx.say("hellolua!!!")'; 
#引入我们刚才在lua文件下创建的example.lua(还是空白),如果以下文件路径使用相对路径则是现对于nginx的安装目录。
/home/aspire/goodscenter/nginx_lua_dev/example/lua/example.lua; 
  } 
}

进入lua下编辑example.lua:

ngx.say("hello lua!!!");

保存退出!

 

Lua工程到此创建完毕,但还差最后一步,要在nginx中的http块内引用lua配置文件的路径:   

include /home/aspire/goodscenter/nginx_lua_dev/example/example.conf;

重启nginx:

../sbin/nginx -s reload

在浏览器输入:http://ip:port/example

openresty升级对应的nginx openresty/1.11.2.5_perl_05

完毕。