Rails, Capistrano, Dreamhost

RIMA – the Ruby Imap Mailbox Archiver

If you need a little script for cleanup your Inbox, check RIMA

Rails, Capistrano, Dreamhost All-In-One Guide

Hi all, I wrote this post in english because I think a lot of people will look at it.
Dreamhost is a very good web hosting company, that support Ruby On Rails application through Apache FastCGI module.
I choosed Dreamhost because the good reviews I see everywere, and I’m pretty happy to have it done. They offer a LOT of disk space, terabytes of monthly bandwidth, unlimited databases and unlimited subdomains, ssh login, svn repository, all that you will need. Truly!
So I wanted to deploy two Rails application I’m working on, in two subdomains, using the “automagic” Capistrano way of deploying.
When I setup Capistrano for the first time, it was version 1.X and I found all the info I need on Dreamhost Wiki and Rails manual, and it worked like a charm.
But the new application I wrote has been “capified” with Capistrano 2.0 (current release) and some little difference in configuration has getting me mad… Because of this I’m writing this little document, for helping other people and even for helping my memory 😀

SSH login without password

So let’s start with ssh for login without password, using public/private RSA keys. (I intend this instructions focused for linux/unix/mac osx users)
First of all we will generate rsa keypair. open a terminal and

[user@nixbox] $ ssh-keygen -t rsa
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/utente/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
90:98:4f:f5:69:39:57:5d:46:83:a4:a2:d6:63:3c:25 user@nixbox

(Please leave standard path and empty passphrase. Just press enter.)
now it’s time to copy the id_rsa.pub key on your dreamhost server (the one you know).

cat .ssh/id_rsa.pub | ssh YOURUSERNAME@YOURDREAMHOSTSERVER 
   'cat >> .ssh/authorized_keys'

if the username on your local machine is different of which on the dreamhost server, you can edit .ssh/config with this lines

Host ALIAS
Hostname YOURDREAMHOSTSERVER
User YOURUSERNAME

now you can login into YOURDREAMHOSTSERVER with only typing

ssh ALIAS

Pretty cool, isn’t it?

SubDomains

Ok, now we have to setup a subdomain for svn and one for the rails application.

SVN

go to the dreamhost controlpanel => domains => Add New Domain / Sub-Domain
For the svn server name choose something like svn.YOURDOMAIN.com, extra web security => YES, FastCGI Support => NO, How do you like the www in your URL? => remove www.
Save configuration.

RAILS

go to the dreamhost controlpanel => domains => Add New Domain / Sub-Domain
for the server name choose something like APPNAME.YOURDOMAIN.COM, extra web security => YES, FastCGI Support => YES, How do you like the www in your URL? => remove www.
Save configuration.

MySQL

we need a mysql database too…
go to the dreamhost controlpanel => goodies => manage MySql
add a new hostname like mysql.YOURDOMAIN.COM
then create a new MySql database with name myappdb, choose username and password => save.
depending on dns, the propagation of the domains name we added may take some time (even 12/24 hours…).

Rails

now we create a local rails application

[user@nixbox] $ rails MyApp
create...


let’s create a database into the local machine

[user@nixbox] $ mysqladmin -u LOCALDBUSER -p create myappdb_dev

then cd into MyApp and edit config/database.yml

development:
  adapter: mysql
  database: myappdb_dev
  username: LOCALDBUSER
  password: YOURPASSWORD
  host: localhost

production:
  adapter: mysql
  database: myappdb
  username: USERNAME
  password: PASSWORD
  host: mysql.YOURDOMAIN.COM

ok. you can test the application by run script/server and pointing the browser to http://localhost:3000

SVN repository

let’s setup SVN now. Go to the dreamhost controlpanel => goodies => Subversion
Choose a project name and a project id. The project id is important because it is part of the url where svn publish your code.
add at least a SVNUSER with a SVNPASSWORD and choose private project.
on the localmachine

[user@nixbox] $ cd MyApp
[user@nixbox] $ svn import -m "New import" 
   http://svn.YOURDOMAIN.COM/PROJECTID
Adding         ...
Transmitting file data .........
Committed revision 1.

Capistrano

It’s Capistrano time!
you have to install Capistrano if you don’t have it installed with

[user@nixbox] $ sudo gem install capistrano

Now we have to “capify” the application, meaning that we will add some configuration file

[user@nixbox] $ cd MyApp
[user@nixbox] $ capify .

Configuring of capistrano is a little tricky…
let’s start with editing config/environments.rb with decommenting

ENV['RAILS_ENV'] ||= 'production'

then edit public/.htaccess by changing (note fcgi instead of cgi!)

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

To:

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

let’s edit config/routes.rb by adding at the end of file

map.connect '', :controller => 'YOUR_PRIMARY_APP_CONTROLLER'

the very tricky part: edit config/deploy.rb

set :application, "YOURAPPNAME"
# this is the path that capistrano understand, 
# even if it is "nonsense"
set :repository,  "svn+ssh://svn.YOURDOMAIN.COM
   /home/YOURUSERNAME/svn/PROJECT_ID" 
role :app, "APPNAME.YOURDOMAIN.COM"
role :web, "APPNAME.YOURDOMAIN.COM"
role :db,  "APPNAME.YOURDOMAIN.COM", :primary => true

set :deploy_to, "/home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM/"
set :use_sudo, false
set :checkout, "export"

set :user, "SVNUSER"

ssh_options[:keys] = %w(/home/YOURLOCALMACHINEUSER/.ssh/id_rsa)

desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
 # fix permissions
 run "chmod +x /home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM
    /current/script/process/reaper"
 run "chmod +x /home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM
    /current/script/process/spawner"
 run "chmod 755 /home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM
    /current/public/dispatch.*"
end

desc "Restarting after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
 run "touch /home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM
    /current/public/dispatch.fcgi"
end

desc "Restarting after rollback"
task :after_rollback, :roles => [:app, :db, :web] do
 run "touch /home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM
    /current/public/dispatch.fcgi"
end

last few things…

we want to remove index.html because it’s the standard rails web page

[user@nixbox] $ svn remove public/index.html
[user@nixbox] $ svn commit -m "remove index.html"

now first deploy with Capistrano

[user@nixbox] $ cap deploy:cold

with this command you will commit the code to svn repository, then Capistrano will checkout the last revision in a timestamped folder under
/home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM/releases/
then link this folder to
/home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM/current/
and copy a file called manteinance.html to
/home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM/shared/system/

another trick now:
go to the dreamhost controlpanel => domains => edit APPNAME domain
edit Specify your web directory:/home/username/ by appending at the end

/current/public/

then save. Without this there is no way to made things work =)

Deploy

[user@nixbox] $ cap deploy:web:enable

this command mainly does two things: first it fire up the dispatcher public/dispatch.fcgi simply by “touch” (in the unix meaning) the file, then it remove the file maintenance.html under
/home/YOURUSERNAME/APPNAME.YOURDOMAIN.COM/current/shared/system
when this file is in place, the rewrite rule on .htaccess made this be the index page, and by removing it, the rewrite rule will redirect the request to the dispatcher.

Open your browser to http://APPNAME.YOURDOMAIN.COM and enjoy!!!

Other actions

from now, just start coding! this three actions will be the one you will use to deploy/undeploy the application

update the code

[user@nixbox] $ cap deploy:update

to stop the application

[user@nixbox] $ cap deploy:web:disable

to rollback to last version

[user@nixbox] $ cap deploy:rollback

Dreamhost 50% DISCOUNT

If you are considering buying a dreamhost account, you have chance to get a 50% price discount on a year subscription by signup on dreamhost by entering this
DREAMHOST PROMO CODE: DVWV50

dreamhost web hosting

3 thoughts on “Rails, Capistrano, Dreamhost

  1. Luis Merino ha detto:

    Great guide, thank you, though I’ve got a problem: when I exec the command

    cat .ssh/id_rsa.pub | ssh YOURUSERNAME@YOURDREAMHOSTSERVER
    ‘cat >> .ssh/authorized_keys’

    inside my DH server I don’t have the folder .ssh nor do I have the authorized_keys file, shall I create those manually to avoid the error shell is throwing out?

    thanks in advance.

  2. dave ha detto:

    Hi Luis
    if the directory .ssh does not exist, maybe can be because it is the first time that you log in.
    after the first log in via ssh, the directory will be created automatically.
    sure id_rsa.pub file will not be there
    bye DV

  3. Adrianna ha detto:

    This ensures there is a sufficient amount of time for them to complete the installation. Consequently, many landlords are critical of those
    laws, although they must be abided by. Now, at last you have
    found it and can spend your very first night there, soaking it
    all up.

I commenti sono chiusi.