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!

Wednesday, May 30, 2012

AWS Ubuntu 12.04, apache and rails production server

>Load Ubuntu 12.04. I get all my base images from http://alestic.com/ You can choose the AWS region on the right pane. If you are using the free tier and don't want to incur any fees use the EBS boot so you can spin up a micro instance.

>Get updates and a few other nick nacks. Run 'sudo apt-get update' and 'sudo apt-get upgrade' Also I like to be able to login remotely to my server without the key so I allow remote login by going into /etc/ssh/sshd_config and set 'setpasswrd = yes'. Then restart ssh with 'sudo service ssh restart'. Now set your users password with 'sudo passwd ubuntu'.

>Install RVM ruby and rails Run 'curl -L get.rvm.io | bash -s stable'. This will probably fail. You need a few packages before rvm can run. Read the output in the error message and there is a section that lists a much of packages. Simply cut/paste this section into the terminal to install them. After this is successful you need to source rvm with 'source ~/.rvm/scripts/rvm', and now install your ruby version of choice. I installed 1.9.2 with 'rvm install 1.9.2'. Now that you have ruby you can 'use' that version by typing 'rvm use 1.9.2' and now you can install rails with 'gem install rails'.

>Install apache
 To install apache type 'sudo apt-get install apache2' Now if you navigate to your web the server in your browser you should see the cheezy 'It Works' massage from apache. Keep in mind you need to open port 80 on you AWS server.

>Install passenger
Run 'gem install passenger'. After the gem installs run 'passenger-install-apache2-module' the install will run and it will probably tell you that you are missing some stuff. So just install that stuff. In my case I need 'sudo apt-get install libcurl4-openssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev'. So after that stuff installs, run 'passenger-install-apache2-module' again. After passenger installs  DON'T CLEAR THE SCREEN!!! You need to copy the LoadModule statement and paste that into your /etc/apache2/httpd.conf file.

>Create a rails app
OK, we have all this cool configuration, now how do we wrap it all up with a rails app. OK, lets create one, but before we do we are in production now, so lets install a real database. Run 'sudo apt-get install mysql-server'. No, its not postgres, which is a REAL database, but it will do. MEEEOW...give that kitty a bowl of milk! OK, now lets create a rails app, and we will make mysql our default DB. Lets run 'rails new task_app -d mysql', and cd into that directory and go ahead and add 'gem therubyracer' to the Gemfile and run bundle (why do I have to do that?).  Also before I can run any rails scaffold commands I have to run 'export RAILS_ENV=production' why do I have to do this?

Sooo, now we can run some scaffolding 'rails g scaffold task name importance:boolean'

OK, if we check out app/config/database.yml we see the mysql configuration from our app, and it has given us a default name of 'task_app_production'. So lets run 'rake db:create' and 'rake db:migrate'.

Now we need to add this to our apache config file in /etc/apache2/httpd.conf

   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>
    ServerName www.yourserver.com
    DocumentRoot /home/ubuntu/task_app/public
    <Directory /home/ubuntu>
        Allow from all
    </Directory>
</VirtualHost>

..and one more thing before we lock and load. We need to go in to config/environments/production.rb and set 'config.assets.compile = true' rather than 'false'. Now restart apache with 'sudo service apache2 restart' and you should be good to go!











See this post for running multiple apps on a singe server.