lua + nginx + FastCGI in Debian

I've recently been doing some testing in lua, and have been comparing the results to the EdgeLink Consulting CMS that we've designed in PHP. So far this solution is able to serve substantially more requests per second than our current CMS. However, we haven't really spent much time optimizing the CMS. The goal is to have a working copy first before any optimizations are done. We've also been working on some eCommerce modules for the platform.

With all that being said, I'd like to post a quick tutorial on how I got this setup. It was quite the task. Although there was a tutorial I found to do the same task, it was a little bit confusing. My tutorial will have a lot of the same steps, with some minor adjustments. This tutorial is written at an intermediate level. Some trivial steps have been omitted.

NOTE: This has been tested with Debian 5.0.4 (Stable)

  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.
     

If you have any problems, post in the comments. Stay tuned for more related posts.

Comments

Jake Billo on 2010-06-27 01:49 PM (#)
What a hack. Hopefully this is insightful enough to get past Akismet.
Dave Lahn on 2010-06-27 01:49 PM (#)
This is a test comment.
Gustav on 2010-06-28 09:10 PM (#)
Sounds interesting? You say this Lua solution is faster. How much faster? Do you have any benchmarks? Just would like to know if it's worth investing my time with it. If it's 10% faster, then what's the point? If it's 500% faster, well that's a different ball game then.
Steven Chamberlain on 2010-07-24 05:12 PM (#)
Re: benchmarks... I'm not sure about the nginx/FastCGI setup detailed above, but the Lua language itself outperforms PHP in the typical benchmarks: http://shootout.alioth.debian.org/u32/benchmark.php?test=all〈=lua&lang2=php Lua is particularly good for its low memory consumption, and perhaps it's a good thing that fewer bytes of cleanly-formatted Lua code seem to be able to perform the same functionality as an equivalent program in most other languages.
Teddy on 2010-08-22 12:28 AM (#)
@Dave Can you update this post to use the super fast LuaJIT. Please :)
Steven Chamberlain on 2010-09-12 04:52 PM (#)
Using ApacheBench on a test setup with 4 concurrent nginx worker processes and 4 concurrent wsapi.fcgi processes: standard lua interpreter handled 1869.43 requests/sec using 4632 KiB RAM; luajit-2.0.0-beta5 handled 2044.13 requests/sec using 4484 KiB RAM. It seems there's not much of a speed boost here, because wsapi.fcgi stores the environment between each execution anyway. In a larger setup, or if using something else instead of wsapi.fcgi, I imagine luajit could be leveraged to greater effect.
Steven Chamberlain on 2010-10-19 10:10 PM (#)
Some quick benchmarking using the fastest pidigits programs from the Computer Language Benchmarks Game, both languages running under nginx webserver via FastCGI to return a 'text/plain' document containing the result (100 digits of pi) in response to the HTTP queries from ApacheBench. PHP, even with APC accelerator module, answered 7.3 reqs/sec using ~6536 KiB resident memory. Lua (via wsapi.fcgi, without LuaJIT) answered 100.11 reqs/sec using ~620 KiB resident memory. Simultaneously around 10x less memory and 10x more speed. This is just one, random benchmark but I still find that encouraging.
zwdia on 2011-12-09 09:56 PM (#)
You may want to change apt-get install nginx to nginx-full to be up to date

Add Comment

If you'd like, you can add your own comments to this post. The site owner may choose to moderate comments, so your comments may not appear immediately.



(Your email address will only be stored and not posted publicly.)

Verification