How I Set Up My EC2 Instance for Rails & Litespeed
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
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:
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-----
-----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.
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.
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:
It will tell you whether it is still pending or return the URL when it is ready. The URL will look something like:
Now, just like a regular remote linux box we can log in with:
ssh -i /Documents/Projects/ec2/auth/id_rsa-rails-server firstname.lastname@example.org
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:
Add you as a user:
useradd -g www 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
yum install subversion
rpm -i ec2-ami-tools.noarch.rpm
tar zxvf rubygems-0.9.0.tgz
sudo ruby setup.rb
Let’s clean up a little
rm ruby* -drf
*change to user steveodom*
sudo gem install rails
Install the lsapi gem needed for litespeed:
sudo gem install ruby-lsapi
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).
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;
..And allow him to connect remotely:
grant all privileges on *.* to email@example.com IDENTIFIED BY 'xxxxxxx';
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):
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.
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
- 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).
Filed under: EC2, rails, Ruby on Rails | 12 Comments