Project

General

Profile

Actions

Ruby on Rails with Lighttpd

Starting the rails application

In lighttpd 1.4 you have the option to spawn FastCGI backends from lighttpd with the "bin-path" option; but it is recommend to use spawn-fcgi for that.

Use something like this with daemontools to supervise your rails backends: (./run script, you need a public/dispatch.fcgi for this; most rails applications provide it or an example which you can use)

#!/bin/sh
# needs spawn-fcgi version >= 1.6

exec 2>&1

export RAILS_ENV=production
export LANG=en_US.UTF-8

# www-data is the user lighty runs as
exec /usr/bin/spawn-fcgi -s /var/run/lighttpd/rails-application.sock -n -U www-data -u rails-app-user -- /path/to/rails-application/public/dispatch.fcgi

Lighttpd 1.4

Get cleanurl.lua ; the file /path/to/rails-application/public/dispatch.fcgi must exist even if you use something else for spawning (or you may need "check-local" => "disable").

$HTTP["host"] == "rails-app.example.org" {
    server.document-root = "/path/to/rails-application/public/" 
    magnet.attract-physical-path-to = ( "/etc/lighttpd/cleanurl.lua" )

    fastcgi.server    = ( "dispatch.fcgi" =>
        ((
            "socket" => "/var/run/lighttpd/rails-application.sock",
# if you don't use spawn-fcgi you may try this:
#            "bin-path" => "/path/to/rails-application/public/dispatch.fcgi",
#            "max-procs" => 1,
#            "bin-environment" => (
#                "RAILS_ENV" => "production",
#                "LANG" => "en_US.UTF-8",
#            ),
#            "bin-copy-environment" => (
#                "PATH",
#            ),
        ))
    )
}

Running rails application in a subdirectory

Warning: Not every rails application supports this.

Lets assume you want to have your blog in http://rails-app.example.org/blog, try this:

$HTTP["host"] == "rails-app.example.org" {
    $HTTP["url"] =~ "^/blog" {
        alias.url = (
            "/blog" => "/path/to/rails-application/public/",
        )
        server.document-root = "/path/to/rails-application/public/" # needed so cleanurl.lua finds the right dispatch.fcgi

        magnet.attract-physical-path-to = ( "/etc/lighttpd/cleanurl.lua" )

        fastcgi.server    = ( "dispatch.fcgi" =>
            ((
                "socket" => "/var/run/lighttpd/rails-application.sock",
                "strip-request-uri" => "/blog",
            ))
        )
    }
}

And add this to your rails application config, for example in config/environments/production.rb

config.action_controller.relative_url_root = "/blog" 

Using RubyOnRails in a simple-vhost environment

Let's say: For an easy setup you want to use simple-vhost but still want to separate the rails-installation:

$HTTP["host"] == "ruby.example.org" {
    server.document-root = "/var/www/site/public/" 
    fastcgi.server = ...
}
else {
    simple-vhost.server-root = "/var/www/servers/" 
    simple-vhost.default-host = "www.example.org" 
    simple-vhost.document-root = "pages" 
}

Updated by gstrauss about 3 years ago · 17 revisions