Setting local Ruby on Rails development environment with Lighttpd and FastCGI

Tired of typing ./script/server? Yeah, me too. Especially with Lighttpd / FastCGI configured for PHP at hand on my development machine - pure envy. So I gave a try with my Ruby on Rails applications.

Easy as pie - first add a custom domain to your /etc/hosts:

127.0.0.1	myapp.local

Then - add FastCGI handler to your lighttpd.conf

$HTTP["host"] =~ "myapp.local" {
	server.indexfiles          = ( "dispatch.fcgi" )
	server.document-root       = "/path/to/myapp/public/"
	server.error-handler-404   = "/dispatch.fcgi"
	fastcgi.server = (
		".fcgi" =>
			( "localhost" =>
				(
					"socket" => "/tmp/myapp.socket",
					"bin-path" => "/path/to/myapp/public/dispatch.fcgi",
					"bin-environment" => ( "RAILS_ENV" => "development" )
				)
			)
	)
}

Restart your Lighttpd and your done. Unless…

Unless you require some custom libs in your Ruby on Rails application. Then you should modify your code (I keep all my require statements in myapp/config/initializers/requires.rb) from:

require "lib/mylib.rb"

to:

require Rails.path.join("mylib.rb")

No more those nasty 500’s! This setup will also work for subdomains, ie. thisis.myapp.local (as long you as handle them /etc/hosts or anywhere else).

Viewing RI in a web browser with lighttpd

In his post Matthias Georgi showed how to view RI in a web browser. He’s using Apache, so I rolled out a couple of snippets for lighttpd users.

First I added a host to my /etc/hosts:

127.0.0.1     ri

And created a virtual host in my lighttpd.conf

$HTTP["host"] =~ "ri" {
	server.document-root = "/my/projects/path/ri/"
	cgi.assign = ( ".rb" => "/usr/bin/ruby" )
	url.rewrite-once = (
		"^(.*)$" => "ri.rb?$0"
	)
}

Since lighttpd has a diffrent way of handling mod_cgi request parameters Matthias’es script needed a little adjusting:

#!/usr/bin/env ruby

require 'rdoc/ri/ri_driver'
require 'rubygems'

print "Content-type: text/html\r\n\r\n"

ARGV << ENV["QUERY_STRING"].sub("/", "") << '-f' << 'html' <<

ri = RiDriver.new

print '<html><body style="width:600px; margin:auto; padding:20px"><pre>'
ri.process_args
print '</body></html>'

Restart your lighttpd, browse to http://ri/String.capitalize. That’s it!