Passenger + Capistrano + Nginx + Rails 2.3.4 + Ubuntu 9.04 (10 steps)

  1. Install capistrano gem. if you use bundler you need to add just one line to Gemfile

    gem "capistrano"

    and then run
    gem bundle
  2. after capistrano installed you need to make Capfile and default deploy.rb, run such from the root of your application
    capify .
  3. create user for deployments in remote machine (let’s say ‘deploy’ user)
    adduser deploy
  4. add sudo privileges to this user. add one line in your /etc/sudoers file
    deploy ALL=(ALL) NOPASSWD: ALL
  5. adjust deploy.rb for you. I used example from this link

    # Application
    set :application, "test"
    set :deploy_to, "/var/www/#{application}"
    # Settings
    default_run_options[:pty] = true
    set :use_sudo, true
    # Servers
    set :user, "deploy"
    set :domain, ""
    server domain, :app, :web
    role :db, domain, :primary => true
    # Subversion
    set :repository, ""
    set :svn_username, "rustam"
    set :svn_password, "password"
    set :checkout, "export"
    # Passenger
    namespace :passenger do
    desc "Restart Application"
    task :restart do
    run "touch #{current_path}/tmp/restart.txt"
    after :deploy, "passenger:restart"

    to understand how it works – read this link
  6. if you don’t use bundler, skip it. Otherwise, it will be usefull to add such piece of code to your deploy.rb file (from Richie’s post)

    # bundler
    namespace :bundler do
    task :install do
    run("gem install bundler --source=")
    task :symlink_vendor do
    shared_gems = File.join(shared_path, 'vendor/bundler_gems')
    release_gems = "#{release_path}/vendor/bundler_gems/"
    %w(cache gems specifications).each do |sub_dir|
    shared_sub_dir = File.join(shared_gems, sub_dir)
    run("mkdir -p #{shared_sub_dir} && mkdir -p #{release_gems} && ln -s #{shared_sub_dir} #{release_gems}/#{sub_dir}")
    task :bundle_new_release do
    rails_env = variables[:rails_env] || 'production'
    run("cd #{release_path} && gem bundle --only #{rails_env}")
    after 'deploy:update_code', 'bundler:bundle_new_release'

    it will bundle all production gems to your bundler_gems path
  7. ask Capistrano to create set of folders for deployments in the remote machine
    cap deploy:setup
  8. ask capistrano to check all remote folders:
    cap deploy:check
    it will return to you all problems that you have with both locale/remote machines – structure of folders/permissions etc. you can get something like this:

    The following dependencies failed. Please check them and try again:
    --> You do not have permissions to write to `/var/www'. (
    --> You do not have permissions to write to `/var/www/releases'. (

    When you’ll fix everything, you’ll get such:
    You appear to have all necessary dependencies installed
    it means that you’re finished with capistrano configs
  9. you need to install passenger and nginx module for passenger in your remote machine according to this

    sudo gem install passenger
    sudo passenger-install-nginx-module

    the second command will tell you how to configure Nginx and to deploy rails application.
    it will create Nginx configuration file for you:
    you need to change server block there

    server {
    listen 80;
    root /var/www/current/public; # <--- notice the 'public' part
    passenger_enabled on; # <--- don't forget this!li

    everything is finished for passenger and nginx!
  10. you can start deploy you app!
    cap deploy:update – copies your project to remote machine and updates symlinks
    cap passenger-restart – will start application
  11. FYI sudo /opt/nginx/sbin/nginx starts nginx in the remote machine

  12. Extra step: if you’re using bundler and used Yehuda’s link to run it(like me :)), you’ll have a problem with preinitializer. I used Tom Ward’s solution to fix it
    i.e. put only such line to preinitializer.rb
    require File.expand_path(File.join(File.dirname(__FILE__), "..", "gems", "environment"))
    and add such piece of code just after loading boot

    # Hijack rails initializer to load the bundler gem
    #environment before loading the rails environment.
    Rails::Initializer.module_eval do
    alias load_environment_without_bundler load_environment
    def load_environment
    Bundler.require_env configuration.environment

Links with detailed description:


4 Responses to “Passenger + Capistrano + Nginx + Rails 2.3.4 + Ubuntu 9.04 (10 steps)”

  1. If only I had a quarter for each time I came to… Amazing read.

  2. […] post is just copied from this page. As a study notes, I re-share this manual here. Please check out original one if you wanna have a […]

  3. […] post is just copied from this page. As a study notes, I re-share this manual here. Please check out original one if you wanna have a […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: