LighttpdOnRails » History » Revision 13
Revision 12 (stbuehler, 2009-04-16 10:40) → Revision 13/17 (gross, 2011-08-04 14:47)
h1. Ruby on Rails with Lighttpd h2. 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:WikiStart|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) <pre> #!/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 </pre> h2. Lighttpd 1.4 Get "cleanurl.lua":http://nordisch.org/cleanurl.lua "cleanurl.lua":http://nordisch.org./cleanurl.lua from http://nordisch.org/; http://nordisch.org./; 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"). *NB*: I had a problem with it, and get error 403 in lighty response. Find this strings in _cleanurl.lua_ <pre> lighty.env["physical.rel-path"] = "/dispatch.fcgi" lighty.env["uri.path"] = lighty.env["physical.rel-path"] </pre> and *swap* them. Or you will ever get 403, because original request string will be overwritten by modified version. <pre> $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", # ), )) ) } </pre> h3. 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: <pre> $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", )) ) } } </pre> And add this to your rails application config, for example in config/environments/production.rb <pre> config.action_controller.relative_url_root = "/blog" </pre> h2. Lighttpd 1.5 Get "cleanurl.lua":http://nordisch.org/cleanurl.lua "cleanurl.lua":http://nordisch.org./cleanurl.lua from http://nordisch.org/. See _nota bene_ above. http://nordisch.org./. <pre> $HTTP["host"] == "rails-app.example.org" { server.document-root = "/path/to/rails-application/public/" magnet.attract-physical-path-to = ( "/etc/lighttpd/cleanurl.lua" ) $HTTP["url"] == "/dispatch.fcgi" { proxy-core.backends = ( "unix:/var/run/lighttpd/rails-application.sock" ) proxy-core.protocol = "fastcgi" proxy-core.allow-x-sendfile = "enable" } } </pre> h2. Using RubyOnRails in a simple-vhost environment Let's say: For an easy setup you want to use [[lighttpd:Docs:ModSimpleVhost|simple-vhost]] but still want to separate the rails-installation: <pre> $HTTP["host"] == "ruby.example.org" { server.document-root = "/var/www/site/public/" fastcgi.server = ... } else $HTTP["host"] =~ "" { simple-vhost.server-root = "/var/www/servers/" simple-vhost.default-host = "www.example.org" simple-vhost.document-root = "pages" } </pre>