Thursday, May 31, 2012

Multiple rails apps on a single server using RailsBaseURI

So after struggling with this for quite some time here is the configuration that works for me to host multiple rails apps on a single server using RailsBaseURI. Before we get into this, you need to be comfortable setting up a typical production rails server. See THIS POST for that.

OK, so if you followed my previous post, you already have one app, so you need to create a 2nd app. After you have done that we not need to edit you apache config file in /etc/apache2/httpd.conf. So I have two apps that are in my home folder. The apps are task_app and calculator_app, and there paths are /home/ubuntu/task_app and /home /ubuntu/calculator_app. We will be using the RailsBaseURI and you can find documentation for this HERE. So, adding the RailsBaseURI options for each of my apps, I now have this in my apache config file...

   LoadModule passenger_module /home/ubuntu/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/ubuntu/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.12
   PassengerRuby /home/ubuntu/.rvm/wrappers/ruby-1.9.2-p320/ruby
<VirtualHost *:80>
    Alias / /var/www/index.html

    ServerName www.hezekiahroad.com
    DocumentRoot /home/ubuntu
    <Directory /home/ubuntu>
        Allow from all
    </Directory>

    RailsBaseURI /task
    <Directory /home/ubuntu/task_app>
        Options -MultiViews
    </Directory>

    RailsBaseURI /calc
    <Directory /home/ubuntu/calculator_app>
        Options -MultiViews
    </Directory>

</VirtualHost>

Next we need to add sym links in the document root folder (in my case my home folder) that point to the app/public folder of each app. So I executed...

' ln -s calculator_app/public/ calc' and 'ln -s task_app/public/ task'



We can name these sym links anything we want, but the name of the symlink is the sub uri that the user will navigate to for each out so for the calc app the user will got to www.yourserver.com/calc and for the tasks app it will be www. yourserver.com/task.

Also notice I added the line ' Alias / /var/www/index.html' in the apache config file. If you do not add this, then when the user visits the root of the url (www.yourserver.com) then they see your applications folder structure. We don't want this, so here I have creating an alias for '/' (which is the url root) and redirecting it to /var/www/index.html which is the apache 'it works' file, but you can redirect this to where ever you like.

Soooo...with all that done, you should be able to reboot your server and visit www.yourserver.com/calc and see your calculator app (or whatever app you have in that location) and also visit www.yourserver.com/task and see your task app. And when you visit the root url, you should see the html file your alias points to.

Hope this helps some folks out. Good luck!

No comments:

Post a Comment