Introduction

I could not find an updated documentation on how to setup properly a rails application on Dreamhost, so I thought I’d share the information I gathered.. First off, thanks to the Phusion team, with Passenger (a.k.a. mod_rails), it’s now very convenient, easy, and cheap to run a rails application on Dreamhost.

Also, special thanks to Todd Huss for discovering the multi dreamhost user hack. Basically, you need to run each of your rails application on Dreamhost under a different shell user. Their process monitor, will start killing your processes when the total memory per user exceeds 200 MB of virtual memory (VSZ).

So here’s the guide, even perfect newbees and skript-kiddies should be able to follow :

The complete step by step

INFO : Let Capistrano create the directory structure on the server BEFORE you set the public dir of your application in the edit domain section of the Control Panel. This will avoid you conflicts when Capistrano attemps to create the symlinks.

  1. First, configure your new rails application using Dreamhost Control Panel :

    1. Create a new repository for your application (screenshot)
    2. Create a new shell / SSH user (read why here) with the same name as your application (screenshot)
    3. Create a new database (screenshot)

  2. Add your application to your repository

    1. Create your rails skeleton (if it’s done yet), on your local machine (assuming you use *nix, but should be similar on Windows)

       rails myrailsapp --freeze
      
    2. Add your application to the repository

      $ cd myrailsapp
      $ svn mkdir http://svn.myrailsappdomain/myrails/trunk
      $ svn checkout http://svn.myrailsappdomain/myrails/trunk ./
      $ svn add  *
      $ svn commit
      
    3. Exclude database configuration, temporary files, and any user upload directory from the source control :

      $ svn copy config/database.yml config/database.yml.example
      $ svn remove config/database.yml -f
      $ svn propset svn:ignore "database.yml" config/
      $ svn remove tmp/*
      $ svn propset svn:ignore "*" tmp/
      # If you need to share unversionned user-uploaded files, between deploys :
      $ mkdir public/uploads
      $ svn add public/uploads
      $ svn propset svn:ignore "*" public/uploads/
      $ svn remove log/*
      $ svn propset svn:ignore "*" log/
      $ svn remove db/*.sqlite3
      $ svn propset svn:ignore "*.sqlite3" db/
      $ svn commit
      
  3. Next, create the Capistrano recipe :

    1. Create recipe skeleton :

      $ cd /path/to/your/myrailsapp
      $ capify .
      [add] writing `./Capfile'
      [add] writing `./config/deploy.rb'
      [done] capified!
      
    2. Replace the content of config/deploy.rb with the following :

      #############################################################
      ## General
      #############################################################
      
      set :application, "myrailsapp"
      #############################################################
      ## Servers
      #############################################################
      set :use_sudo, false    # Dreamhost does not support sudo
      set :user, application  # Dreamhost SSH User
      set :domain, "myrailsapp.domain.com"
      
      server domain, :app, :web
      role :db, domain, :primary => true
      
      #############################################################
      ## Subversion
      #############################################################
      set :scm, :subversion
      set :scm_user, application   # Sets 'my_svn_user' instead, if you are using different name than your app.
      set :scm_auth_cache, true  # Prompts for password once
      set :scm_password, Proc.new { Capistrano::CLI.password_prompt("SCM password for #{scm_user}:") }
      set :repository,  "http://svn.myrailsappdomain/#{application}/trunk"
      set :deploy_to, "/home/#{user}/#{domain}"
      # keeps a local checkout of the repository on the server to get faster deployments
      set :deploy_via, :remote_cache
      
      #############################################################
      ## Tasks
      #############################################################
      
      namespace :deploy do
        desc "Restart Application (using tmp/restart.txt)"
        task :restart_passenger do
          run "touch #{current_path}/tmp/restart.txt"
        end
      
        desc "Restarts your application."
        task :restart do
          restart_passenger
        end
      
        desc "Link shared files"
        before :symlink do
          run "rm -drf #{release_path}/public/uploads"
          run "ln -s #{shared_path}/uploads #{release_path}/public/uploads"
          run "rm -f #{release_path}/config/database.yml"
          run "ln -s #{shared_path}/database.yml #{release_path}/config/database.yml"
        end
      end
      
  4. Then, prepare the directory structure on the server :

    1. Create the Capistrano structure (releases/, shared/), by typing this on your local machine :

      $ cap deploy:setup
      
    2. Login to your server to manually create your shared directories and files (if any)

      $ ssh myrailsapp.domain.com
      [tootsie]$ cd myrailsapp/shared
      
    3. Configure your database on the server (you don’t want this file in your repository) :

      # config/database.yml
      
      development:
        adapter: sqlite3
        database: db/development.sqlite3
        timeout: 5000
      
      test:
        adapter: sqlite3
        database: db/test.sqlite3
        timeout: 5000
      
      production:
        adapter: mysql
        encoding: utf8
        database: myrailsapp
        username: myrailsapp
        password: [your_db_password]
        host: mysql.[myrailsapp.domain].com
        timeout: 5000
      
    4. Create your upload directory (OPTIONAL) :

      [tootsie]$ mkdir uploads
      
  5. Deploy your application. Note : it will only prompt for your SVN password at the first deploy.

    $ cap deploy
    
  6. And finally the most important, configure your domain like this :

[UPDATE 2008/07/01]

If you need to install custom gems on your account, you can find more info here, here and here.

References

  1. http://labs.peritor.com/webistrano/wiki/ConfigurationParameter
  2. http://groups.google.ca/group/capistrano/browse_thread/thread/6ef7c7c212547eab (for scm_password)
  3. http://groups.google.com/group/capistrano/browse_thread/thread/ae7b16a16abf4e5d (for task hookups with namespaces)
  4. http://groups.google.com/group/capistrano/web/deploying-on-dreamhost
  5. http://gabrito.com/post/ruby-on-rails-dreamhost-plugin
  6. http://www.railsforum.com/viewtopic.php?id=8118
  7. Dreamhost Wiki : Capistrano
  8. Dreamhost Wiki : Ruby on Rails
  9. Capistrano Manual