In this part, you are going to get your EC2 instance set up and ready to host your Rails application.
Creating EC2 instance
Log in to your AWS account and go to the EC2 management console by clicking the link shown below:
Click on the blue Launch Instance button to get to the wizard window where you can configure a new EC2 instance. The first step is selecting the AMI and we are going to use Ubuntu Server 12 LTS. Choose 64-bit and click Select.
Step 2, you are going to be prompted to choose the instance type which will also determine the size of CPU/Memory for your instance. Choose whichever you like, but MAKE SURE THAT YOU UNDERSTAND THE COST IMPLICATION OF SELECTING BIGGER INSTANCES. Or, for demonstration purposes, choose the cheapest and smallest type which is t1.micro. Then, click Next: Configure Instance Details button
I am going to skip step 3 for this tutorial and just use the default values. So, goa ahead and click Next: Add Storage.
In step 4, you can configure the volume size of your instance(s). The number will vary depending on the application’s need, but 50GB should be more than enough for the blog app that you created in Part 1. Once you are done, click Next: Tag Instance
In step 5, you can set key-value pairs metadata for yor application. This is useful if you want to categorize your instances by environment or by other variables. Name your instance blogapp and add a new tag named Environment and set its value to production. Once you are done, click Next: Configure Security Group
Step 6, this is where you can create rules for incoming traffic (by configuring which IPs and/or ports are going to be accessible). Because your instance is going to be hosting a web application and running a web server, you will want to open port 80. And, because you want to be able to SSH to it, you also want to open up port 22. SSH is added by default, so click Add Rule and choose HTTP for the protocol to open port 80. The wizard will automatically save this security group setting for you. Give it a different name and call it webserver. Make sure your setting looks like the following:
Click Review and Launch and you will be taken to a page to review your instance settings. Go ahead and click Launch. A pop up will appear asking you to create or select an existing key-pair which wil allow you to SSH to your instance. I am going to create a new key-pair and name it blogapp. Download the key-pay and click Launch Instance
Go to View Instances, and make sure that you see the newly created instance blogapp.
SSHing to your instance
After you have created your instance, you want to SSH to it and install packages that are essential to running your web app. The first thing that you need is the address of your instance. To get this, you need to go to the EC2 instance management console, select the blogapp instance, click the Description tab, and locate the Public DNS
The Public DNS should look something like ec2-12-345-67-891.compute-1.amazonaws.com. If you try to ssh to it (go ahead and give it a try), you are going to get a
Permission Denied error. You need to use the private key that you downloaded earlier at the end of the instance configuration to log in to your instance.
ssh -2 -i ~/Downloads/blogapp.pem firstname.lastname@example.org
Make sure that you change the location of the pem file and the hostname appropriately. Note that you are logging in as ubuntu user. This is the default user that gets created when you start a new instance with Ubuntu AMI.
Warning: Unprotected Private Key File
The above ssh command would often fail because the permission of your private key file is too open. You need to make it more restrictive by running the following command
sudo chmod 600 ~/Downloads/blogapp.pem
Installing essential packages
Once you are able to log in to your instance, you will want to install packages that will allow your pull your code and deploy the app. Run the following on commands:
sudo apt-get update sudo apt-get install build-essential libopenssl-ruby1.9.1 libssl-dev zlib1g-dev libmysqlclient-dev libpcre3-dev libcurl4-openssl-dev mysql-client-core-5.5 openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison nodejs subversion
The second command installs quite a bit, so please remove/add as necessary according to your app’s need.
In the following steps, you are going to install Ruby with the desired version on our Ubuntu instance. I am going to use Rbenv for managing the Ruby installations, but feel free to use RVM instead.
git clone git://github.com/sstephenson/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile echo 'eval "$(rbenv init -)"' >> ~/.profile
cd ~ git clone https://github.com/sstephenson/ruby-build.git cd ruby-build sudo ./install.sh
mkdir ~/.rbenv/plugins git clone git://github.com/dcarley/rbenv-sudo.git ~/.rbenv/plugins/rbenv-sudo
cd ~ exec $SHELL -l
Install Ruby and Bundler
rbenv install 2.0.0-p353 rbenv global 2.0.0-p353 gem install bundler
Generating SSH key and add it to Bitbucket/Github
If you created your app repository as private on Bitbucket/Github, you would not be able to clone it inside your Ubuntu instance unless you add the SSH key of your instance to your Github/Bitbucket profile. If your repository is public, you can skip this step entirely.
Create an SSH key by running the following command
Keep pressing enter until it has generated your ssh keys. The key generation should look something like this:
Your new SSH key should be in
~/.ssh/id_rsa.pub. Copy the content of that file and add it to your list of SSH keys in Github/Bitbucket.
Cloning your app and starting the server
Once you have added your SSH key, clone your repository to your home directory (Note: real-world scenarios often use
/var/apps to store its application codes) and install all of the necessary gems
cd ~ git clone email@example.com:denniss/blogapp.git cd ./blogapp rbenv rehash bundle install rbenv rehash
Once you have installed all the gem dependencies, you are ready to start your app
rbenv sudo passenger start -p80 &
The previous command is going to install all Passenger’s dependencies on your instance (which will take a while) and start the Passenger server. The output should more or less look like this:
Once Passenger is and started, you can see your app by entering the Public DNS to your browser. You should see the Hello World that you created from Part 1
There you go. Now you have your Rails app running in the cloud :)
If you have any questions/comments, please send me an e-mail at firstname.lastname@example.org