FastCGI For Neko On Share Hosting

In my previous post, I described you could setup neko web services on a shared host, using CGI. This method is not as efficient as it might be because a separate process is required for each request. However it is possible to extend this to “Fast CGI” (FGCI), which starts a single process, and keeps it alive. Apache talks to this over a socket, sending requests and receiving data and a very efficient manner.

If you got CGI working, and your server supports FCGI, then the transition on pretty simple.

The first thing to do is to download the new “fastcgi” haxelib. From a shell use:

haxelib install fastcgi

If haxelib asks you for a project directory, the following discussion assumes you specify your “haxeneko/lib” directory.
There is one bit of housekeeping you should do at this time – copy the “nekoapi.dso” object from “~/haxeneko/lib/fastcgi/0,1/ndll/Linux” into your “~/haxeneko” directory. This ensures that this dso will be found when neko is run by the web server.

Now it is time to change the cgi script. The code is very similar, except the extension should be “.fcgi”. Here is the script I used Site.fcgi:

export HAXENEKO=~/haxeneko
cd ../../site
exec neko SiteFCGI.n

Note the final “exec” call to ensure the pipes are all correctly plumbed.
And the obvious change to the .htaccess file (.fcgi extension):

RewriteEngine on
RewriteRule \\.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^(.*)?$ cgi-bin/Site.fcgi [L]

Finally, compile the “Test.hx” file that came with the fastcgi lib. I have a slightly altered version here:

class Test
   static var processed = 0;
   static public function main()
      // Called in single threaded mode...
      // This can be called multi-threaded...
      var req = new fastcgi.Request();
      while( req.nextRequest() )
         req.write( "Content-type: text/html\r\n" +
            "\r\n" +
            "<title>Neko FastCGI<title/>" +
            "<h1>Fast CGI</h1> Requests processed here: " + (processed++) );
         req.write( "\n page = " + req.getParam("REQUEST\_URI") );

This version prints the request uri too. To compile, use:

haxe -main Test -neko SiteFCGI.n -lib fastcgi

And that should be that! When you visit your web page, you should see the “processed” counter increase, verifying that it is the same process that is running.

Currently the system does not support easily killing the FCGI process, which is something that you must do when you update the “.n” neko file. The only way at the moment is to use the shell to do “ps -x” to identify the process number, and then “kill -9 number”, where number is the process number of the neko executable.

This entry was posted in linux, neko and tagged , , , , , . Bookmark the permalink.

5 Responses to FastCGI For Neko On Share Hosting

  1. tonypee says:

    Thanks Hugh. I was thinking about neko hosting for using flash sockets. I guess the fast cgi one would be the best, and sockets should work out of the box!
    We use site5 too, which makes these even more relevant ;P

  2. Hi Hugh,

    I tried to get this to work on a shared server (Dreamhost) and it seems like we can’t run a “sh” whilst it’s running as fcgi. I was wondering if there was an alternative to launch this (perl?)


  3. This is the error I get from the error.log from my server –> Premature end of script headers: Site.fcgi, referer:

  4. Huge says:

    Hi Simon,
    Have you “chmod a+x” the file, and started with “#!/bin/sh” ? It is also possible that you are only allowed jailshell ?
    Running fcgi requires a protocol to be followed, you can’t simply “print” stuff to the output, and any debug may get in the way.
    One possible way to start neko, besides a shell script, may be a small c-program. This would be pretty simple, and I can have a go is all else fails.
    Also, the error talks about “script errors”, which may suggest that it not treating it as “fcgi”, maybe “cgi” or some other protocol, which would be a .htaccess issue.
    If you still have problems, I can try to compile a c++ program that has no dependencies.


  5. Interesting:

    .htaccess file contents:
    SetHandler fastcgi-script

    RewriteEngine on
    RewriteRule \.(css|jpe?g|gif|png)$ – [L]
    RewriteRule ^(.*)?$ Site.fcgi [L]

    export HAXENEKO=~/haxeneko
    cd ../haxe-site
    exec neko Site.n

    Dreamhost currently has the default settings for FastCGI:
    FastCgiConfig -autoUpdate -initial-env RAILS_ENV=production -idle-timeout 60 -startDelay 10 -maxClassProcesses 2 -killInterval 300

    Interesting though is that nothing comes up if I type ps -x, so it seems like the neko isn’t started yet.

Leave a Reply

Your email address will not be published. Required fields are marked *