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>

Setting up a Git repository on an Amazon EC2 instance

Put a git repository on the cloud… all the cool kids are doing it, and you can too! Here’s how:

First and foremost, you need to add your EC2 identity to the ssh authentication agent.  This prevents problems with git later, namely getting the error “Permission denied (publickey).” when trying to do a git push to the EC2 repository.

ssh-add path/to/privateEC2key.pem

Now you can go ahead and create the git repository on the EC2 instance.

ssh username@hostname.com 
mkdir the_project.git 
cd the_project.git 
git init --bare

So not much going on here, all we do is create an empty repository and then leave. Now, on the local machine, you do something like the following:

cd the_project 
git init git add . 
git commit -m "Initial git commit message" 
git remote add origin username@hostname.com:the_project.git 
git config --global remote.origin.receivepack "git receive-pack" 
git push origin master

The ‘git config’ command is a fix that I found necessary to be able to push to the EC2 repository. I was getting errors that looked like “git: ‘the_project.git’ is not a git command. See ‘git –help’.”, and this fixed that problem. You can read more about this at StackOverflow.  At this point you should have successfully created an EC2 git repository! Congrats! Team members can now clone the remote repository like so:

git clone username@hostname.com:the_project.git

That’s it! Much thanks to Jamie Hill @ theLucid.com for his tutorial. Check it out for a slick tip on tracking the remote repository.



Hello, World.

Today marks the first day in my internship at the Ann Arbor T-Shirt Company.  I’ll be posting here to detail my troubles/discoveries/experiences along my journey through the lands of Ruby, Rails, e-commerce, the cloud, and custom apparel. Stay tuned!