Home > On the Ground > On the Rails: Bootstrap a Rackspace Cloud Server

On the Rails: Bootstrap a Rackspace Cloud Server

January 8th, 2010

I’m going to assume you already have a Rackspace Cloud account. (If you don’t, I’ll leave it to you to rectify the situation.)

Create a Server

Follow these steps to create a Rackspace Cloud Server:

  1. Log in to the control panel at manage.rackspacecloud.com.
  2. Click on Hosting in the left nav bar, then Cloud Servers.
  3. Click Add New Server.
  4. Now, it’s time to pick how much RAM and storage space you want (and how much you want to pay per hour). The default, 256 MB RAM and 10 GB storage at $0.015 an hour, is fine for our purposes.
  5. Scroll down and give your server a name, like, oh, I don’t know, “Fred” or “Rails-Test” or something.
  6. Next, choose your favorite flavor of ice cream operating system. I like the Ubuntu LTS (Long-Term Support) releases. Yum. No, wait, I mean apt-get. :-P
  7. Finally, click Add Cloud Server.

While it is building your server according to your specifications, the control panel will give you an IP address and a root password. This information will also be emailed to the primary contact email address associated with your account. Be sure to keep it in a safe place.

Once your server is ready, you can SSH into it. In a Terminal, issue the following command:

ssh root@<your_server_IP_address>

Enter your root password when prompted.

Bootstrap That Baby

Marc Chung (formerly of OpenRain, now of redPear) has published, as a GitHub repo, a collection of handy-dandy shell scripts to aid us in bootstrapping a Rails server. Go here and start to follow the instructions in the README. Update and upgrade Ubuntu, install Git, and clone the Git repo to get the scripts.

Set Up Password-less Authentication

Before we start running scripts, we ought to make it so that we can connect to our remote server from our local Mac without having to type in a password, while at the same time ensuring that no one besides us can connect in this way. (This is necessary for the deployment process to work, as we’ll see in our next episode.) We’ll achieve this by adding the public key we generated earlier on our Mac to an “authorized_keys” file on the remote server. (reference)

  1. On the Mac, run “cat ~/.ssh/id_rsa.pub” and copy the result to the clipboard.
  2. On the server, run “pico ~/bootstrap/config/root_authorized_keys”. Paste the key into this file and save it (Ctrl-O).
  3. On the server, run “pico ~/bootstrap/config/default_authorized_keys”. Paste the key into this file and save it (Ctrl-O).

Edit/Run Scripts

On the server, from the ~/bootstrap/slicehost directory, we are going to be running these scripts in order:

00-core.sh
05-ruby.sh
10-misc.sh
15-postgresql.sh
25-apache.sh
36-passenger.sh
40-rails-apps.sh

You can run the core, ruby, and misc scripts as written.

Before running the postgresql script, you should edit it to customize one of the config options. Namely, edit the “add addresses” block in that file. Change the IP addresses to match the machines from which you’ll be remotely connecting to your PostgreSQL database server, if any. If there are none, you can delete this block.

When running the postgresql script, you have to provide it a password for your database’s admin user. We’ll set a temporary environment variable for this, at the same time we run the script, as shown in Marc’s instructions:

PGPASSWORD='clark_kent_is_superman' sh ./15-postgresql.sh

Be sure to replace the value given for the password with your own value.

We’ll then run a separate block of script, similar to what’s shown in Marc’s instructions, to create a normal user for our database, and then the database itself:

cat > /tmp/postgres.sql <<EOF
CREATE ROLE railstest
  LOGIN ENCRYPTED PASSWORD 'batman_is_bruce_wayne'
  INHERIT CREATEDB;
CREATE DATABASE railstest
  OWNER = railstest;
EOF
sudo su postgres -c psql template1 < /tmp/postgres.sql
rm /tmp/postgres.sql

In the above block of script, remember to replace the values given with your own values for:

  1. your normal database user’s username (in our case, railstest)
  2. your normal database user’s password (we’ll have to add this to our app in railstest/config/database.yml too)
  3. the name of the database you want to create for your app (in our case, railstest_production)

The apache script can be run as written.

The passenger script requires that one environment variable be set: the public hostname of your server (example: rails.example.com):

SERVERNAME=rails.example.com sh ./36-passenger.sh

After running the passenger script, let’s edit one of our Apache config files:

pico /etc/apache2/sites-available/default

Along with the addition and modification of a couple other options, we’re going to change the DocumentRoot to the directory where our app is going to live. Edit the VirtualHost block of the file to look like this:

<VirtualHost *>
    ServerAdmin youremail@yourdomain.com
    ServerName rails.example.com
    DocumentRoot /var/local/railstest/current/public
    RailsBaseURI /
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/local/railstest/current/public>
        Options Indexes FollowSymLinks -MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>
...

Save the file with Ctrl-O. Exit with Ctrl-X, and then run:

apache2ctl -k graceful

to restart Apache.

Finally, you can run the rails-apps script as written.

Post-Scripts

After we’ve finished running the scripts, we want to make it so that the server can connect to GitHub and access our app’s code repo during the deployment process.

Let’s generate an SSH keypair on the remote server, but as the “app” user instead of root, since that’s the one that we’ll be running the deployment process as:

sudo su app -c "ssh-keygen -t rsa"

As we did earlier with the keys generated on our Mac, we’ll paste the public key into our GitHub config:

  1. Run “cat /home/app/.ssh/id_rsa.pub” on the server and copy the result to the clipboard.
  2. On GitHub, go to your repo’s home page and click the “Admin” button.
  3. Under the “Deploy Keys” section, click “Add another deploy key”.
  4. Give the deploy key a title, like “Production App Server” or something.
  5. Paste the contents of the clipboard into the “Key” textarea.
  6. Click “Add Key”.

We’re done bootstrapping! Our Rackspace Cloud Server is now prepped and ready to run our app upon its arrival. Be sure to type “exit” to log out and close your SSH connection.

One More Thing

Before we call it quits for this episode, let’s go back to our Mac for a second and make sure we add the normal database user’s password to our config file. In Terminal, from our home directory, do the following:

cd workspace/railstest
pico config/database.yml

Go down to the empty “password:” line in the “production” section at the end of the file and add the password you specified above in the separate postgres.sql script (the one which created the normal user and the database). Type Ctrl-O to save the file.

Next Time

In our next episode, we’ll install Capistrano and deploy our app to this baby! See you then!

Categories: On the Ground Tags:
Comments are closed.