Deploying Rails 2.3.8 on WebFaction with Nginx, Passenger, Git and Capistrano

Sunday 18 July 2010 1 comment

Create a Rails 2.3.8 app in your WebFaction control panel:

Create your WebFaction account and a new Rails 2.3.8 app. If you require a MySQL or Postgres database, you can add it through the control panel, then update your app’s config/database.yml file with the production database details.

Get Git Going

  • Go to the Git site and copy a link to the latest release, then log in to your WebFaction account using SSH and get it (adjust your actions according to the release): wget http://kernel.org/pub/software/scm/git/git-1.7.1.1.tar.bz2
  • Untar Git: tar -xvjf git-1.7.1.1.tar.bz2
  • Change into the Git directory: cd git-1.7.1.1
  • Configure it: ./configure --prefix=$HOME
  • Make it and install it: make && make install
  • Now test to make sure everything worked by getting the Git version: git version
  • Now go to your home folder, and create a place for your repository on the server:
    cd
    mkdir git
  • Everything going well so far? Great… next step

Copy Up Your App

  • Start by creating a zip archive of your app on your local machine.
  • Copy your app up to WebFaction: scp /path/to/myapp.zip username@domain:~/git/
  • Go back to your WebFaction account and unzip the app
    cd git
    unzip myapp.zip
    cd myapp
    

Create The Git Repo

  • Create a .gitignore file in your app root to make sure you don’t have any unwanted junk in there, mine looks like this:
    log/*.log
    tmp/**/*
    .DS_Store
    doc/api
    doc/app
    coverage
    db/*.sqlite3
    public/system/**/*
    public/javascripts/cache/**/*
    public/stylesheets/cache/**/*
  • Now initialize a new Git repository:
    git init
    git add .
    git commit -a -m "Initial commit."
  • I usually go into the Git repo and modify the .git/config file to avoid hassles:
    [core]
            repositoryformatversion = 0
            filemode = true
            bare = true
            logallrefupdates = true
    

Bash Profile

Edit your .bashrc and .bash_profile located at /home/me/.bashrc and .bash_profile:

  • .bashrc:

    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    ME=/home/#{me}/webapps/#{my_app}/bin/
    RAKE=/home/#{me}/webapps/#{my_app}/bin/rake
    GIT=/home/#{me}/git-1.7.1.1/
    PATH=$ME:$RAKE:$PATH:$HOME/bin:$GIT
    export PATH
    export GEM_PATH=/home/#{me}/webapps/#{my_app}/gems/bin 
    export GEM_HOME=/home/#{me}/webapps/#{my_app}/gems 
    
  • .bash_profile

    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    ME=/home/#{me}/webapps/#{my_app}/bin/
    GIT=/home/#{me}/git-1.7.1.1/
    RAKE=/home/#{me}/webapps/#{my_app}/bin/rake
    PATH=$ME:$RAKE:$PATH:$HOME/bin:$GIT
    export PATH
    
    export GEM_PATH=/home/#{me}/webapps/#{my_app}/gems/bin 
    export GEM_HOME=/home/#{me}/webapps/#{my_app}/gems 
    

Update your nginx conf file, located at /home/me/webapps/myapp/nginx/conf/nginx.conf:

  • First Update the app root:
    root /home/#{me}/webapps/#{my_app}/#{myapp}/current/public;
  • Then you may want to add some Passenger settings to help keep your app alive and avoid long startup times:
    rails_appspawner_idletime 0;
    rails_frameworkspawner_idletime 0;
    passenger_poolidle_time 1000;
    

Clone and Deploy The Application

  • Back on your local machine’s command line, clone your WebFaction Git repo, and Capify it:
    git clone me@domain:/home/me/git/myapp myapp
    cd /path/to/myapp
    capify .
  • Create a Capistrano deploy recipe:
  • Commit your Capistrano recipe:
    git add .
    git commit -a -m "Capified."
    git push
    
  • Deploy the app:
    cap deploy:setup
    cap deploy:update
    

Everything should be working, but don’t forget to install any required gems.

After you’ve deployed once, in future you can run cap deploy, and/or cap deploy:migrations when you’ve added migration files.

Comments

1 Comment Subscribe via RSS

  • Avatar

    anrake Saturday, August 14, 2010, 02:12 AM

    Thanks! very helpful! You should post this recipe to the webfaction forums.