Getting started quickly with an Amazon EC2 Ruby on Rails box

Posted by Jonathan Weyermann on November 14, 2017 at 12:00 AM

Cloud computing

This tutorial will assume you have an Amazon Web Aervices account. If you don't, its easy to create at aws.amazon.com. You will need a credit card, but everything in this tutorial will come under the free tier (for one year). 

In the interest of brevity, I'll choose an Amazon Machine Image (AMI) that  has ruby on rails pre-installed with a test app already deployed. The setup steps will still require us to create the EC2, connect to the EC2 and configure Nginx, set up Postgres (done on the same server for now) and start passenger. To deploy your own app, you will need to use capistrano in your rails app, or clone the image onto the server from github


A) Spin up the EC2


1) go to the EC2 section, click instances, and 'Launch Instance'

2) Click 'Community AMIs'

3) Search for 'Ruby on Rails'

4) Choose 'webapp_nginx_rails_passenger'

5) pick t2.micro (or something larger if you want, but t2.micro is the free tier)

6) if it's the only instance you'll be running on this account, you can click 'Review and Launch', otherwise make sure it's in the VPC you want. You can configure storage options and a host of other details if you wish, but it's not necessary.

7) Click Launch, select 'Create a new key pair', and enter a key pair name, and download the keypair - be sure to keep this file, as you will need it to access your instance by ssh. You can now click Launch to launch the instance.

8) go back the EC2 main page, click 'instances' and you should see your running instance (or see it getting ready, at least)

9) click on your instance, find your security group and click on it to modify it, and click 'inbound'

10) ensure it looks like this, otherwise click edit and add rules

Type
Protocol
Port Range
Source
Description

HTTP
TCP
80
0.0.0.0/0

SSH
TCP
22
0.0.0.0/0

You will want to add HTTP access to port 80 from 'everywhere'

11) In the EC2 main page, look for the VPC id of your instance (something like vpc-61348706) and then go to the vpc section, click the vpc your ec2 was launched in, click Actions, Edit DNS Hostnames, and select 'Yes'. This will allow you to view your instances -Public DNS (IPv4) address. You can now go back to your ec2, and copy the Public DNS address (the format is something like ec2-35-133-139-01.us-west-2.compute.amazonaws.com


B) Accessing your instance and reverse proxying nginx


1) open a terminal window  (if you're using Windows, use PuTTy to ssh) and and add your keypair (for example, if you called the keypair 'demo' and placed it in ~/.ssh, the command would be 

chmod 400 ~/.ssh/demo.pem
ssh-add  ~/.ssh/demo.pem

The first command reduces your permissions to make the pem file valid to use, while the second command adds it to your ssh keys 


2) you should now be able to access the instance with using the ec2-user and the public DNS

ssh ec2-user@ec2-35-133-139-01.us-west-2.compute.amazonaws.com


3) set up nginx

once on the server, type to open vim to configure the nginx.conf file

sudo vi /opt/nginx/conf/nginx.conf


inside the server brackets, add

location / {

proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_buffering off;


4) start nginx

sudo service nginx start


C) Configuring Postgres and starting the Passenger Server


There is a small sample app that comes with the image - deploying your own app will be covered in a future tutorial. I recommend using capistrano to deploy your app 

For today we will cover how to configure the postgres database locally (on the same server) so we can run the app


1) configuring the envs

enter the following on the command line on your server. you can use any USERNAME or PASSWORD

export DB_HOST=localhost
export DB_USERNAME=ec2_user
export DB_PASSWORD=12345678


2) install postgres

You can enter these commands to take care of it for you

sudo yum -y install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs
sudo service postgresql initdb
sudo sed -i.bak -e 's/ident$/md5/' -e 's/peer$/md5/' /var/lib/pgsql9/data/pg_hba.conf
sudo /sbin/chkconfig --levels 235 postgresql on


3) configure your database

sudo -u postgres psql


set the postgresql user password (this is a different password than the one you will use for the ec2_user)

postgres=# \password


Create a new user and password and the user's new database:

postgres=# create user “ec2_user" with password ‘12345678';
postgres=# create database "test_app_production" owner "ec2_user";


4) start postgres

sudo service postgresql start


5) start passenger 

cd ~/testapp
passenger start  --daemonize


That's it!  There should now be a sample rails app running at ec2-35-133-139-01.us-west-2.compute.amazonaws.com in your browser