Easier Ruby on Rails Deployment with Slicehost

Friday, June 22nd, 2007

Server

Rails has made it incredibly easy, fast and enjoyable to build web apps but when it comes to deploying them to the net it can be a little tricky. Being such a new framework the methods of running rails apps in production are constantly changing and improving but it’s still nowhere near as simple as PHP deployment.

Having said that I’d much prefer to spend a little extra time in deploying a rails app that a few extra months in developing a php app!

So what is the best way to go about doing it? At the moment mongrel your best bet, using apache as a front end, but this can be a little confusing and difficult to set up. Enter Capistrano, a way of automating deployment of web applications, especially rails!

So here is a guide to setting up your Rails production server, I’m basing my guide on Slicehost VPS using Ubuntu, mainly because that is what I use and from what I’ve been hearing from other rails developers it’s their favourite for the price as well (only £10 per month) and you get full root access as if you had your own dedicated machine.

I’m also doing this from a mac so windows users might have to add “ruby” in before some commands and I suggest using putty for SSH access.

So lets get started!

  1. First up once you have your Slice ordered and ready from slicehost you’ll need to do some setting up via SSH, so on your local machine in the terminal put:

    ssh root@your.slice.ip

  2. Add a new user - since you dont want to be running everything as root you need to create a new user:

    adduser --ingroup users andrew

  3. Give your new user some permissions - So that you can still do everything you need lets give the new user some power:

    visudo

    Add this line to the bottom of the file:

    andrew ALL=(ALL) ALL

    Save and exit.

  4. Add some more sources of software for apt-get - so that we can install everything we need later using apt-get:

    sudo nano /etc/apt/sources.list

    And uncomment (remove the #’s ) these lines from the file:

    # deb http://us.archive.ubuntu.com/ubuntu/ dapper universe
    # deb-src http://us.archive.ubuntu.com/ubuntu/ dapper universe

    # deb http://security.ubuntu.com/ubuntu dapper-security universe
    # deb-src http://security.ubuntu.com/ubuntu dapper-security universe

    Then save and exit and we then need to update apt-get:

    sudo apt-get update && sudo apt-get upgrade

So that should be all you need to do over SSH on your slice or server, everything else can be done from your local machine using Capistrano and Deprec gems.

Repeat: The rest of the steps are done on your LOCAL MACHINE

  1. So first up make sure you have both capistrano and deprec installed on your machine (along with the rest of your rails development environment, if you’ve not done that yet go here: Teabass.com/super-simple-ruby-on-rails-with-macports/

    sudo gem install capistrano deprec --include-dependencies

  2. Next you’ll need a rails application and since so it is so easy we might as well just make one now:

    rails myapp

    cd myapp

  3. Now get deprec to work some magic and set itself up within you application:

    deprec --apply-to .

  4. You’ll now need to go into the config folder in your rails app and edit deploy.rb and database.yml with your server, user details and database settings.
  5. Once that is done you can run the awesome command:

    cap install_rails_stack

    This will take quite a while as it installs every piece of software you need to run a standard production server via ssh on your Slicehost VPS

  6. And now to finalise the setup:

    cap deprec_setup

    Note: the database password should be blank at this point, you can secure it later

Congrats, your done! Hopefully that was fairly painless, you’ve set up a full rails stack and the server is just waiting for your application, the command to deploy is:

cap deploy_with_migrations

And then you might find it helpful to restart apache after this as well if its the first time you’ve deployed you app:

cap restart_apache

If you’re going to get an account with slicehost it would be really cool if you used my refferal link so I can
save some on my hosting bill:

https://manage.slicehost.com/customers/signup?referrer=347966070

Thanks!

5 Comments so far »

  1. Thanks for that tutorial, keep on posting stuff like that. I just love rails and I might sign up with slicehost.

    Patrick on June 24, 2007 7:34 am

  2. At step 4, which lines do you uncomment and which do you add?

    ryan on August 4, 2007 3:07 pm

  3. You just need to uncomment (remove the #) of the four likes shown:

    # deb http://us.archive.ubuntu.com/ubuntu/ dapper universe
    # deb-src http://us.archive.ubuntu.com/ubuntu/ dapper universe

    # deb http://security.ubuntu.com/ubuntu dapper-security universe
    # deb-src http://security.ubuntu.com/ubuntu dapper-security universe

    Andrew on August 5, 2007 10:48 am

  4. Sorry, I was referring to step 4, “Deploy.rb”.

    There are many lines in the deploy.rb file that deal with the apache conf and such. What is the best practice there? (or do you know of a site or tutorial that deals with it?) I have been looking through the Capistrano website without finding what I’m looking for.

    ryan on August 6, 2007 1:46 am

  5. I found that the only thing I needed to uncomment was:

    set :apache_proxy_port, 9000

    which was so that I could run multiple rails app from one server, if your not doing that you should not need to even do that.

    Andrew on August 6, 2007 1:28 pm


Leave a Comment