注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

itoedr的it学苑

记录从IT文盲学到专家的历程

 
 
 

日志

 
 

一个lua+nginx的实施参考方案  

2014-10-06 09:15:42|  分类: nginx编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
看看ntopng的lua-site方案,开始拆腾lua-web套件.一份关于lua-web支持方案:
https://github.com/unbit/uwsgi-docs/blob/master/Lua.rst#using-luawsapi-with-uwsgi

  1. Install nginx

     apt-get install nginx

    We’ll have to do some modifications later on to add the FastCGI handler. For simplicity we will keep the web path to “/var/www/nginx-default” and listen on port 8081 in case you have another webserver running on port 80.

  2. Install lua 5.1 (and WSAPI libraries)

    apt-get install lua5.1 liblua5.1-wsapi-fcgi-0 liblua5.1-coxpcall0 liblua5.1-filesystem0

    apt-get install liblua5.1-wsapi-doc

    Can’t do much testing without this. Note: The second line is not necessary if you are running Debian testing, and get the liblua5.1-wsapi-fcgi-1instead.

    EDIT: You’ll notice that I added in liblua5.1-filesystem0. Steve pointed out that there is a bug in liblua5.1-wsapi-fcgi-0. It doesn’t include it as a dependency. He reported this as a bug here, and it was fixed in liblua5.1-wsapi-fcgi-1.

  3.  Install spawn-fcgi

    If you’re running Debian testing you may be able to get spawn-fcgi through the distribution, however, I just downloaded it and compiled from source.

    wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
    tar -xzvf spawn-fcgi-1.6.3.tar.gz
    cd spawn-fcgi-1.6.3.tar.gz
    ./configure
    make
    make install

  4.  Create a FastCGI Socket

    spawn-fcgi -F 4 -u www-data -s /var/run/nginx-fcgi.sock -P /var/run/nginx-fcgi.pid — /usr/bin/wsapi.fcgi

    For the sake of simplicity, we will just spawn it manually for now. If you’re feeling crafty you can add the above line to the start condition in/etc/init.d/nginx, and the line below to the stop condition. You can add both of them to restart.

    cat /var/run/nginx-fcgi.pid | xargs -n 1 kill

  5.  Create a lua file in /var/www/nginx-default/

     In this tutorial, use hello.lua. You can change this to whatever, you want but just make sure you make the modification in the nginx configuration below as well.

  6.  Edit /etc/nginx/sites-available/default

    Now let’s add the code that will point nginx to the correct file. For simplicity, we will simply point it to hello.lua. You can change this to anything, or simply modify the code to accept any *.lua file, as seen in the tutorial listed above. Here is the top of my default file:

    listen   8081 default;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        fastcgi_pass    unix:/var/run/nginx-fcgi.sock;
        fastcgi_param   SCRIPT_FILENAME “/var/www/nginx-default/hello.lua”;
        fastcgi_param   PATH_INFO       $request_uri;
        fastcgi_param   QUERY_STRING    $query_string;
        fastcgi_param   REQUEST_METHOD  $request_method;
        fastcgi_param   CONTENT_TYPE    $content_type;
        fastcgi_param   CONTENT_LENGTH  $content_length
    ;
    }

  7. Restart nginx

    /etc/init.d/nginx restart

  8. Visit http://localhost:8081/

    Congratulations! You should now see hello.lua

不过还有更好的方案:uwsgi/wsapi+nginx/apache2.4+

***********************
WSAPI直接提供了大量的Lua库
***********************

WSAPI提供了大量的使用Lua库,这使得我们更容易在网络编程。一些支持的功能在Lua包括,

  • Request processing

  • Output buffering

  • Authentication

  • File uploads

  • Request isolation

  • Multiplexing

WSAPI一个简单的例子如下所示。

#!/usr/bin/env wsapi.cgi

module(..., package.seeall)
function run(wsapi_env)
   local headers = { ["Content-type"] = "text/html" }
   
   local function hello_text()
      coroutine.yield("<html><body>")
      coroutine.yield("<p>Hello Wsapi!</p>")
      coroutine.yield("<p>PATH_INFO: " .. wsapi_env.PATH_INFO .. "</p>")
      coroutine.yield("<p>SCRIPT_NAME: " .. wsapi_env.SCRIPT_NAME .. "</p>")
      coroutine.yield("</body></html>")
   end

   return 200, headers, coroutine.wrap(hello_text)
end
  评论这张
 
阅读(195)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017