October 24, 2010

Rails 3 on Ubuntu 10.10 with RVM, Passenger and Nginx

Filed under: Uncategorized — Tags: , , — yasi8h @ 1:09 am

This covers setting up a Ubuntu 10.10 box to run rails 3 on production. Please notice it only focus on installing and setting up things mentioned in the title. However there are other things that you should configure and setup when deploying a application to production (like ntpd for example).

Installing RVM

We will need git before we can get started.

sudo apt-get install git

Install rvm according to the official guide.

bash < <( curl )


[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

to your .bashrc.

Once you edit your bashrc file execute the following to load rvm without logging out and back in

source ~/.rvm/scripts/rvm

Just to be safe check wether rvm is a function, which is what it should be.

type rvm | head -n1

Install ruby 1.9.2

See what you get out of rvm notes. you will need to install some stuff before you can compile ruby.

aptitude install build-essential bison openssl libreadline5 libreadline5-dev curl git-core zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev

The above packages covers some common stuff. It should be enough to get you started. However depending on what gems you want to install you might need to install additional dependencies.

rvm install 1.9.2

Set 1.9.2 as the default

rvm use 1.9.2 --default


Install passenger as instructed in rvm documentation

rvm 1.9.2 --passenger
rvm 1.9.2
gem install passenger
rvmsudo passenger-install-nginx-module

Let passenger downland and install nginx for you.

Modify /opt/nginx/conf/nginx.conf (thats the default location).

‘For Nginx users, replace the passenger_ruby line with:’

passenger_ruby /home/wayne/.rvm/bin/passenger_ruby;

Add a block like this to host your RoR site

server {
      listen 80; #the server will be running on this port
      root /home/deployer/your_rails_project/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;

Nginx init script


git clone git://
sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
sudo chown root:root /etc/init.d/nginx

More information on


sudo apt-get install mysql-client mysql-server

Get the source for your rails app. unzip it in your home folder (or where ever you want to. But be sure to edit the nginx config as appropriate).

cd to-your-rails-app-folder
bundle install
rake db:setup RAILS_ENV=production

sudo /etc/init.d/nginx restart

Thats it!


October 22, 2010

Getting Started with EC2

Filed under: Uncategorized — Tags: , , — yasi8h @ 10:06 am

Amazon EC2 allows you to create and manage server instances in the cloud with ease. They are scalable and easy to setup. There are many AMIs (Amazon machine image (AMI)) that you can use to get started. AMIs are basically server images that contain a whole OS installation and more. For example some AMI’s may have a web server with php setup. You can think of it as a virtual host image. This makes things much easier in the long run. You can use one of many AMIs out there to get started with your new server, rather than having to start from scratch. You can also build your own AMIs.

Other than AMIs you have AKIs and ARIs. AKI stands for amazon kernel image and ARI stands for amazon ramdisk image. You can lunch an AMI with a AKI of your choice. Obviously these should match and you should be sure that they work together.

Interfacing with AWS

There are several ways you can interface and communicate with AWS. When it comes to EC2 for example you could use the AWS Management console, the command line tools or the API. If you are a human you would prefer using the management console which is a web interface or the command line tools.

AWS Management console is a web application and its pretty easy on newbies. Where as command line tools have a ton of commands that you take you sometime to memorize (if you can memorize them at all). However thanks to grate documentation from Amazon it shouldn’t be hard to find help with any interface that you choose to use.

Setting up the Command Line Tools

You will need to download and setup the command line tools on your machine. Command line tools are written in java so you will need java to make use of them. You will also have to setup some environment variables. After setting up the command line tools you will have to download your ec2 private key and the certificate and put them some where safe. You will also need to setup some environment variables to point to these.

For complete up to date instructions on how to do this on your OS please refer to

After setting everything up my environmental variables were set like the following:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home

export EC2_HOME=~/.ec2
export PATH=$PATH:$EC2_HOME/bin


Setting up a SSH Key Pair

You will need to setup a ssh key pair so that you use it to login to server instances you lunch. You can create the key pair your self or let amazon do it for you. Refer the the guide for step by step instructions on how to do this.

Launching a Instance

Doing this with the AWS Management console is pretty straight forward. So i will focus on how to do it with the command line tools. Keep in mind that you get billed for the time that you are running your instances. So if you are only playing around EC2, be sure to terminate any instances you lunch after you are done.

Finding a suitable AMI

You can use the ec2-describe-images command to find a image that suites you. However just executing this command with no options will give you nothing. ec2-describe-images -a will give you all the available images. But be warned that its a lot of images, so it will take some time.

ec2-describe-images -o amazon –filter image-type=machine

would give you a list of images of machine type owned by amazon. For a full reference of all the available options for ec2-describe-images refer

You can use grep to filter out the results and find interesting images.

ec2-describe-images -a –filter image-type=machine | grep rails | more

However i find it easier to use to AWS management console (AWSMC from here onwards) much easier for this task.

Whatever image you choose to lunch remember its id. As you need that to lunch the image. I am going to go with

ami-6b26ca02 bitnami-cloud/rubystack/bitnami-rubystack-2.0-3-rails3-linux.manifest.xml

Root Devices

When you are choosing an AMI you would notice that some of them has their root device as instance store while some of them has it as EBS (elastic block storage). The basic difference between these instances are that if one uses the instance store, it will basically loose all its data when you terminate it. However a image that uses EBS will remember its state. Note that whats in the ram will be destroyed in both cases.

Security Groups

One thing that confused me the most when i started working on a server that was hosted on ec2 was the existence of security groups. You can think of them as firewall policies, sets of rules on what ports are to be allowed and not on a given group. By default every port is blocked.

Its a good idea to add a security group before lunching any instances as you will have to specify a security group for the instance that you are lunching. And without specifying one you won’t be able to access the instance through ssh.

You can use ec2-describe-group to find out what groups you already have, and what the configurations are on those. In my case i was using my companies AWS account and there were some existing groups. The output looked something like the following.

yazs-MacBook-Pro:~ yasi8h$ ec2-describe-group
GROUP 139637636613 default default group
PERMISSION 139637636613 default ALLOWS all FROM USER 139637636613 GRPNAME default
PERMISSION 139637636613 default ALLOWS tcp 22 22 FROM CIDR
PERMISSION 139637636613 default ALLOWS tcp 80 80 FROM CIDR
GROUP 139637636613 Web Server Incoming HTTP
PERMISSION 139637636613 Web Server ALLOWS tcp 22 22 FROM CIDR
PERMISSION 139637636613 Web Server ALLOWS tcp 80 80 FROM CIDR
PERMISSION 139637636613 Web Server ALLOWS tcp 443 443 FROM CIDR
GROUP 139637636613 SSH Only Only allow incoming SSH
PERMISSION 139637636613 SSH Only ALLOWS tcp 22 22 FROM CIDR

There are three groups here and they all allow ssh from the whole of internet. If you want to create a security group you will need to use ec2-add-group.

ec2-add-group ladygaga -d “strictly for gaga fans only”

the above command will create a group called ladygaga with a description. Note that you can always use the default group rather than creating new ones.

ec2-authorize ladygaga -p 22 would open up port 22 (used for ssh).

Running an Instance

ec2-run-instances ami-6b26ca02 -k yf-keypair

this would run a instance with the specified AMI. The k option here specifies the ssh key pair we want to use to authenticate our self’s when logging in to the instance. When you execute this command you should see some output like the following.

yazs-MacBook-Pro:~ yasi8h$ ec2-run-instances ami-6b26ca02 -k yf-keypair
RESERVATION r-e3915f89 139637636613 default
INSTANCE i-8833e3e5 ami-6b26ca02 pending yf-keypair 0 m1.small 2010-10-22T07:53:33+0000 us-east-1c monitoring-disabled instance-store

The second field have the id for the instance we just lunched. We can use this to get the status of our instance.

ec2-describe-instances i-8833e3e5

the output would be something like

yazs-MacBook-Pro:~ yasi8h$ ec2-describe-instances i-8833e3e5
RESERVATION r-e3915f89 139637636613 default
INSTANCE i-8833e3e5 ami-6b26ca02 domU-12-31-39-09-25-31.compute-1.internal running yf-keypair 0 m1.small 2010-10-22T07:53:33+0000 us-east-1c monitoring-disabled instance-store paravirtual

We can see that my instance is running now!. It might take a minute or two for your instance to be up and running. So give it sometime.

Wait, i think i forgot to tell AWS to assign this instance to the ladygaga security group. So lets terminate it and re launch it with the correct group assigned.

ec2-terminate-instances i-8833e3e5


INSTANCE i-8833e3e5 running shutting-down

Note that terminating instances who’s root device is a instance node will make it loose all data. If you just need to restart your instance use

ec2-reboot-instances i-8833e3e5

Lets verify that its terminated

ec2-describe-instances i-8833e3e5


yazs-MacBook-Pro:~ yasi8h$ ec2-describe-instances i-8833e3e5
RESERVATION r-e3915f89 139637636613 default
INSTANCE i-8833e3e5 ami-6b26ca02 terminated yf-keypair 0 m1.small 2010-10-22T07:53:33+0000 us-east-1c monitoring-disabled instance-store paravirtual

We can see that the instance is been terminated. Let’s lunch another and assigning it a security group.

ec2-run-instances ami-6b26ca02 -k yf-keypair -g ladygaga

After you have verified that it has started correctly. We can log in using SSH. To access it using SSH we need to know it’s public DNS name or the public ip. We can get this information by running

ec2-describe-instances i-f03eee9d


yazs-MacBook-Pro:~ yasi8h$ ec2-describe-instances i-f03eee9d
RESERVATION r-95aa64ff 139637636613 ladygaga
INSTANCE i-f03eee9d ami-6b26ca02 ip-10-122-53-159.ec2.internal running yf-keypair 0 m1.small 2010-10-22T08:08:05+0000 us-east-1a monitoring-disabled instance-store paravirtual

We can see that the public DNS name for this instance is We can connect to it through ssh. But we will have to use the key pair we used when we initially created the instance, to authenticate our self’s.

yazs-MacBook-Pro:~ yasi8h$ ssh -i .ec2/yf-keypair.pem

for most systems you can login as root. But for some systems (some ubuntu AMIs require you to login as ubuntu) this can be different.

Now that you are logged in you can configure the system and have a lot of fun!

Availability Zones and Regions

If AWS EC2 is the cloud it consists of regions. These regions are located in different parts of the world. And within the regions you have multiple availability zones. If your server carters mainly for visitors from a specific region you could run your instance in that region. And to improve the availability of your application you could have instances of your application running in multiple availability zones. So in case one availability zone goes down, your application can still survive if its hosted on other availability zones.

You can view the regions that are available to your account by executing

yazs-MacBook-Pro:~ yasi8h$ ec2-describe-regions


REGION eu-west-1
REGION us-east-1
REGION us-west-1
REGION ap-southeast-1

For a list of availability zones for a region

ec2-describe-availability-zones –region=eu-west-1


AVAILABILITYZONE eu-west-1a available eu-west-1
AVAILABILITYZONE eu-west-1b available eu-west-1

You can specify which availability zone your instance should be in, when you are launching it.

ec2-run-instances ami-6b26ca02 -k yf-keypair -g ladygaga –availability-zone us-east-1c

Thats it. I learned a lot by writing this. Note that my main source of information for this blog post was the AWS official documentation. The official documentation seems to be very good. So if you ever need to know something google it up. If you need to get more information on the command line tools and the different arguments that they accept refer the command line reference here (

And IMHO if you are new to AWS, its best to try things out using the AWSMC first before digging in to the command line. But once you get comfortable with using it. You could just use a combination of guessing, command line reference mentioned above and refer the in built help (ec2-command –help) to find your way through the command line tools.

October 16, 2010

Task Scheduling with Cron, RVM and Rails

Filed under: Uncategorized — Tags: , , , — yasi8h @ 4:00 am

I wanted to do something every hour in my rails app. Basically a scheduled task. So i looked around and there were some plugins. But i choose to go with plain old cron. This should be a very straight forward task. However there are some pitfalls to be aware of. Specially if you are using RVM which depends on some environmental variables that are set on the shell. I created a bash script that is called from corn as scheduled. If you do the same be sure to make the script executable. chmod +x

my crontab -e looks like this

# m h dom mon dow command
0,30 05-07 * * * /home/foobar/script task1
0,10,20,30,40,50 08-18 * * * /home/foobar/script task1
0,30 19-21 * * * /home/foobar/script task1
0,30 19-21 * * * /home/foobar/script task2
#clean up the logs # once a week every saturday, at 00:00 (midnight)
0 0 * * 6 /home/foobar/script task3

for clean code get the gist

I have scheduled three tasks here. I am calling the same script with different arguments.

The script.

Note the source statement in the beginning. This loads RVM. And i am using the rails runner to call the methods i want to. These are located in some model classes within my rails app. Using the runner is just one option, but there are many you can use. Take look at here for more information on this topic.
If you are interested in using a gem to do the scheduling for you. Take a look at this.
After writing this i found some more post on the net that shows some what more elegant and better ways of doing the same thing. I think you should definitely give them a read.

October 3, 2010

Rooting your HTC Desire and Custom ROMs: Notes and Links

Filed under: Uncategorized — Tags: , — yasi8h @ 2:52 pm

Recently i have been having a lot of fun messing with my HTC Desire. Here are some links and notes for future reference.


Custom ROMs

Rooting and installing custom roms is all fun. But be a bit careful. I mean some roms can be a bit unstable. They are good for testing and trying out when they are hot and new but not perfect for day to day use. And there is always the risk of bricking your phone ;).

Good luck fellow android users!


Blog at