How I Set Up My EC2 Instance for Rails & Litespeed

16Feb07

The below are the step by step instructions I used to set up my EC2 instance for my soon to launch rails app (Quizical.net). It uses Rails and Litespeed as the server. At the end of the install, it makes heavy use of my Capistrano and EC2.rake tasks to install my app.

Warning. I’m not an expert on setting up a linux box. So caveat emptor. This is why I had to document everything I did so I could go back and do it again if I had to.

Set Up the EC2 Tools

Most of this section I followed the Amazon Getting Started Guide. but I also used these sources (1, 2)

First we have to set up the EC2 tools on our local computer (Mac OS X).

  • Download the Command Line Tools from Amazon
  • Unzip it to the directory of your choice. I put it in: /Documents/Projects/ec2/api/
  • Copy the below into /etc/profile:


export JAVA_HOME=/Library/Java/Home
export EC2_HOME=~/Documents/Projects/ec2/api/
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=~/Documents/Projects/ec2/auth/pk-4IMZKCL2QEK2FDPLWCKICJNOTNUNWT24.pem
export EC2_CERT=~/Documents/Projects/ec2/auth/cert-4IMZKCL2QEK2FDPLWCKICJNOTNUNWT24.pem

Now, we need to generate the private key pair. From the Amazon “Getting Started Guide”

You will be running an instance of a public AMI. Since it has no password you will need a public/private keypair to login to the instance. One half of this keypair will be embedded into your instance, allowing you to login securely without a password using the other half of the keypair.

# ~/Documents/Projects/ec2/api/bin/ec2-add-key pair rails-server

Which will generate something like….
KEYPAIR rails-server a8:20:2a:ad:c0:16:b8:20:ff:45:43:7e:54:8c:55:ce:43:36:32:d1
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAjVwZnRILPoTPSpij4+lLq7ByP8QGMkJOq50Z9Hf3+HOw+6v7MihrZaeprTz68+Lyi9O3P2MGrEFJmgEmvpIdmjpS+vfGlPd+g7BgvFMej+hiXONJZISxG6XbmnbmE1oaxblPgIR2
tMZ6sdwZ3xJt2+Pped8eqDcuYm4TCHZhZM9Qv3sCycoJ1fFAr5d3EjGijNTHfrWBcDA=
-----END RSA PRIVATE KEY-----

Copy everything between (and including) the “—–BEGIN RSA PRIVATE KEy” and “—–END RSA PRIVATE KEY—–” and paste it into a text file named ‘id_rsa-rails-server’. I saved my key in a directory called: /Documents/Projects/ec2/auth/

Next we need to change permissions to this file so its readable and writable.

cd Projects/ec2/auth/
chmod 600 id_rsa_rails-server

Let’s find an instance to start with. Instances are what Amazon refers to the disk images. These are the basic server configurations. We’ll start with one and customize to our needs.

cd /ec2/api/bin/

./ec2-describe-images -a

That will generate a list of all public images and our own images we had previously saved. The ‘-a’ parameter instructs it to return all public images and your private images as well. If you leave off the -a then it will only return your private instances.

I chose Marcin’s Fedora Core 6 Lite install (ami-78b15411).

Run the instance

./ec2-run-instances ami-78b15411 -k rails-server

The -k rails-server parameter is the name of our private key we created earlier.

This will take a few minutes to commission. You can keep checking with:

./ec2-describe-instances

It will tell you whether it is still pending or return the URL when it is ready. The URL will look something like:

domu-12-31-33-00-01-9f.usma1.compute.amazonaws.com

Log in!

Now, just like a regular remote linux box we can log in with:

ssh -i /Documents/Projects/ec2/auth/id_rsa-rails-server root@domu-12-31-33-00-01-9f.usma1.compute.amazonaws.com

Now we can start customizing our image.

Add users and groups

(The reference I used for linux users and groups is here)
Create a group:
groupadd www

Add you as a user:
useradd -g www steveodom
passwd steveodom
Changing password for user steveodom.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Repeat the above to add user lsws. I run with litespeed server with this username and restrict its permissions.

I created a directory for my self (mkdir /home/steveodom/). This is where I’ll store my rails app. Set the permissions of /home/steveodom to allow members of the www group to access it.
chmod g+rwx /home/steveodom

Now add myself and www to sudo file: (use visudo and add to the end steveodom ALL = ALL)

Install the packages I need:

1. yum install wget tar zip fileutils sudo make gcc
2. yum install ruby ruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs ruby-devel rsync ruby-mysql.i386

Install Mysql (Source)
yum install mysql mysql-devel mysql-server mysql-admin

Instruct mysql to start on reboot:
/sbin/chkconfig mysqld on


Install Subversion

yum install subversion
export SVN_EDITOR=vi

Amazon Tools:
wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm
rpm -i ec2-ami-tools.noarch.rpm

Ruby Gems:
wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
tar zxvf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo ruby setup.rb

Let’s clean up a little
cd ..
rm ruby* -drf
rm ec2-ami-tools.noarch.rpm

*change to user steveodom*

Rails:
sudo gem install rails

Install the lsapi gem needed for litespeed:
sudo gem install ruby-lsapi

LiteSpeed

I chose Litespeed server over Mongrel. I’ll post another day the reasons why.

I followed the instructions here:
1. wget http://litespeedtech.com/packages/2.2/std/lsws-2.2.6-std-i386-linux.tar.gz
2. tar xf lsws-2.2.6.tar.gz
3. cd lsws-2.2.6
4. sudo ./install.sh

Went through the installation wizard. Selected the default ports. I set it up to run with user lsws and group lsws. They have no privileges.

Note: to start lightspeed: /opt/lsws/bin/lswsctrl start [restart | cancel]

This screencast is very useful too for setting up litespeed to use rails.

Note: Since litespeed is running as lsws I had to give my /home/steveodom/quizical directory permission for lsws to access it.

I did it with:
-sudo /usr/sbin/usermod -a -G steveodom lsws [where steveodom is the name of the group]-

I checked the permissions by doing:
sudo -u nobody ls -la /home/steveodom/ [should get permission denied]
sudo -u lsws ls -la /home/steveodom/ [should show you the directory listing]

MySQL Setup (Source)

Set a password for root!
mysql -u root -p

You will be prompted for a password, and as the password is currently empty, simply press the enter key.

Change the password by typing the following command:
SET PASSWORD FOR root@localhost=PASSWORD('newPassword');

Delete user accounts that have no usernames and/or passwords: (These are insecure accounts and should be deleted).
use mysql;
delete from user where user='';
delete from user where host='localhost.localdomain';

Create a new mysql account…
GRANT ALL PRIVILEGES ON *.* TO 'steveodom'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
FLUSH PRIVILEGES;

..And allow him to connect remotely:
grant all privileges on *.* to steveodom@66.90.167.160 IDENTIFIED BY 'xxxxxxx';
exit;

Now let’s open up the port to allow remote access to mysql:
(back on your home machine)
ec2-authorize default -p 3306 (ssh)

At this point, before adding my rails app, I bundled and registered this instance. I used my Capistrano recipes and EC2.rake tasks. Using that, bundling, uploading to S3, and registering is as simple as typing (from local machine):
cap complete_bundle

Update the Server using Capistrano:
This part uses my capistrano recipes and EC2.rake tasks. You must run the below from your local machine. It will patch the server, checkout the latest code, do the migrations, etc.
cap initial_install

Configure the App in Litespeed

Goto the admin GUI for litespeed by pasting in the url for your new instance (see above) and appending ‘:7080′ on the end. Example: http://domu-12-31-33-00-01-F8.usma1.compute.amazonaws.com:7080

For this part it is helpful to watch Bob Silva’s screencast.

  • delete existing virtual host and its listener
  • clicked on EasyRailsWithSuExec
  • named my virtual host ‘quizical’
  • for domain I put ‘*’
  • for virtual host root I put ‘/home/steveodom/quizical/current’
  • instantiate it
  • restart
  • add your listener.

Once its restarted, you need to go to the quizical virtual host and change the location to /$VH_ROOT/current.
To do that, click on the Context Tab -> find the rails line – > click edit and change the location box to /$VH_ROOT/current.

You also have to put litespeed in development mode if that is what you want to run.

You should now have a running rails app. To check it, goto your url with :8088 appended at the end (or whatever ports you chose during the litespeed setup).

About these ads


12 Responses to “How I Set Up My EC2 Instance for Rails & Litespeed”

  1. I’m really curious and I think I’ll give it a try (just for fun, I still think it’s way to expensive).
    Can you please post the output from /proc/cpuinfo and /proc/meminfo ?

  2. Here it is Piku:
    ———
    -bash-3.1# cat cpuinfo
    processor : 0
    vendor_id : AuthenticAMD
    cpu family : 15
    model : 37
    model name : AMD Opteron(tm) Processor 250
    stepping : 1
    cpu MHz : 2405.452
    cache size : 1024 KB
    fdiv_bug : no
    hlt_bug : no
    f00f_bug : no
    coma_bug : no
    fpu : yes
    fpu_exception : yes
    cpuid level : 1
    wp : yes
    flags : fpu tsc msr pae mce cx8 apic mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt lm 3dnowext 3dnow pni lahf_lm ts fid vid ttp
    bogomips : 4823.02
    ————-

    -bash-3.1# cat meminfo
    MemTotal: 1740944 kB
    MemFree: 137044 kB
    Buffers: 79360 kB
    Cached: 188968 kB
    SwapCached: 0 kB
    Active: 895412 kB
    Inactive: 598632 kB
    HighTotal: 1003528 kB
    HighFree: 51256 kB
    LowTotal: 737416 kB
    LowFree: 85788 kB
    SwapTotal: 917496 kB
    SwapFree: 917460 kB
    Dirty: 64 kB
    Writeback: 0 kB
    Mapped: 121256 kB
    Slab: 83552 kB
    CommitLimit: 1787968 kB
    Committed_AS: 213832 kB
    PageTables: 740 kB
    VmallocTotal: 118776 kB
    VmallocUsed: 628 kB
    VmallocChunk: 118084 kB

  3. Thanks :)
    Wow, that’s nice. 1.7G is enough RAM for mongrels and even memcache.
    It’s really good for scaling and you can go three tiers and power machines during peak hours and poweroff when they are no longer needed (of course you’ll need load balancing).

  4. 4 Curious

    I’m the first person to congratulate amazon for an amazing service in both EC2 and S3, but every time I go to Quizical the load times are horrendous. Using Firebug, I’ve gone to Quizical several times over the last week and eyed the network load times. Consistently it appears that quizical.net itself loads in under 50ms (median ~20ms, makes since if it’s a LiteSpeed cache). But I’m also seeing that all your other assets that are hosted from quizical.s3.amazonaws.com load very, very slowly. I’m seeing times with a median of 500ms and maximums of 2secs. I know it’s not my end as I’m on a direct Fiber line from work and a cable modem from home and have seen the same results consistently.

    I’ve read the Dr Dobbs report that measured latency off S3 and their results completely contradict this empirical data, but I wonder if the growth of S3 users since that article was written has lowered S3’s performance. I’ve also found anecdotal accounts of folks getting terrible performance overseas from S3.

    I’m hoping other folks out there can contribute what they’ve seen in terms of load times for Quizical and other services that are using S3. Perhaps, I’m an isolated case, but I’m in Palo Alto, CA so you better hope all the VC’s here don’t have the same load times I’m getting.

  5. You are the second person, Curious, to comment on the slowness. The problem was not related to EC2. I think I have identified and fixed the slowness problem. I was serving my entire rails public directory from S3, as you identified. I was also fragment caching much of my site from S3 as well. As an experiment, I was trying to serve as much of quizical from S3 as I could. I’m still serving cache fragments from S3, but have moved the serving of the public directory back locally. That seems to have helped a great deal.

    Another problem that might have been causing slowness is that I was ran out of room in my main EC2 partition. Amazon gives you ~10GB in the main partition, with the rest of the 165GB in their “ephemeral storage” located on /mnt. I had my database stored in the sda1 partition and, along with rails apps and applications, quickly consumed all the 10GB. I’ve now moved my database to /mnt.

    Thanks for the feedback.

    Steve

  6. 6 Sean

    I’m interested in you decision to go with litespeed. Performance reasons?

  7. Hi Sean,

    That’s a good question. Why litespeed. I saw a posting one time from Zed Shaw, saying he recommends litespeed for those that are not experienced system admins. I think he was trying to save himself some support tickets for Mongrel. It has a good admin gui and quick commercial tech support. Performance is as good or better than other configurations, depending upon whose doing the testing. Plus, I saw that techno-weenie was using it (don’t know if he still is). All those things made it seem like a good fit for me.

  8. You have a typo:

    # ~/Documents/Projects/ec2/api/bin/ec2-add-key pair rails-server

    Should be:
    # ~/Documents/Projects/ec2/bin/ec2-add-key pair rails-server

  9. 9 James

    Ben

    I have just got my EC2 login!

    many thanks for publishing this, i am using a new mac (recent convert from WinXP/Redhat) and could not find my java path!

    James

  10. 10 Bobby

    Steve,

    Any chance you can show how you deploy to and serve up additional Rails apps using LiteSpeed server on EC2? I, too, am not a sysadmin and I would like to (easily) deploy my apps using Capistrano and LiteSpeed. I tried the SliceHost forums, but no luck so far.

    Bobby

  11. 11 Bobby

    I meant on a single EC2 instance, just to be clear. LiteSpeed seems to be easy to configure to handle multiple Rails apps.


  1. 1 Some EC2, Fedora, Rails, Mongrel, Memcached Links - Laughing Meme

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: