rubybroker-ec2 is a collection of Capistrano recipes geared towards getting Merb applications live on the internet with minutes. It utilizes Amazon’s Elastic Compute Cloud (EC2) and the Apache module Phusion Passenger to give you a straightforward, automated, and repeatable deployment process.
It builds on work started by George Malamidis and developed further whist deploying my side project.
Using rubybroker-ec2 you will bootstrap a Debian instance (ami-bded09d4) with the following stack; Ruby 1.8.7, Rubygems 1.2.0, Apache2, Passenger, Mysql and Git. Your application will be stored in the ephemeral storage (/mnt) and Apache will be configured correctly to serve it with Phusion Passenger.
Prerequisites
To use rubybroker-ec2 you will need the following:
- An EC2 account setup and configured to manage EC2 instances (described in detail here).
- A Merb application versioned in a Git repository with all dependancies stored in its “gems” directory.
- The Capistrano gem installed,
capify . run on the application and the basic configuration of your application name and Git scm details completed.
Once you have this you can follow the next few steps to very quickly get you application live.
Configuration
The first step is to download and install the gem:
$ sudo gem install ruby-broker-ec2-0.0.1.gem
Add it as a requirement at the bottom of your Capfile:
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'
require "rubybroker-ec2"
now copy the following configuration into your deploy.rb file and complete the first section with your AWS credentials:
# Replace the examples with your AWS credentials
set :account_id, "your account id here"
set :access_key_id, "your access key id here"
set :secret_access_key, "you secret access key here"
set :pk, "your pk here"
set :cert, "your cert here"
set :keypair, "your keypair here"
# Complete this section in when instance started
set :instance_id, "your instance id from cap instance:start here"
set :instance_url, "your instance id from cap instance:describe here"
role :app, instance_url
role :web, instance_url
role :db, instance_url, :primary => true
Up and Running
The next few steps bootstrap an instance for you, configure Capistrano so it can access it and install the final pieces of software which are not part of the AMI.
Get the instance started an instance with:
$ cap instance:start
The output of this command will contain the instance id, this should be entered in the :instance_id field of you deploy.rb. When this is completed invoke:
$ cap instance:describe
In the description of the instance should be the instance url (its looks something like ec2-75-111-111-111.compute-1.amazonaws.com) which is required for your deploy.rb. Note: it may take a few attempts to get the instance url while the instance starts up.
Thats Capistrano configured which allows us to install and configure the final pieces of software using:
$ cap instance:bootstrap
The instance will now have the full stack on but mysql will require some configuration specific to your application. The easiest was to do this is to connect to the instance using:
$ cap instance:ssh
You can set mysql root password and create the production database:
$ mysqladmin -u root password [password]
$ mysqladmin -u root -p create [databasename]
Application Specific Configuration
This is also the time to configure the instance for any requirements specific to your project.
For my side project I had to ensure that the instance could access my git repository which is hosted at GitHub. This is as easy as creating a authentication key with:
$ ssh-keygen -t rsa
then coping the contents of ~/.ssh/id_rsa.pub to the “Deploy Keys” of the admin section of the GitHub repository.
I also required RMagick and ImageMagick which I installed using the following command:
$ apt-get install imagemagick librmagick-ruby1.8 libfreetype6-dev xml-core
When you have finished your own customisation you are ready to deploy.
Deployment
Phusion Passenger supports Merb applications through the Rack interface. So before your first deployment you need ensure you have a file named config.ru to the root your application containing the following:
# config.ru
require 'rubygems'
require 'merb-core'
Merb::Config.setup(:merb_root => ".",
:environment => ENV['RACK_ENV'])
Merb.environment = Merb::Config[:environment]
Merb.root = Merb::Config[:merb_root]
Merb::BootLoader.run
run Merb::Rack::Application.new
The actual deployment steps are the same as the standard Capistrano deployment but the implementation has been customised for this stack.
Set up the directories on the server:
$ cap deploy:setup
Deploy the code, migrate the database and start Apache with:
$ cap deploy:cold
And… Thats it! Hitting your instance url you should now see you app.
Other Commands
rubybroker-ec2 also includes helpers for a few other common commands.
Tell Passenger to restart your application with:
$ cap deploy:restart
Analyze Phusion Passenger’s and Apache’s real memory usage with:
$ cap passenger:memory_stats
Inspect Phusion Passenger’s internal status with:
$ cap passenger:status
Problems?
If you don’t see your application you will need to dig around the in Apache error logs and most of the time you can get a pretty good idea of what went wrong. These can be found /var/log/apache2/error.log and /mnt/apps/{application}/current/logs/error.log.
The first time I deployed the couple of problems I had were:
- Wrong permissions on the cache directories
- Sass not generating CSS when running under Apache
TODO’s
There are a few feature still which include:
- Add the domain name to the configuration
- Configure caching to work correctly with Apache / passenger
- Configure MYSQL to work from the ephemeral storage
References and Links