Download Hosting Ruby on Rails on Amazon AWS With Ruby PDF

TitleHosting Ruby on Rails on Amazon AWS With Ruby
File Size62.8 KB
Total Pages16
Document Text Contents
Page 1

Tutorial on hosting a web application (Ruby on Rails - RoR) on Amazon AWS with EC2,
EBS, Ruby Enterprise Edition (REE) and Phusion Passenger (mod_rails).
Author: K. C. Ramakrishna (www.rknowsys.com)
License: GNU-FDL - http://www.gnu.org/licenses/fdl.html
Date: 18-Nov-08

Please mail comments/corrections to kcr AT rknowsys DOT com, kcr AT members DOT
fsf DOT org,

Note: This is a follow up to the tutorial on hosting the site with mongrel.
This tutorial can be found here: http://docs.google.com/
Doc?id=dcn2ckbh_20hk4kc4d4
You may have to read it for some optimisations of RoR app to use EBS.

Credits and Disclaimer: Quite a bit of stuff exists in various tutorials - especially the part
where we configure mysql to use EBS - This excellent tutorial is on AWS resources page
(I don't have the exact URL). We just made an effort to consolidate a lot of relevant stuff
into one document with a consistent form. This is the general outline you need to follow
for any app on AWS.

Background:
We built a tourism portal in RoR for one of our clients. You have a look at it - -
www.tripladder.com
After building it, we were requested to host and manage it for them. Initially we went
with knownhost which is OK but a production RoR application needs more RAM than
what we get on most VPS plans - especially if we have image processing. We did
consider AWS but at that time it did not have EBS and the client did not initially expect
enough traffic to justify a 'scalr' managed cluster. We were looking for a replacement to
a dedicated server. Once EBS was launched, we immediately decided to move the site to
AWS. The Cost-benefit analysis is compelling.

Update:
tripladder.com is not live anymore, please look at www.vitalizeu.com but the below
process is still good.

The following tutorial starts off after signing up with AWS and configuring your desktop/
laptop to be able to connect to AWS and launch instances i.e. we assume that you have
completed the 'Getting Started' section of AWS.

We have started with the stock Fedora image and modified it to our requirements. We
could have used CentOS but Fedora-8 appeared at the top of the list and we went ahead
with it.

The application hosting has the following steps.
1. Launching an instance.
2. Installing RoR, gems, plugins...We used rmagick, hence we had to install

Imagemagick too.
3. Installing REE and Phusion (mod_rails)
4. Installing mysql.
5. Intalling the application (checkout from subversion).
6. Creating and attaching a EBS volume. Mysql with data on EBS
7. Modifying the RoR app to save user upload files to EBS.(http://docs.google.com/

Doc?id=dcn2ckbh_20hk4kc4d4)
8. Installing and configuring a production level ferret server
9. Configuring Apache to serve the application, caching optimisations for

performance.

Page 2

10. Configuring permanent public IP (covered) and DNS (we have the domain parked
with go daddy but this is not covered in this article)

11. Configuring smtp (email) support for RoR application.
12. Once we have the perfect server setup, save it to S3.
13. Periodic automated backups - Using Amazon snapshots.

1. Launching an instance and logging in:

[email protected]:~$ ec2-run-instances ami-2b5fba42 -k gsg-keypair -z us-east-1a
RESERVATION r-a864bec1 526262918289 default
INSTANCE i-xxxxxxxx ami-2b5fba42 pending gsg-keypair
0 m1.small 2008-11-09T10:19:29+0000 us-east-1a aki-
a71cf9ce ari-a51cf9cc

[email protected]:~$ ec2-describe-instances i-xxxxxxxx
RESERVATION r-a864bec1 526262918289 default
INSTANCE i-xxxxxxxx ami-2b5fba42 ec2-xx-
xxx-49-xxx.compute-1.amazonaws.com
domU-12-31-39-00-68-93.compute-1.internal running gsg-keypair 0
m1.small 2008-11-09T10:19:29+0000 us-east-1a aki-a71cf9ce ari-
a51cf9cc

# Authorise access on port 22 for ssh
[email protected]:~$ ec2-authorize default -p 22
GROUP default
PERMISSION default ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0

# Authorise access on port 80 for http
[email protected]:~$ ec2-authorize default -p 80
GROUP default
PERMISSION default ALLOWS tcp 80 80 FROM CIDR 0.0.0.0/0

# Authorise access on port 3306 for mysql admin.
[email protected]:~$ ec2-authorize default -p 3306
GROUP default
PERMISSION default ALLOWS tcp 3306 3306 FROM CIDR 0.0.0.0/0

1.1 Associating a permanent IP address with this instance.

[email protected]:~$ ec2-allocate-address
ADDRESS 75.101.158.167
[email protected]:~$ ec2-associate-address -i i-xxxxxxxx 75.101.158.167
ADDRESS 75.101.158.167 i-xxxxxxxx

1.2 Check by logging in

[email protected]:~$ ssh -i ~/AWS-do-not-delete/id_rsa-gsg-keypair [email protected]
xxx-49-xxx.compute-1.amazonaws.com

OR

[email protected]:~$ ssh -i ~/AWS-do-not-delete/id_rsa-gsg-keypair
[email protected]

Server is accessible;

Page 8

# - Now create your DB, Db-user and access privileges to the db-user.
# - You will have to do something like this:

[[email protected] tripladder]# mysql -u root -p
mysql> create database <database name>;
Query OK, 1 row affected (0.01 sec)

mysql> grant all on <database name>.* to '<database-user>'@'localhost'
identified by '<database-passwd>';

Query OK, 0 rows affected (0.00 sec)

4.3 Creating a schema or restoring from backup. (Optional) You can even do this later.

# after creating the database, you will have to create the schema and seed data -
You can do this: (Take care to edit the config/database.yml file as shown in

the next section before you run rake)
[[email protected] tripladder]# rake db:migrate

RAILS_ENV=production

OR

[[email protected] tripladder]# mysql -u <database-user> -p
<database name> < <Path to backup scripts>/backup.sql

4.4 Stop the mysqld

[[email protected] tripladder]# service mysqld stop

5. Installing the application

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# svn co https://<servername>/svn/tripladder/

5.1 Configuring the database access credentials

[[email protected] html]# cd tripladder/
[[email protected] tripladder]# vi config/database.yml
production:

adapter: mysql
database: <database name>
username: <database user>
password: <passwd for database user>
host: localhost

5.2 Configuring some stuff in environment.rb

# kc - Note - these configs are spread across the whole file. I am listing out the
sections that need changing.
# - kc Note - Tripladder uses google apps for Intranet and email.

[[email protected] tripladder]# vi config/environment.rb
ENV['RAILS_ENV'] ||= 'production'

RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

Page 9

# kc start - use google to send our email - 23Sep08
ActionMailer::Base.smtp_settings = {

:tls => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "tripladder.com",
:authentication => :plain,
:user_name => "<mail-user>@tripladder.com",
:password => "<mail-user-passwd>"

}
# kc end - use google to send our email - 23Sep08

5.3 Configuring stuff for ferret

# kc - Ferret will give some trouble unless all related directories have the right
ownerships/permissions. You may need to iterate here a bit.

5.3.1 Ferret configurations

[[email protected] tripladder]# vi config/ferret_server.yml
production:

host: localhost
port: 9010
pid_file: log/ferret.pid
log_file: log/ferret_server.log
log_level: warn

# kc - Note - Also comment out rest of the file.

[[email protected] tripladder]# vi config/environments/
ferret_environment.rb

INDEX = Index::Index.new(:path => '/var/www/html/tripladder/index')

# - kc - Now we will commit all these files to subversion so that we don't have
to keep worrying about this during svn updates (Developers will have to take
care to change relevant sections.
[[email protected] tripladder]# svn commit config -m "From production
server - kc"
Sending config/database.yml
Sending config/environment.rb
Sending config/environments/ferret_environment.rb
Sending config/ferret_server.yml
Transmitting file data ....
Committed revision 997.

5.3.2 Configuring ferret init scripts

# kc - Create this file and paste the contents (make changes as per your setup)

[[email protected] tripladder]# vi script/ferret_init_production
# kc - end of content for the file - Do not copy this line

#!/bin/bash
#
# This script starts and stops the ferret DRb server
# chkconfig: 2345 89 36
# description: Ferret search engine for ruby apps.
#

Page 15

[[email protected] software]# service mysqld stop
Stopping MySQL: [ OK ]

8.2 Ensure that services do not start when system reboots - This is important as we
have to attach EBS before we start the services.

[[email protected] software]# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] software]# chkconfig --list mysqld
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] software]# chkconfig --list ferret_init_production
service ferret_init_production supports chkconfig, but is not referenced in any runlevel
(run 'chkconfig --add ferret_init_production')
[[email protected] software]# chkconfig --add ferret_init_production
[[email protected] software]# chkconfig --list ferret_init_production
ferret_init_production 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[[email protected] software]# chkconfig ferret_init_production off
[[email protected] software]# chkconfig --list ferret_init_production
ferret_init_production 0:off 1:off 2:off 3:off 4:off 5:off 6:off

8.3 Bundle the volume and save it to S3.

[[email protected] ~]# ec2-bundle-vol -d /mnt/ -k .ec2/pk-
<xxxxxxxxx>.pem -c .ec2/cert-<xxxxxxxx>.pem -u <12 digit AWS user id without
hypehns>
Please specify a value for arch [i386]:
....# Lots of activity

[[email protected] ~]# ec2-upload-bundle -b tripladder-09Nov08 -m
/mnt/image.manifest.xml -a <accesskey> -s <secret>

[email protected]:~$ ec2-register tripladder-09Nov08/image.manifest.xml
IMAGE ami-3c15f155

8.4 Cleaning up

# kc - The AWS bundle tools seem to have memory leak - The used memory never
comes back to normal and seems to be using up ~ 1 GB of RAM
[[email protected] ~]# umount /mnt/data-store/
[[email protected] ~]# reboot

9. Bringing up the application after reboot.

[[email protected] ~]# mount /dev/sdh /mnt/data-store/
[[email protected] ~]# service mysqld start
Starting MySQL: [ OK ]
[[email protected] ~]# service ferret_init_production start
Starting ferret DRb server.
Install the ruby-openid gem to enable OpenID support
starting ferret server...
[[email protected] ~]# service httpd start
Starting httpd: [ OK ]

# kc - Site should be running perfectly.

10. Bringing up the application from AMI

Page 16

If ever your instance is killed, just do the following:
1. Launch your bundled instance wih the AMI id you got when you registered your
bundle,
2. Attach the EBS volume,
3. Associate the IP address
4. Start services - mysql, ferret and httpd.
5. Presto!!!

# - Important - NEVER put the mount option for EBS in fstab. When the system is
brought up from AMI, the EBS volume is still not attached and hence will not be
mounted. This will not allow the system to boot. You AMI will essentially be useless.
WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG,
WRONG !!!!!!!!!!!!!!!!
[[email protected] ~]# vi /etc/fstab
/dev/sdh /mnt/data-store ext3 defaults,noatime 0 2
# - Amazon is working on attaching a volume to EC2 instance during launch - when this
feature is available, we can use fstab....
WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG, WRONG,
WRONG !!!!!!!!!!!!!!!

Similer Documents