Deploying Rails 3.1 Apps to Amazon EC2: A Guide

As a data scientist or a software engineer, the ability to deploy your Rails applications on a scalable and robust platform like Amazon EC2 is an essential skill. This blog post will guide you on how to perform this task effectively and efficiently.

Deploying Rails 3.1 Apps to Amazon EC2: A Guide

As a data scientist or a software engineer, the ability to deploy your Rails applications on a scalable and robust platform like Amazon EC2 is an essential skill. This blog post will guide you on how to perform this task effectively and efficiently.

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a web service from Amazon Web Services (AWS) that provides resizable compute capacity in the cloud. It’s designed to make web-scale computing easier by providing a simple interface to obtain and configure capacity.

Prerequisites

Before proceeding, ensure you have the following:

  1. An AWS account
  2. A Rails 3.1 application
  3. Basic knowledge of Unix commands

Step 1: Set up EC2 Instance

First, you need to set up an EC2 instance. Login to your AWS account and navigate to the EC2 dashboard. Click on Instances > Launch Instance. Choose an Amazon Machine Image (AMI) - for Rails, we recommend the latest Ubuntu Server version.

Select an instance type; t2.micro is sufficient for small apps and is free-tier eligible. Click Next: Configure Instance Details, leave the default settings, and click Next: Add Storage. The default storage size is 8GB, but you may need more for larger apps.

Proceed through the steps until you reach Configure Security Group. Create a new security group with SSH and HTTP rules. For SSH, the source should be My IP to ensure only you can SSH into your instance. For HTTP, the source should be Anywhere, so anyone can access your web app.

Click Review and Launch > Launch. You’ll be prompted to create a new key pair or use an existing one. This key pair is necessary to SSH into your instance.

Step 2: Setting up the Environment

Once your instance is up, SSH into it using your key pair. First, let’s update the packages:

sudo apt-get update -y && sudo apt-get upgrade -y

Install Ruby and Rails. We’ll use RVM (Ruby Version Manager) to manage Ruby versions.

\curl -sSL https://get.rvm.io | bash -s stable --rails
source ~/.rvm/scripts/rvm
rvm install 1.9.3
rvm use 1.9.3 --default

Now, we’ll install some necessary libraries:

sudo apt-get install -y build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev nodejs libsqlite3-dev sqlite3

Next, install the Rails gem:

gem install rails -v 3.1

Step 3: Deploying the Rails App

Clone your Rails 3.1 app to the instance. If it’s on GitHub, use the following command:

git clone https://github.com/yourusername/yourrepository.git

Navigate to your app’s directory, install any necessary gems, and migrate your database:

cd yourrepository
bundle install
rake db:migrate RAILS_ENV=production

Step 4: Install and Configure Passenger & Nginx

Phusion Passenger is an application server for Ruby, while Nginx is a web server which we will use as a reverse proxy for Passenger.

gem install passenger
passenger-install-nginx-module

During the Nginx installation, choose the option 1. Yes: download, compile and install Nginx for me. (recommended). The installer will download Nginx and compile it with Passenger support.

Next, we need to configure Nginx. Open the Nginx configuration file:

sudo nano /opt/nginx/conf/nginx.conf

In the server block, add the following:

server {
    listen 80;
    server_name yourdomain.com; # Replace with your domain
    root /home/ubuntu/yourrepository/public; # Replace with your app's public directory
    passenger_enabled on;
    rails_env production;
}

Restart Nginx:

sudo service nginx restart

That’s it! Your Rails 3.1 application should now be accessible on your EC2 instance’s public IP or domain. You’ve successfully deployed your Rails 3.1 app to Amazon EC2.

Remember, deploying is not a one-time operation but a process that involves continuous integration and delivery. Keep learning and improving your deployment skills. Happy deploying!


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.