Setting up Ubuntu for Rails 3 on Passenger & nginx From Scratch

I had the itch to set up a continuous integration server but didn’t want to set up a physical box for it because I was too lazy, and also because I don’t have such hard lying around. I guess it was more of the latter.

With that, I did a rigorous 10-minute review of hosting providers and settled on Rackspace because I didn’t need a lot of power for this thing and Rackspace was pretty much the only cloud hosting provider that allows you to provision a really small slice for about $10 a month.

About an hour later, I was set up with a clean Ubuntu 10.10 install. True to the title of this post, this is where we start.

Step 1: Don’t log in as root

So we should all pretty much know why logging in as root is not a good idea, so let’s just get this out of the way.

adduser app_user

The adduser command creates a user account with some defaults. Next, edit your /etc/sudoers file by adding this line:

$ app_user ALL=(ALL) ALL

Disconnect from the server and reconnect back as your new account. Now, change your SSH settings to prevent users from logging in as root

$ vi /etc/ssh/sshd_config

Search for theline where PermitRootLogin is commented out or set to yes and change it to:

PermitRootLogin no

For the changes to take effect, simply do this:

$ /etc/init.d/ssh restart

No rocket science here.

Step 2: Set up the system for Ruby with RVM

RVM is pretty neat and you ought to just install it just because. But first, we’re going to have to update the system itself; get the latest packages by performing these three commands:

$ apt-get update
$ apt-get install build-essential
$ apt-get install ruby ri rdoc irb ruby-dev libruby libreadline-ruby libopenssl-ruby libssl-dev libreadline5-dev git-core zlib1g zlib1g-dev libcurl4-openssl-dev libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev libxml2 libxml2-dev libxslt1-dev sqlite3 libsqlite3-dev libpcre3 libpcre3-dev

Depending on your needs, some of these packages could be omitted, but I tried my best not to add anything that wasn’t really necessary when compiling this list.

We’re going to use the official installation instructions because it just works.

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
$ source .bash_profile

This should set up your system with RVM, which makes us ready to install the rubies we want. I’m going to be installing Ruby Enterprise Edition, which is also what I use when I deploy apps to Heroku.

$ rvm get latest
$ rvm reload

$ rvm install ree-1.8.7
$ rvm use ree-1.8.7

Running these commands will probably take some time, so check back in a minute. If you want to use REE as the default ruby, you can do this:

rvm --default use ree-1.8.7

Step 3: Installing Passenger & nginx

Now we come to the really fun part. Okay, I’m kidding, setting up server software is not really that fun (bad experience for me). Lucky for us, we’re gonna be using nginx, which isn’t that bad.

Installing such software usually requires super powers, the kind that you get with sudo. However, due to the way RVM works, we cannot use sudo directly with our Ruby commands. Fortunately, RVM provides us with an alternative command:

$ rvmsudo gem install passenger
$ rvmsudo passenger-install-nginx-module

Be sure to select option 1 to let Passenger and install and configure nginx for you. The config file can be found in /opt/nginx/confg/nginx.conf

To make it easier to start/stop nginx, we will be creating an init script (courtesy of Hackido) like so:

$ cd /etc/init.d/
$ sudo wget http://vince71.googlepages.com/nginx
$ sudo chmod +x /etc/init.d/nginx

Once you are done, you should be able to perform these commands on nginx:

$ sudo /etc/init.d/nginx start
$ sudo /etc/init.d/nginx stop
$ sudo /etc/init.d/nginx restart

or

$ sudo service nginx start
$ sudo service nginx stop
$ sudo service nginx restart

Remember to use sudo; without it, the script runs as if it were successful, but does not start/stop the server at all.

Lastly, we will want nginx to start each time the server boots and that’s what the init script is going to help us with. All you have to do is run this:

sudo /usr/sbin/update-rc.d -f nginx defaults

I’m not exactly sure why, but my installation of nginx did not come with the sites-available and sites-enabled folders. With these two folders, it becomes easy to set up multiple configuration files for your different sites. Simply put the config files in the sites-available folder and symlink to it from sites-enabled to enable the site. And of course, remove the symlink to disable the site.

$ cd /opt/nginx
$ mkdir sites-available sites-enabled

To include the configuration files in the sites-enabled folder, add this line to your config file:

include /opt/nginx/sites-enabled/*;

Creating the site-specific config files are a straight forward matter and I will go into that in a later blog post when I will be setting up CI Joe.

  1. jaryl posted this