Multiple Rails Apps Hosted on One Amazon EC2 Instance

I wanted to get some e-commerce software up and running together with a content management system on the same EC2 instance.  Spree was chosen to fulfill e-commerce role and Refinery the static content management role. Spree is an open source e-commerce Rails app that has an active community and boasts a lengthy resume.  Refinery is also an open source project with pretty polished looks.  The two don’t play well together on the same database because they have some similar named tables.  Apparently, Rails 3.1 will solve this problem with some sort of namespace usage. Until that happens, the two should probably be installed as separate apps so that they can be configured to use different databases.

The basic procedure I followed to get this to work was just two install the two apps separately and then configure Apache’s VirtualHosts and the DNS configuration so that the applications are correctly pointed to.  Sounds simple enough, but here are some difficulties I ran into and how I fixed them:

Spree installation

Problem 1) Don’t use the latest version of the RubyGems installer (1.8.* as of this post). It would hang indefinitely when trying to do a simple ‘gem install rails’. Version 1.7.2 works well, so go ahead and make sure you have that.

Problem 2) When installing gems, the subsequent installation of ri-doc and rdoc would take forever. Not sure if this has something to do with the way EC2 works, but you probably don’t need to install this documentation onto a server anyway. You can speed the installation process up by doing

gem install  --no-rdoc --no-ri

Problem 3) Difficulty configuring the Gemfile correctly.  Had to specify the use of an older mysql2 gem, namely anything below version 0.3.0.  In the Gemfile, this just looks like

gem mysql2, '<0.3'

Problem 3a) The spree extensions had changed from what was written in the tutorial, so I had to search for the updated locations. This part of the Gemfile is supposed to look as follows:

gem 'spree_active_shipping', :git => 'git://github.com/spree/spree_active_shipping.git'
gem 'spree_product_assembly', :git => 'git://github.com/spree/spree-product-assembly.git'
gem 'spree_static_content', :git => 'git://github.com/spree/spree_static_content.git'

Problem 4) After the step where you create the databases (‘rake db:create’), you must also install the spree extensions as well as the site itself.  You can get a list of the generators available for this app by entering ‘rails g’.  In my specific instance, I had to do

rails g spree_product_assembly:install
rails g spree_static_content:install

Problem 5) ‘Error connecting to MySQL: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)’. Bitnami places the mysql socket in /opt/bitnami/mysql/tmp/mysql.sock. This may not be the best fix, but it works: create a symbolic link in /tmp/ to the correct socket location.  To do this, write

ln -s /tmp/mysql.sock /opt/bitnami/mysql/tmp/mysql.sock

Refinery installation

Problem 6) For better or worse, the refinery install does everything for you. Make sure you specify the database you want to use, along with the username and password. In my case, this was done with ‘refinerycms appname -d mysql -u user -p password’.

Apache configuration

Problem 7) The configuration file you’ll be editing on this Bitnami stack is located at ‘/opt/bitnami/apache2/conf/httpd.conf’. If you scroll down through the file you’ll find a place to write in the VirtualHost configuration. There, write ‘Include /opt/bitnami/apps/virutalhost.conf’. This tells Apache that it should look for the virtual host configuration at that location. The file should look something like the following:

NameVirtualHost *:80

<VirtualHost *:80>
        ServerName hostname.com
        DocumentRoot "/opt/bitnami/apps/refinery/public"
        RailsEnv production

        bitnami/apps/refinery/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
<!--VirtualHost>

<VirtualHost *:80>
        ServerName store.hostname.com
        DocumentRoot "/opt/bitnami/apps/spree/public/"
        RailsEnv development

        bitnami/apps/spree/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>