Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active May 18, 2016 12:29
Show Gist options
  • Select an option

  • Save dannguyen/fed20952bea4b60fc5ea to your computer and use it in GitHub Desktop.

Select an option

Save dannguyen/fed20952bea4b60fc5ea to your computer and use it in GitHub Desktop.
Rails 4, Ruby 2, Capistrano 3

Setting up EC2 with Amazon

Configure and launch an instance

These steps can be done through the AWS Web Panel and choosing the Launch Instance wizard :

https://console.aws.amazon.com/ec2/v2/home?region=us-east-1

Choosing an Amazon Machine Image (AMI)

We will be using the Amazon Linux AMI, which is based off of CentOS.

Sample description, from the AMI with ID ami-fb8e9292 (64-bit):

The Amazon Linux AMI is an EBS-backed image. It includes Linux 3.10, AWS tools, Java 7, Ruby 2, and repository access to multiple versions of Apache, MySQL, PostgreSQL, Python, Ruby and Tomcat.

This is usally the top choice by default. Choose the 64-bit version.

Choose an instance type

This is where you can specify the performance and memory of a machine. m1.small is good enough for most light apps without costing you too much.

Configure Instance Details

These details involve configuration specific to other AWS settings you may have. You can usually leave this alone and move on.

Add Storage

Pick enough memory to host your initial app and its data. You don't want to have to resize the machine in production as it'll delete the existing data.

Tag instance

Add any meta-data you see fit to help you identify your machine (for a programmatic script that deals with the AWS API, for instance).

Configure Security Group

This is where you can allow/prevent access to your machine. For practice, you'll want to have at least SSH and HTTP open to the world:

SSH: TCP, 22, 0.0.0.0/0 HTTP: TCP, 80, 0.0.0.0/0

Review Instance Launch

This is simply a list of what you just configured.

Choose a security pair


Setting up the remote machine

These steps set up the software on your new EC2 instance that are required to host and run your web app. Ruby 2.0 is already installed on Amazon Linux, but you will have to install:

  1. Linux-dev tools (e.g. git)
  2. Node.js (to provide a JavaScript runtime needed for your Rails app to precompile JS assets and such)
  3. Passenger and Apache - Passenger is a Ruby gem, technically, but it'll serve as the bridge between your Rails app and the Apache web server. You could install Apache (or Nginx) standalone, but Passenger can do it for you

We do not have to worry about installing Rails or your Rails app just yet.

1. Setting up Linux-dev tools

On Amazon Linux/CentOS

Capistrano

This section assumes these steps have happened:

  1. You have built a Rails app and successfully have run it from your machine
  2. This app is pushed to a web-accessible repo, such as Github.

At this point, you can require and install the capistrano gem locally in your app. Start by putting the gem specs in Gemfile:

gem 'capistrano', '~> 3'
group :development do
    gem 'capistrano-rails', '~> 1.0.0'
end

Then run bundle install

cap install

After that's completed, run cap install, which will create a few new files, including config/deploy.rb and config/deploy/production.rb

Preparing your app

This is where you set up the Capistrano-specific config files...this is how the cap task runner knows how to act on your behalf, including accessing your Git repo, copying it over to your remote server, and running things like rake db:migrate on your behalf.

http://capistranorb.com/documentation/getting-started/preparing-your-application/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment