** UPDATE **
I’ve also posted an entry on how to use Apache2 proxy to handle multiple Ruby on Rail applications on the same domain url.
** END UPDATE **
One of the stranger things about Ruby on Rails is that after you have installed the basic software foundation to be able to run Ruby on Rail framework using lighttpd server with fast-cgi, the first application that you install via rails would be accessible only at the root url.
For example, if you had ran a ‘rails app1′, the url to access app1 would not be
http://website.com/app1/ , but rather http://website.com/
If you had set up another rails application called ‘app2′, you would have to stop lighttpd and change the lighttpd.conf to point to app2 before you could restart the lighttpd server and access app2 at the root url.
Many of the solutions I found on web to set up multiple ruby on rails applications also requires you to come up with a different domain for each application. i.e. http://app1.website.com and http://app2.website.com. I didn’t really want to adopt this approach.
I would have much prefered to keep it all under a single domain (i.e. http://website.com/app1/ and http://website.com/app2/). It would make good sense if one could set up the appropiate configuration to avoid all this restarting and reconfiguring for multiple rails applications under the same domain url.
To my surprise, the online documentation and google search weren’t able to provide me with sufficient details to do this painlessly. All the routing errors, 404s, 403s, 501s, and whatnots that I ecountered during countless configuration changes almost made me pull out my hair. For a such simple and vital configuration, it sure wasn’t easy to figure out every last details that were necessary to get it all clicking together.
But, fortunately, after many false starts and dead-ends, I was able to figure all this out and here is what you need to do to be able to have multiple Ruby on Rails applications running under the same domain url (i.e. http://website.com/app1/ and http://website.com/app2 )
We need to set up lighttpd so that it is able to detect when app1 or app2 is on the url and pass the request off to the correct app fast cgi. You don’t want to strip-request-uri in this configuration since this configuration change needs the full url to function correctly. Later, we will be adding a route change to ensure that the url path is correct for the entire hierachy of each app.
in your lighttpd.conf (/usr/local/etc/lighttpd.conf for FreeBSD systems), make these changes:
server.modules = ( "mod_rewrite", "mod_alias", "mod_access", "mod_fastcgi", "mod_accesslog" )server.errorlog = "/var/log/lighttpd.error.log" # don't forget to chown www:www this fileindex-file.names = ( "dispatch.fcgi", "index.php", "index.html", "index.htm", "default.htm" )accesslog.filename = "/var/log/lighttpd.access.log" # don't forget to chown www:www this file$HTTP["url"] =~ "^/app1/" { server.document-root = "/usr/local/www/lighttpd/data/app1/public/" alias.url = ( "/app1/" => "/usr/local/www/lighttpd/data/app1/public/" ) server.error-handler-404 = "/dispatch.fcgi" server.indexfiles = ( "dispatch.fcgi", "index.html" ) fastcgi.server = ( ".fcgi" => ( "localhost" => ( "min-proc" => 1, "max-proc" => 1, "socket" => "/tmp/app1-fastcgi.socket", "bin-path" => "/usr/local/www/lighttpd/data/app1/public/dispatch.fcgi", "bin-environment" => ( "RAILS_ENV" => "development" )# "strip-request-uri" => "/app1/" ) ) )}
$HTTP["url"] =~ "^/app2/" { server.document-root = "/usr/local/www/lighttpd/data/app2/public/" alias.url = ( "/app2/" => "/usr/local/www/lighttpd/data/app2/public/" ) server.error-handler-404 = "/dispatch.fcgi" server.indexfiles = ( "dispatch.fcgi", "index.html" ) fastcgi.server = ( ".fcgi" => ( "localhost" => ( "min-proc" => 1, "max-proc" => 1, "socket" => "/tmp/app2-fastcgi.socket", "bin-path" => "/usr/local/www/lighttpd/data/app2/public/dispatch.fcgi", "bin-environment" => ( "RAILS_ENV" => "development" )# "strip-request-uri" => "/app2/" ) ) )}
Now we need to ensure that all the urls spit out by app1 and app2 controllers will include ‘app1′ or ‘app2′ at the beginning of the root url:
in app1’s routes.rb:
add this line:ActionController::AbstractRequest.relative_url_root = "/app1"above the line:map.connect ':controller/:action/:id'
in app2's routes.rb:add this line:ActionController::AbstractRequest.relative_url_root = "/app2"above the line:map.connect ':controller/:action/:id'
Finally, stop and restart lighttpd at this point and you should be able to access both apps now!