美文网首页iOS知识库
Lua系列文章 第一篇 安装OpenResty(Nginx+Lu

Lua系列文章 第一篇 安装OpenResty(Nginx+Lu

作者: meng_philip123 | 来源:发表于2018-02-26 17:11 被阅读96次

    Lua安装

    首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用。借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序。而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在大公司的核心功能上都在使用Nginx+Lua架构,比如淘宝、京东、去哪儿等等。

    安装环境

    安装步骤可以参考http://openresty.org/#Installation。

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

    2、安装依赖(我的环境是centos,可以使用如下命令安装,其他的可以参考openresty安装步骤)

      yum install pcre-devel openssl-devel gcc curl

    3、下载ngx_openresty-1.11.2.5.tar.gz并解压

      wget https://openresty.org/download/openresty-1.11.2.5.tar.gz

      tar xf openresty-1.11.2.5.tar.gz

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

    5、安装LuaJIT

      cd openresty-1.11.2.5/bundle/LuaJIT-2.1-20170808/ 

      make clean && make && make install 

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

    6、下载ngx_cache_purge模块,该模块用于清理nginx缓存

      cd /usr/local/src/openresty-1.11.2.5/bundle 

      github地址:https://github.com/FRiCKLE/ngx_cache_purge

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

      mv 2.3.tar.gz ngx_cache_purge.2.3.tar.gz

      tar xf ngx_cache_purge.2.3.tar.gz

    7、下载nginx_upstream_check_module模块,该模块用于ustream健康检查

      cd /usr/local/src/openresty-1.11.2.5/bundle 

      github地址:https://github.com/yaoweibin/nginx_upstream_check_module

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

      mv v0.3.0.tar.gz nginx_upstream_check_module.v0.3.0.tar.gz

      tar xf nginx_upstream_check_module.v0.3.0.tar.gz

    8、安装ngx_openresty

      先检测nginx是否安装 如果安装后

      请通过 nginx -V 检查已经安装过的模块等等

      nginx version: nginx/1.10.2

      built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)

      built with OpenSSL 1.0.1e-fips 11 Feb 2013

      TLS SNI support enabled

      configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre

      cd /usr/local/src/openresty-1.11.2.5

      进行编译安装

      ./configure --prefix=/usr/local --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-http_realip_module  --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 

      make && make install

    mkdir -p /usr/local/site/lualib /usr/local/site/pod /usr/local/site/manifest

    ln -sf /usr/local/nginx/sbin/nginx /usr/local/bin/openresty

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

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

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

      --with-luajit              集成luajit模块

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

    9、到/usr/local/src目录下

      cd /usr/local/src && ll 

    total 340

    drwxr-xr-x.  2 root  root    4096 Feb 26 07:57 bin

    -rw-r--r--  1 root  root  22924 Feb 26 07:57 COPYRIGHT

    drwxr-xr-x.  2 root  root    4096 Sep 23  2011 etc

    drwxr-xr-x.  2 root  root    4096 Sep 23  2011 games

    drwxr-xr-x  11 root  root    4096 May 19  2017 go

    drwxr-xr-x  6 root  root    4096 Mar  3  2017 graphviz

    drwxr-xr-x.  4 root  root    4096 Nov 28 02:57 include

    drwxr-xr-x.  5 root  root    4096 Feb 26 07:27 lib

    drwxr-xr-x.  4 root  root    4096 Nov 22 03:23 lib64

    drwxr-xr-x  6 root  root    4096 Oct 25 04:41 libdatrie

    drwxr-xr-x.  2 root  root    4096 Sep 23  2011 libexec

    drwxr-xr-x  7 root  root    4096 Nov 26  2016 libmcrypt

    drwxr-xr-x  6 root  root    4096 Feb 26 07:57 luajit

    drwxr-xr-x  6 root  root    4096 Feb 26 07:57 lualib

    drwxr-xr-x  4 root  root    4096 Nov 28 02:57 man

    drwxr-xr-x  4 root  root    4096 Nov 26  2016 mcrypt

    drwxr-xr-x  5 root  root    4096 Nov 26  2016 mhash

    drwxr-xr-x  13 mysql mysql  4096 Jan 11  2017 mysql

    drwxr-xr-x  11 root  root    4096 Oct 16 08:19 nginx

    drwxr-xr-x  8 root  root    4096 May  8  2017 openresty

    drwxr-xr-x  9 root  root    4096 Nov 26  2016 php

    drwxr-xr-x  43 root  root    4096 Feb 26 07:57 pod

    drwxr-xr-x  6 redis redis  4096 Nov 27  2016 redis

    -rw-r--r--  1 root  root  216208 Feb 26 07:57 resty.index

    drwxr-xr-x.  2 root  root    4096 Sep 23  2011 sbin

    drwxr-xr-x. 11 root  root    4096 Feb 26 07:27 share

    drwxr-xr-x  5 root  root    4096 Feb 26 07:57 site

    drwxr-xr-x. 14 root  root    4096 Feb 26 07:24 src

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

    /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版本和安装的模块

            nginx version: openresty/1.11.2.5

    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: --prefix=/usr/local/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-http_realip_module --add-module=/usr/local/src/openresty-1.11.2.5/bundle/ngx_cache_purge-2.3 --add-module=/usr/local/src/openresty-1.11.2.5/bundle/nginx_upstream_check_module-0.3.0

    10、启动nginx

      执行/usr/local/nginx/sbin/nginx 或者 service nginx restart

      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

      Stopping nginx:                                            [  OK  ]

      Starting nginx:                                            [  OK  ]

    11、接下来该配置nginx+lua开发环境了

        配置环境

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

      1)、编辑nginx.conf配置文件

          vim /usr/local/nginx/conf/nginx.conf 

      2)、在http部分添加如下配置

          #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 内容如下:

          #lua.conf 

          server { 

            listen      81; 

            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

      6)、编写HelloWorld实例

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

          location /lua { 

                default_type 'text/html'; 

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

          } 

          测试配置是否正确 /usr/local/nginx/sbin/nginx  -t 

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

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

      7)、lua代码文件

        我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储、方便管理和维护

        cd /usr/local/nginx/conf  && mkdir lua

        vim /usr/local/nginx/conf/lua/test.lua 

        #添加如下内容 

        ngx.say("hello world");

        然后lua.conf修改为 

        location /lua { 

        default_type 'text/html'; 

        content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录 此处conf/lua/test.lua也可以使用绝对路径/usr/local/nginx/conf/lua/test.lua。

        }

      8)、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; 

        } 

        开启后-t和-s reload nginx会看到如下报警

            [root@localhost conf]# nginx -t

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

    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

    [root@localhost conf]#

    [root@localhost conf]# nginx -s reload

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

    9)、nginx+lua项目构建

      以后我们的nginx lua开发文件会越来越多,我们应该把其项目化,已方便开发。项目目录结构如下所示:

      luaexample

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

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

            test.lua

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

            *.lua

            *.so

      其中我们把lualib也放到项目中的好处就是以后部署的时候可以一起部署,防止有的服务器忘记复制依赖而造成缺少依赖的情况。

      我们将项目放到到/usr/luaexample目录下。

      /usr/local/nginx/conf/nginx.conf配置文件如下(此处我们最小化了配置文件)

    user nginx nginx;

    worker_processes  auto;

    error_log  /home/wwwlogs/nginx_error.log  crit;

    pid        /usr/local/nginx/logs/nginx.pid;

    #Specifies the value for maximum file descriptors that can be opened by this process.

    worker_rlimit_nofile 51200;

    events {

        use epoll;

        worker_connections 51200;

        multi_accept on;

    }

    http {

        include      mime.types;

        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

          '$status $body_bytes_sent "$http_referer" '

          '"$http_user_agent" "$http_x_forwarded_for"';

        server_names_hash_bucket_size 128;

        client_header_buffer_size 32k;

        large_client_header_buffers 4 32k;

        client_max_body_size 50m;

        sendfile        on;

        tcp_nopush    on;

        keepalive_timeout  65;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;

        fastcgi_send_timeout 300;

        fastcgi_read_timeout 300;

        fastcgi_buffer_size 64k;

        fastcgi_buffers 4 64k;

        fastcgi_busy_buffers_size 128k;

        fastcgi_temp_file_write_size 256k;

        gzip  on;

        gzip_min_length  1k;

        gzip_buffers    4 16k;

        gzip_http_version 1.1;

        gzip_comp_level 2;

        gzip_types    text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

        gzip_vary on;

        gzip_proxied  expired no-cache no-store private auth;

        gzip_disable  "MSIE [1-6]\.";

        server_tokens off;

        access_log off;

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

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

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

        include /usr/luaexample/example.conf;

        server {

          listen      80;

          server_name  localhost;

          location / {

      root  html;

      index  index.html index.htm;

          }

          error_page  404              /404.html;

        }

      include vhost/*.conf;

    }

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

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

      server { 

            listen      81; 

            server_name  _; 

            location /lua { 

                default_type 'text/html'; 

                lua_code_cache off; 

                content_by_lua_file /usr/luaexample/lua/test.lua; 

            } 

        } 

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

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

    相关文章

      网友评论

        本文标题:Lua系列文章 第一篇 安装OpenResty(Nginx+Lu

        本文链接:https://www.haomeiwen.com/subject/eniyxftx.html