Johan Sørensen

Setting up Lighttpd for local Rails development

In my previous lighttpd article I only briefly mentioned how to configure it once it was compiled and installed. But I don’t really think there’s much reason to go into detail with configuring lighttpd because it’s pretty straight-forward and the comments in the example `lighttpd.conf` explains the different options pretty well.

However, since Lighttpd is a lean mean fast-cgi serving machine I’ve starting using it locally for developing Rails applications. It simply feels slighty faster than Apache2 with fast-cgi and since Rails reloads all it’s lovely goodies when you’re running in development mode, I see no reason to spend time waiting for webrick or “normal cgi” whenever I reload the page.

The “system” I’m currently using for local development is that each Rails application get its own lighttpd.conf located in `$RAILS_ROOT/config`. Obviously this limits me when it comes to working on more than one Rails application at exactly the same time, but I don’t multitask that good anyway and since I have a certain amount of terminals open anyway, using the tabbed ones in iTerm in my case, whenever I’m working on a particular app (tailing logfiles, irb and such), so why not open another one for lighttpd while I’m there.

Anyway, to the Config Mobile!

My lighttpd.conf for each project looks like this (located in $RAILS_ROOT/config/lighttpd.conf):

UPDATE: Apparently there’s a bug in Apples kqeue, which causes system freezes on dual machines, I’ve experienced this myself. The fix is to use “select” as the event-handler.


server.port = 8080
server.bind = "127.0.0.1"
server.event-handler = "freebsd-kqueue" # OR "select" if you're on a dual cpu mac
server.modules = ( "mod_rewrite", "mod_fastcgi" )

server.document-root = "/Users/johan/railsapp/public"
server.errorlog      = "/Users/johan/railsapp/log/server.log"

server.error-handler-404 = "/dispatch.fcgi"

fastcgi.server = ( ".fcgi" =>
  ( "localhost" =>
      (
        "min-procs" => 1, 
        "max-procs" => 5,
        "socket"   => "/tmp/railsapp.fcgi.socket",
        "bin-path" => "/Users/johan/railsapp/public/dispatch.fcgi",
        "bin-environment" => ( "RAILS_ENV" => "development" )
      )
  )
)
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".css"          =>      "text/css",
  [...and many more...]
  )

That’s the bare minimum needed to get a fully functional Rails application development environment up and running that gets served by lighttpd, the above will most likely only work with Rails from 0.10.x and up since the error-handler stuff is what makes Routes happy.

I didn’t paste all of the mimetypes in there, you can grab those from the lighttpd.conf that ships with the lighttpd dist. Fact is, it works without defining the mimetypes for all my browsers except Camino, but I guess it’s always a good thing to make sure that files gets served the right way.

I then start lighttpd with either `lighttpd -D -f config/lighttpd.conf` or `lighttpd -f config/lighttpd.conf` depending on my mood (the -D switch makes it not go into the background; makes it easier to terminate without killall/PID hunting).

The README supplied with Rails also contains an example config for lighttpd which is pretty much the same as this one

That’s how I’m doing it right now, I guess there are better ways such as using the vhost facilities of lighttpd. But honestly I just can’t be bothered for local development on my box where i’m the only one accessing the webserver(s). I like to keep things simple and seperated.


Comments:

  1. Chris Says:


    This may be obvious to others, but since I had been using a plain Apache setup to date, I had not installed the FCGI Ruby gem, which you’ll need for this. Therefore, I’d suggest mentioning, or people doing sudo gem install fcgi if you haven’t already. After this, my Rails setup on lighttpd worked fine.

  2. Chris Says:


    One other note. If you’re running lighttpd as non-root user (such as for a local Rails setup like above), lighttpd is probably not in your path. It is in /usr/local/sbin.

  3. Jan Kneschke Says:


    to simplify the pidfile hunting, set server.pid-file = “/var/run/lighttpd.pid”

    $ kill `cat /var/run/lighttpd.pid`

    will take it down afterwards. This is what the start and stop scripts are doing for you otherwise.

  4. Kevin Says:


    Is anyone running lighttpd under OSX on dual CPU machines? I’m having real stability problems which I’ve detailed here.

  5. johan Says:


    Kevin: Haven’t noticed anything (I have a dual), but I only run it on osx for local development purporses, which means hardly no load..

  6. cboone Says:


    Great idea Jan, but it didn’t work for me.

    Instead, I use

    sudo kill `cat /var/run/lighttpd.pid`

    The sudo is needed because I run lighttpd under it (— is that unnecessary? —); the backticks dictate that cat be run first, and that its output be fed to kill as an argument.

    Then in my .profile, I defined the following alias:

    alias light_stop='sudo kill `cat /var/run/lighttpd.pid`'

    and now I just type light_stop and lighttpd is dead.

    Thanks for the excellent tutorial Johan…

  7. cboone Says:


    Argh. Maybe Jan and I said the same thing…

    The backticks are being filtered out of the comments. There should be a backtick before ‘cat’ and after ‘pid’…