RSS
 

Posts Tagged ‘EC2’

Moving an old Rails 2 App to a New Server Using Bundler

04 Dec

I’ve had this Rails 2.3.2 app running on an Amazon EC2 small instance since March 2009. The app does some back-end processing for the media player software I created for SecondLife. The App stack is nginx, Rails 2.3.2, and some Mongrels. I decided to switch to a micro instance. This will cut my monthly bill from $60 to around $15. The 613MB RAM available on the micro-instance should be more than enough for my app. Amazon also just announced free basic resource monitoring for EC2 instances which means i can easily monitor resource usage to be sure my app isn’t running out of resources.

Packaging Rails Apps with Bundler
I first discovered Bundler while looking for an easy way to deploy Ragios. Ragios now uses Bundler for dependency management. With bundler I can just package my Rails app with all its dependencies (gems) and copy it to a new system. Bundler will install the right version of all the rubygems required for my app to run on the new system.

This is how I used it;

On the old Server
First install the Bundler Rubygem

sudo gem install bundler

Change directory to the root of your rails app

cd myapp

Create a GemFile for your rails app

bundle init

The Gemfile is created in the root directory of your rails app.
Edit the Gemfile and add the gems used by your app.

Example,

gem "rails"

or

gem "rails" , "2.3.2"

Include version number if you want bundler to use a specific version of the gem.

In my case, I wanted every gem on the new server to be exactly the same version as the gems on the old server. So I added version numbers for each gem.

If you want to list all the gems on your system to see their version number, run the ‘gem list’ command;

gem list

Below is a copy of my Gemfile:

source "http://rubygems.org"
 
gem "rails", "2.3.2"
gem "hpricot","0.8.1"
gem "simple-rss","1.2"
gem "rake","0.8.7"
gem "mongrel","1.1.5"
gem "mongrel_cluster","1.0.5"

You don’t need to add every gem on gem list to your Gemfile. You just need the gems you installed and required on your app. Bundler will detect the rest and install all dependencies. Example activesupport, actionmailer etc. will be installed by Bundler for the Rails gem.

Next ;

sudo bundle install

With this command, bundler will download and install all the listed gems and their dependencies. If you already have the gems installed on your system, bundler will use the one you already have. Below is the output from running bundle install with my Gemfile;

Fetching source index for http://rubygems.org/
Using rake (0.8.7) 
Using activesupport (2.3.2) 
Using actionpack (2.3.2) 
Using actionmailer (2.3.2) 
Using activerecord (2.3.2) 
Using activeresource (2.3.2) 
Using cgi_multipart_eof_fix (2.5.0) 
Using daemons (1.1.0) 
Using fastthread (1.0.7) 
Using gem_plugin (0.2.3) 
Using hpricot (0.8.1) 
Using mongrel (1.1.5) 
Using mongrel_cluster (1.0.5) 
Using rails (2.3.2) 
Using simple-rss (1.2) 
Using bundler (1.0.7) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

When you run bundle install, it will creates a file Gemfile.lock, this file contains a map of all your gems and their dependencies. Bundler will use this file to install gems on the new server.

Now copy the entire directory of your rails app to the new server. Make sure you have Gemfile and Gemfile.lock in the root directory of your Rails app when you copy it to the new server.

On the new server
With your Rails app now copied to this server,

Install bundler

sudo gem install bundler

Change directory to the root of your rails app;

cd myapp

Run bundler to download and install the gems required for your rails app to run on this server

bundle install

After this, your Rails app is ready to run on the new server. You can test it with WEBrick.

I then installed Apache and Phusion Passenger.

Ruby has come a long way, it took only a few minutes to move the app from the old server and setup it up on a new server.

You can see I also switched the app from nginx/mongrels to Apache/Passenger.

There are other ways to use bundler with Rails 2.3, since this is the only Rails app I plan to run on this server, I simply needed Bundler to install the right version of all my gems globally in the system for MRI to use.

Hope this was helpful.

More info on Bundler here.

 
 

Installing Rails + Apache + Passenger on Ubuntu 10.04 with Speedtrain

08 Aug

[Update 3/7/2011] Check the updated version of the speedtrain script for Ruby 1.9.2 and Rails 3 here: http://obi-akubue.org/?p=922 and https://github.com/obi-a/speedtrain. This version doesn’t install Apache, Passenger yet.

I have written a new version of the Speedtrain script. This version now includes an option to install Rails with Apache and Phusion Passenger ready for production. By default the script will install Ruby, Rubygems, Rails, Sqlite and a Sample app. But by using the advanced option, the script will also install apache and phusion passenger with directions to deploy the Sample app on passenger.

Usage:
Step 1. Download the script

wget http://whisperservers.com/speedtrain

Step 2. Make the script executable

chmod +x speedtrain

Step 3. Option 1
For a basic rails setup for development, which install Rails (Ruby,Rubygems) and Sqlite. simply run the script

./speedtrain

Option 2. For a production type Rails setup, which install Rails (Ruby,Rubygems), Sqlite, Apache and Phusion Passenger run the script with a -p switch.

./speedtrain -p

After the installation the script will create a folder in your $HOME directory called rails_apps. This is the folder where you can create and store your rails apps. The folder is at $HOME/rails_apps. The script will also create a sample Rails app at $HOME/rails_apps/test_app. You can run the sample app by typing

cd $HOME/rails_apps/testapp
ruby script/server

Then open your browser and type http://localhost:3000/

If you used the option to install Apache and Passenger, you will be guided interactively through the Apache + Passenger installation. After the installation, follow the simple steps displayed on the screen to deploy Rails apps on passenger. Very easy steps:)

This script is ready to run on most Ubuntu 10.04 installations. Works great and easy on ubuntu as host OS on a machine. Using virtual box Apache gets some permission issues but should be easy to fix if one digs deeper. It works perfectly on Amazon EC2 Ubuntu 10.04 instances.

To install Rails with Passenger on an EC2 instance with this script you can use this Ubuntu AMI image# ami-2d4aa444 or choose one from here

Using these AMIs the username is literally “ubuntu” as in
ssh -i yourkeyname.pem ubuntu@ec2-123-45-67-89.compute-1.amazonaws.com. I found this in the comments after some digging.

Have fun.

The source code for the new speedtrain script.

#!/bin/bash
#:Title   :Speedtrain on Rails
#:Date    :
#:Author  :Obi Akubue
#:Version :0.1
#:Description: This script installs Ruby on Rails on Ubuntu 10.04 with the option to install Apache and Phusion Passenger. (It creates a test rails app)
 
#Usage Option 1: To Install Rails (Ruby + Rubygems) + sqlite. Simply run the script by typing ./speedtrain
#Usage Option 2: To Install Rails (Ruby + Rubygems) + Sqlite + Apache + Passenger. Run script by typing ./speedtrain -p
 
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m' #light blue
cyan='\e[0;36m'
CYAN='\e[1;36m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m' # No Color
 
#begining  messages
 if [ "$1" = "-p" ]; then
    #option to install Apache + Passenger
        echo  -e "${GREEN} Welcome to Speedtrain on Rails"
        echo " Installing Rails (Ruby + Rubygems) + Apache + sqlite + passenger ..."
        echo " This will take a few minutes to complete.."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
 
        echo -e "${NC}"
 
    else
        echo  -e "${GREEN} Welcome to Speedtrain on Rails"
        echo " Installing Rails..."
        echo " This will take a few minutes to complete.."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
        echo -e "${NC}"
   fi
 
 read key
 
echo -e "${RED} updating system..."
echo -e "${CYAN}"
sudo aptitude update
 
echo -e "${RED} Installing build essential..."
echo -e "${CYAN}"
sudo apt-get install build-essential
 
echo -e "${RED} installing ruby..."
echo -e "${GREEN}"
sudo apt-get install irb libopenssl-ruby libreadline-ruby rdoc ri ruby ruby-dev
 
echo -e "${RED} installing rubygems..."
echo -e "${CYAN}"
cd /usr/local/src
sudo wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.6.tgz
sudo tar xzvf rubygems-1.3.6.tgz
cd rubygems-1.3.6
sudo ruby setup.rb
 
echo -e "${RED} updating rubygems..."
echo -e "${GREEN}"
sudo update-alternatives --install /usr/bin/gem gem /usr/bin/gem1.8 1
sudo gem update --system
 
echo -e "${RED} installing rails..."
echo -e "${CYAN}"
sudo gem install rails
 
echo -e "${RED} installing sqlite..."
echo -e "${GREEN}"
sudo apt-get install libsqlite3-dev sqlite3 sqlite3-doc
sudo gem install sqlite3-ruby
 
echo -e "${RED} creating dev directory..."
echo -e "${CYAN}"
mkdir ~/rails_apps/
 
cd ~/rails_apps/
echo -e "${RED} creating dev test app..."
echo -e "${GREEN}"
rails testapp
 
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${RED} installing Apache..."
echo -e "${CYAN}"
sudo apt-get install apache2
 
echo -e "${RED} installing passenger..."
echo -e "${GREEN}"
sudo gem install passenger
 
#install apache development headers
#required by passenger
#Apache 2 development headers --
sudo apt-get install apache2-prefork-dev
 
#Apache portable Runime (APR) development headers --
sudo apt-get install libapr1-dev
#install passenger
sudo passenger-install-apache2-module
fi
 
#ending message
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${YELLOW}"
printf "Your Apache Configuration file is /etc/apache2/httpd.conf\n"
echo -e "${NC}"
fi
 
echo -e "${CYAN}"
echo "Rails has been successfully installed"
echo -e "${GREEN}"
echo "Your Apps directory is $HOME/rails_apps"
echo "We created a Test Rails App for you at $HOME/rails_apps/testapp/"
echo "To run the Test App type"
echo -e "${YELLOW} cd $HOME/rails_apps/testapp"
echo " ruby script/server"
echo -e "${NC}"
 
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${GREEN}"
echo "To deploy the Test Rails app on passenger "
echo "Edit the Apache Configuration file and add the following virtualhost"
echo -e "${YELLOW}\n"
 
echo ""
echo "  ServerName www.yourhost.com"
echo "  DocumentRoot $HOME/rails_apps/testapp/public"
echo " "
echo "     AllowOverride all"
echo "     Options -MultiViews"
echo " "
echo ""
echo -e "${NC}"
echo -e "${GREEN}"
echo "then Restart Apache: sudo /etc/init.d/apache2 restart"
echo "and Open on your browser: http://yourdormain.com or http://your-ip.com"
echo -e "${NC}"
fi
 
 
 
Premium Wordpress Plugin