Topic: Deploying Multiple Rails Applications

Hi guys,

I've developed an a rails application that I need to now deploy on a dedicated server (FreeBSD). I'm a newbie in the deployment area so you'll excuse me if my question isn't the best. The application I've developed will be accessible to only a few users, with each user having his own application  almost similar to the way wordpress.com works i.e. each user will have a sub-domain with his own application like http://hackersapien.xapps.com what I'd like to know is how I do I setup multiple rails application on one server.

On my windows box, I've tried starting mongrel separately for each app but that doesn't work, just wondering how it would be possible for linux.

Any help or advice will be appreciated.

Cheers

Re: Deploying Multiple Rails Applications

If you can go for Nginx (web server), a tutorial for deploying multiple apps is http://docs.planetargon.com/Deployment (Goto Step3 .. Nginx)

Re: Deploying Multiple Rails Applications

nakula wrote:

If you can go for Nginx (web server), a tutorial for deploying multiple apps is http://docs.planetargon.com/Deployment (Goto Step3 .. Nginx)

I'm a bit skeptical of using Nginx due to the fact that there is very little documentation available online and the little that is available is in russian with a few in english. Any idea how I can do the same using lighttpd or Apache+Mongrel Cluster?

Re: Deploying Multiple Rails Applications

No idea. Sorry.

Re: Deploying Multiple Rails Applications

I have not tried this but I *think* you can use http://wiki.zhekov.net/System_Administration (http://wiki.zhekov.net/mongrelmultiapp) for your work. [conf file does not uses subdomains though sad]

Re: Deploying Multiple Rails Applications

I struggled with this myself, and then decided that

1) the hassle wasn't worth banging my head over
2) the overhead of multiple mongrels might not be a great idea

So I used 1 instance of rails/mongrel/cap/apache etc, and did subdomain routing in routes.rb.  Works like a champ for me, but, prolly depends on your functional requirements.

Re: Deploying Multiple Rails Applications

d2d wrote:

I struggled with this myself, and then decided that

1) the hassle wasn't worth banging my head over
2) the overhead of multiple mongrels might not be a great idea

So I used 1 instance of rails/mongrel/cap/apache etc, and did subdomain routing in routes.rb.  Works like a champ for me, but, prolly depends on your functional requirements.

On the part of subdomain routing, did you need to configure your own DNS server to handle this or did you pull it off just using the routes.rb?I'd like for users to be able to put in the name of their application e.g.d2d and that gives them a subdomain to my application i.e. http://d2d.xapps.com, I mentioned this to someone and they suggested that I'd have to setup my own DNS server to handle the subdomains, I'm really interested in knowing whether it can be done programatically using rails instead of going the whole DNS(BIND) way.

Re: Deploying Multiple Rails Applications

I just stumbled upon this interesting blog post about setting up a multi-site application and using the account_location plugin http://railstips.org/2006/11/13/buildin … pplication which really helps me understand the whole subdomain issue, but one the major issues I'm facing is that every user with a subdomain will have their own layout/template based upon the one they submit to  us to integrate into the application. In apps like basecamp www.basecamphq.com the template look is carried across and all the user can do is change certain aspects of their project space like background color of the header.

This brings me to my question, since each user will have a customised layout does that mean that with the creation of the subdomain in the sites/accounts table do I also create a new layout?

In order for me to ensure that each user sees what only belongs to their domain does that mean I have to create an association between the user and the id in the sites table?

Re: Deploying Multiple Rails Applications

You need either your own DNS server, with an CNAME record wildcard, or a provider who allows you to put in a CNAME record wildcard (most registrars should allow it in their control panel).

After that, you're talking just routes.rb, which would look like this:

ActionController::Routing::Routes.draw do |map|
  map.with_options :conditions=>{:domain=>'noobs.com',:subdomain=>'d2d'} do |beer|
    beer.connect '', :controller => "d2d"
    beer.connect 'main/stains/:content',
      :controller => 'main',
      :action => 'stains'
    ...
  end
  map.with_options :conditions=>{:domain=>'noobs.com',:subdomain=>'you'} do |beer|
    beer.connect '', :controller => "you"
    ....
  end
end

And from that, you can get as elegant as you want with the maps.  I like this too cause sharing controllers, helpers, etc. can be really handy.  I'm using it because my site allows users to create their own canned yet somewhat customizable sites on a seperate relatively unlabeled domain.  That site automatically lists the horses they have for sale on my main site, but on their non-branded "barn" site, as well as other listings like that.  Works great.  One application appearing as many more.

hackersapien wrote:

On the part of subdomain routing, did you need to configure your own DNS server to handle this or did you pull it off just using the routes.rb?I'd like for users to be able to put in the name of their application e.g.d2d and that gives them a subdomain to my application i.e. http://d2d.xapps.com, I mentioned this to someone and they suggested that I'd have to setup my own DNS server to handle the subdomains, I'm really interested in knowing whether it can be done programatically using rails instead of going the whole DNS(BIND) way.

10

Re: Deploying Multiple Rails Applications

Actually, I'm doing exactly what you are talking about.  When a user signs up for a "barnsite" on isoldmyhorse, then they get their own subdomain.  My dns for barnsite is a wildcard cname to the ip.

I have a route that checks to make sure the domain is barnsite.com (I use a seperate domain for these auto-created subdomain sites to simplify things), then redirects the request to barnsite controllers.  In the controllers I do the checking on what the subdomain is exactly, and the controllers hand out what content gets shipped off to whom.  You can probably move that out of the controllers into routes, but I couldn't figure out how to make dynamic subdomain routes.  Easy to do a :

this_subdomain = request.subdomains.first

in the controller.

Keep in mind that session cookies are domain based, which in my case is no biggie but could affect other implementations.

Re: Deploying Multiple Rails Applications

d2d wrote:

You need either your own DNS server, with an CNAME record wildcard, or a provider who allows you to put in a CNAME record wildcard (most registrars should allow it in their control panel).

After that, you're talking just routes.rb, which would look like this:

ActionController::Routing::Routes.draw do |map|
  map.with_options :conditions=>{:domain=>'noobs.com',:subdomain=>'d2d'} do |beer|
    beer.connect '', :controller => "d2d"
    beer.connect 'main/stains/:content',
      :controller => 'main',
      :action => 'stains'
    ...
  end
  map.with_options :conditions=>{:domain=>'noobs.com',:subdomain=>'you'} do |beer|
    beer.connect '', :controller => "you"
    ....
  end
end

And from that, you can get as elegant as you want with the maps.  I like this too cause sharing controllers, helpers, etc. can be really handy.  I'm using it because my site allows users to create their own canned yet somewhat customizable sites on a seperate relatively unlabeled domain.  That site automatically lists the horses they have for sale on my main site, but on their non-branded "barn" site, as well as other listings like that.  Works great.  One application appearing as many more.

If I have this correct, your writing to the routes.rb file with every subdomain thats added? Does this mean that if 5000 people register you'll set up maps like above for each and every subdomain?

12

Re: Deploying Multiple Rails Applications

hackersapien wrote:

If I have this correct, your writing to the routes.rb file with every subdomain thats added? Does this mean that if 5000 people register you'll set up maps like above for each and every subdomain?

No.

What I do is I have a dedicated domain name for these subdomain things, seperate from my main application.

Main application is : http://www.isoldmyhorse.com
Dynamic subdomain domain is : http://barnsite.com

my dns for barnsite is a wildcard cname.

routes.rb handles the differentiation between isoldmyhorse.com and barnsite.  the barnsite controllers handle the subdomain differentiation.

so, if you go to noob.barnsite.com, the route sent it to the barnsite controllers (instead of isoldmyhorse).  I have a model for barnsites, that includes all the information applicable to each specific subdomain (customizations, etc).  I do a find_by_subdomain(request.subdomains.first) to get which subdomain, and the controller /views hand out the appropriate content.

so I can have noob.barnsite.com, beer.barnsite.com, etc. etc.  all with their own content.  all without ever having to edit anything.

I _think_ there's probably a way to do this in the routes instead of the controller.  And people might frown on using the controller to do this cause it's a query...but...one extra query to the DB doesn't really bother me any.

Re: Deploying Multiple Rails Applications

I was just wondering whether I can have a scenario where the apps are hosted on server on a particular domain example www.xapps.com in the format of www.xapps.com/app1 but let the client have a subdomain on their webhost example http://johnapp.mywebsite.com and have the domain point to my app.