Deploying A Rails App To Rackspace Cloud Servers On Ubuntu Using Nginx and Unicorn

by dontangg on April 4, 2012

Configure the server

Get a server with Ubuntu

With Rackspace Cloud Servers, this is very easy. Login to Rackspace, choose Cloud Servers under Hosting, click Add Server, and choose your OS. I chose Ubuntu 11.10 (Oneiric Ocelot). In just a minute, you’ll have an IP address and root credentials that you can use for ssh.

Install Stuff

I did everything using ssh. If you’ve never done this, Google it and learn the best way to do it on your platform.

When you first get your server, it is pretty bare. Let’s get some stuff installed. First, you’ll want to update apt-get so that it installs the latest versions of everything:

$ apt-get update

Then, upgrade all the currently installed packages (-y means say yes to all prompts):

$ apt-get upgrade -y

Now, we’ll install new stuff:

$ apt-get install git-core, build-essentials, curl, zlib1g-dev, \
    libxml2-dev, libxslt1-dev, openssl, nodejs, postgresql, libpq-dev, \
    nginx

I installed nodejs, so that I’d have a javascript runtime to compile my static assets. The postgresql packages is to run a PostgreSQL server on the machine. The ligpq-dev is so that my pg ruby gem can connect to the PostgreSQL server. The git-core package is so that I can run a git pull command to update the code on the server. Everything else is a pretty fundamental need.

Since I used rbenv and ruby-build for ruby, I didn’t install any ruby package here. Instead, I just made sure that all ruby’s dependencies were installed.

$ apt-get build-dep ruby1.9.3

Create a new user

Now, we need to create a user for our app to run as. I called mine app_user.

$ useradd -m -g staff app_user

The -m option will create the new user’s home directory (/home/app_user). The -g option tells it which group to add the user to (staff).

Set your new user’s password.

$ passwd app_user

To allow app_user to execute commands with super-user privileges, you need to add him to the sudoers file located at /etc/sudoers.

# /etc/sudoers
app_user ALL=(ALL) ALL

Now you’re set to log out as root and log back in as app_user. If I specify any commands that it says you don’t have rights to access, just put sudo in front of the command, enter app_user’s password and it will let you.

Ruby

I really like rbenv. These steps are mostly copied from its homepage, but slightly simplified for my use case.

Clone rbenv into ~/.rbenv.

$ cd
$ git clone git://github.com/sstephenson/rbenv.git .rbenv

Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility.

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

Add rbenv init to your shell to enable shims and autocompletion.

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Restart your shell so the path changes take effect. You can now begin using rbenv.

$ exec $SHELL

Now, we’ll install ruby-build that makes it really simple to install ruby.

$ mkdir -p ~/.rbenv/plugins
$ cd ~/.rbenv/plugins
$ git clone git://github.com/sstephenson/ruby-build.git

$ rbenv install 1.9.3-p125

Set the global version of ruby to 1.9.3.

$ rbenv global 1.9.3-p125

Rebuild the shim binaries. You should do this any time you install a new Ruby binary (for example, when installing a new Ruby version, or when installing a gem that provides a binary).

$ rbenv rehash

We’re done installing ruby. Let’s add some reasonable defaults for installing ruby gems.

# ~/.gemrc
---
:update_sources: true
:verbose: true
:buld_threshold: 1000
:backtrace: false
:benchmark: false
gem: --no-ri --no-rdoc

Now let’s just install a couple ruby gems that will allow us to use capistrano later.

$ gem install rake
$ gem install bundler
$ rbenv rehash

Page: 1 2 3 4 5 6

9 comments

Cool post!

A few comments/thoughts:

1. I still like RVM, and am used to it and see no reason to switch to rbenv. That said, I had trouble (and am still having trouble) with Ubuntu + OpenSSL + Ruby 1.9.2 + RVM. I’m not sure which aspect is to blame here, but all I know is I built this same server on Gentoo a year ago and didn’t have these OpenSSL issues.
2. I like thin over unicorn, as it allows for asynchronous IO. Although, to be fair, I’m not utilizing it much yet, so at this point I don’ think it matters too much what server I use for my Rails app.

I have been working hard on getting this dang Ubuntu box up and running. I think part of my frustration has actually stemmed from selecting the smallest (256mb) size server. It might be an interesting idea to choose a bigger instance at first, then after you’re done installing all the prerequisites, re-size it to a smaller instance and then save an image. I think that’s what I’ll do next time.

I also thing I’ll go back to Gentoo next time, or even (God help me), CentOS.

by Steve on May 7, 2012 at 1:34 am. Reply #

Thanks, I’m about to deploy to rackspace and plan to follow your instructions.

Does rackspace give you information regarding how much memory your instance is using?

by stephen murdoch on June 20, 2012 at 4:19 pm. Reply #

Yes, Rackspace tells you how much space you’re using. I haven’t look hard in the website to find it, but I know you can see it when you ssh into your server. As part of the welcome message, it gives you a bunch of stats like, number of running processes, hard disk usage, ram usage, etc.

I watched Ryan Bates’ Railscast after I posted this and found it very beneficial. I highly recommend it. You can find it here: http://railscasts.com/episodes/335-deploying-to-a-vps.

by dontangg on June 20, 2012 at 4:42 pm. Reply #

Thanks for the post, i absolutely will try this configuration later, while still on development.
Now i’m also on rackspace’s 256mb Debian 6, RVM + passenger + nginx + mysql.

by Adi Suryadi on July 26, 2012 at 5:59 pm. Reply #

I have a just a few questions. If I set my nginx, and unicorn files to the Rackspace provided IP instead of port 80 I can load the static pages from my rails app If I change it to port 80 and try to go to the rackspace provided IP is just see bad gateway message from nginx, would you be able to give me some guidance?

by nofx1717Jesse on September 19, 2012 at 10:56 am. Reply #

Sorry for the late reply. I just got back from a trip to Disneyland with my family. I don’t consider myself an expert in server setup/maintenance. At this point it would take me a while to remember everything. Part of the reason for this post was for me to be able to remind myself what I did. I don’t think that I can be much help with the time I have. I definitely recommend the Railscasts that I mentioned in the post on setting up a VPS. He does a great job of explaining the details. IMO, the one video itself is well worth the $9 it costs for a membership for a month.

by dontangg on September 28, 2012 at 10:33 pm. Reply #

Awesome post! What version of ubuntu are you running on RackSpace? Is it a 1st gen or next gen cloud server?

by James Stone on November 24, 2012 at 1:19 am. Reply #

I’m glad you liked it! It is a 1st gen cloud server. They hadn’t announced the next gen ones when I started. I’m running Ubuntu 11.10.

by dontangg on November 24, 2012 at 1:29 am. Reply #

Terrific post. Thank you! Got it up and running without issue.

by Bijan on February 19, 2013 at 6:12 pm. Reply #

Leave your comment

Required.

Required. Not published.

If you have one.