Welcome Guest | Login

Deploying multiple Rails apps under one account



This is a wiki article created by HostingRails users. Please login or signup to make edits.


    When you signup for a HostingRails account (any of them) you get unlimited databases, subdomains, and addon domains.

This tutorial is only meant for FastCGI deployment. A Mongrel instance or a Mongrel cluster can not serve multiple Rails applications. That is, unless you bundle all your Rails apps into one Rails app.

Please give this thread a read before you start the following process - it will help you understand what is going on behind the scenes so you can avoid problems. This gets a little tricky if you haven't symlinked or used addon domains before.

----

    After following the basic FastCGI deployment tutorial for setting up your main Rails app, the process for setting up an additional app running as a subfolder, subdomain, or totally different domain is essentially the same process.

You'll want to make EVERY rails app reside in your root (designated as [~]).  From here all the magic happens with symlinking.  

So - let's run through this tutorial assuming you already have app1 running and you want to run app2 as either a subfolder (e.g. www.domain.com/blog), subdomain (e.g. blog.domain.com), or addon domain (www.example-blog.com)



To start, your root directory should look something like:

app1
public_html
www
Login to your root and:
[~]# rails app2
Perhaps you're already at this point, but if not just follow the basic deployment tutorial until you get to the symlinking part and then continue. Basically do everything for app2 as you did for app1 but stop when you get to the symlinking and switch to this tutorial.

If you're adding a domain or subdomain, login to your cPanel and create it now.  Make sure not use use abnormal characters like '_' in your domain info or you will get an error.  When done, this processes will place a directory in your public_html (let's call it 'app2folder') that will later be designated as the root of your app2.  If you're going to run app2 as a subfolder of your app1, then you don't need to do anything in cPanel.  



So, first a little review. Assuming app1 is your main app, following the basic deployment tutorial you would have already done:

[~]# mv public_html public_html_backup   
This is necessary for the symlink, because the link cannot already be there.  To say it another way, the second half of the symlink cannot already exist.

Then you did:

[~]# ln -s ~/app1/public ~/public_html
Which made your app1 live.

So, now that your app2 is setup and the app2folder is placed by cPanel in your public_html ( which is /app1/public ), you need to remove the app2folder for the symlink to work correctly.  (If you want to run app2 as a subfolder then obviously cPanel did not create a folder so you can skip just this one step below.)

[~]# mv ~/public_html/app2folder ~/public_html/app2folder_backup
so now you're set to create the symlink (all cases should do this):

[~]# ln -s ~/app2/public ~/public_html/app2folder
Finally, in the .htaccess of app1, add these lines where it says to (i.e. above the rewrite rules for Rails):

RewriteCond %{REQUEST_URI} ^/app2folder.* 
RewriteRule .* - [L]
Make sure the these lines are above the line you added to the .htaccess when you first configured app1 (shown below).

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]


And that's it.  This will work for addon domains and subdomains.

Don't forget to update your routes.rb for app2 and delete the index.html file in the app2folder as you did for the first app.

If you're using an addon domain, you can now point its nameservers to your server.


Next, and ONLY if you're running your Rails app as a subfolder (e.g. www.domain.com/app2folder) then you need to add the following to your /app2/public/.htaccess

RewriteBase /app2folder
and of course (still ONLY if you're running your Rails app as a subfolder) then in your enviornment.rb of app2

ActionController::AbstractRequest.relative_url_root = "/app2folder"
Note: check out this thread to learn more about the above command




This process can be repeated for multiple apps.  If it didn't work please feel free to ask a question in our deployment troubleshooting forum and/or update the content here for the next developer.



"Finally, in the .htaccess of app1, add these lines where it says to (i.e. above the rewrite rules for Rails):

RewriteCond %{REQUEST_URI} ^/app2folder.*
RewriteRule .* - [L]" just to clarify, does this mean that for each new application you add to your account (using eg. add-on domains), you would also need to modify the .htaccess document in side the public directory of your main application? [bitbutter]




Hello Guest! In order to edit this article you must be an active client with us, please log in or sign up today!






Contributing Author(s):
William
Brian
Bruce
Cathyb
Wackimonki
Bitbutter