Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created November 20, 2013 09:34
Show Gist options
  • Select an option

  • Save stevenyap/7560365 to your computer and use it in GitHub Desktop.

Select an option

Save stevenyap/7560365 to your computer and use it in GitHub Desktop.
Capistrano 2 Server Setup

This guide explains the way to setup a production server using Capistrano.

Pre-requistions

  • CentOS 6
  • Root access with SSH
  • depot is the assumed name of the project in this guide. Please change it to your own project name.
  • Server is installed with Apache/Passenger/Bundler/RVM.
    Please refer to Server Setup.md gist for info on how to set it up.

Apache Config

  • Create a file at vi /etc/httpd/conf.d/localhost.conf
<VirtualHost *:80>
    ServerName localhost
    
    # Note that current is the folder created by Capistrano
    # It is created as a sym-link to /releases which contains the latest codes
    DocumentRoot /var/www/html/current/public
	  
	  # Add it as development first to see any errors
	  # Comment out for production
	  RailsEnv development
	  
    <Directory /var/www/html/current/public>
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
	    Order Allow,deny
	    Allow from all
    </Directory>
</VirtualHost>

Setup Git on Server

  • This setups the git server on the remote server
  • Developer will push to this folder (~/git/depot.git) and Capistrano will pull from this folder and deploy it
mkdir -p ~/git/depot.git
cd ~/git/depot.git
git --bare init

Next, we need to setup the SSH for Capistrano to access the Git:

test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

# Need to do this for CentOS
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys 
restorecon -Rv ~/.ssh

# Accepting host authenticity for user
# Test it (you need to accept the host authenticity):
ssh localhost
# if you did not see any prompt for password, it is done!

# Accept host authenticity for git:
git ls-remote ssh://user@domain/~/git/depot.git master

Setup Capistrano on LOCAL development

  • Capistrano is a development gem which assist the developer to run commands on the production server (something like a Heroku toolbelt)
  • Hence, it is installed and configured on developer's computer
# Gemfile

# Use Capistrano for deployment
group :development do
  gem 'capistrano', '~>2.15'
  gem 'rvm-capistrano'
end
  • Run bundle install
  • Run capify .
  • Edit the file Capfile
# Capfile

load 'deploy'

# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
  • Edit the file config/deploy.rb with your own user, domain and application:
require 'bundler/capistrano'

# be sure to change these
set :user, 'root'
set :domain, 'localhost'
set :application, 'depot'

# if the SSH port is different
ssh_options[:port] = 2222

# adjust if you are using RVM, remove if you are not
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.0.0-p247' # Make sure it matches your ruby version (run "ruby --version" to check it)
require 'rvm/capistrano'

# file paths
set :repository, "ssh://#{user}@#{domain}:2222/~/git/#{application}.git"
set :deploy_to, "/var/www/html"

# distribute your applications across servers (the instructions below put them # all on the same server, defined above as 'domain', adjust as necessary)
role :app, domain
role :web, domain
role :db, domain, :primary => true

# you might need to set this if you aren't seeing password prompts
# default_run_options[:pty] = true
# As Capistrano executes in a non-interactive mode and therefore doesn't cause
# any of your shell profile scripts to be run, the following might be needed
# if (for example) you have locally installed gems or applications.  Note:
# this needs to contain the full values for the variables set, not simply
# the deltas.
# default_environment['PATH']='<your paths>:/usr/local/bin:/usr/bin:/bin'
# default_environment['GEM_PATH']='<your paths>:/usr/lib/ruby/gems/1.8'
# miscellaneous options
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
set :normalize_asset_timestamps, false
set :rails_env, :development
set :rvm_type, :system

namespace :deploy do
  desc "cause Passenger to initiate a restart"
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end

  desc "reload the database with seed data"
  task :seed do
    deploy.migrations
    run "cd #{current_path}; rake db:seed RAILS_ENV=#{rails_env}"
  end
end

Push from local to server

  • This assumes your local is already git initizated (Why shouldn't you!??)
  • After you have capify and edited all the Capistrano config, you should push it to the server:
git remote add production ssh://user@host/~/git/depot.git
git push production master

Deploy Capistrano

  • First time Capistrano is setup, we need to run
cap deploy:setup
  • You can do a check via cap deploy:check

Setup is finished!

  • Setup is completed and continuous deployment will follow the flow like below:
git add .
git commit -m "New Commit Message"
git push production master

cap deploy
cap deploy:migrate
cap deploy:seed

Generate Public Key

  • This will skip the step of entering password everytime you push into the server
ssh-keygen -t rsa -C "your_email@example.com"
# go with the default file storage (~/.ssh/id_rsa.pub) and enter a good passphrase
  • Send your id_rsa.pub to the server
# Make sure .ssh directory exists in the server
scp ~/.ssh/id_rsa.pub root@localhost:~/.ssh/id_rsa.pub

# or send with port 2222 if you are using VirtualBox
scp -P 2222 ~/.ssh/id_rsa.pub root@localhost:~/.ssh/id_rsa.pub
  • Next, SSH into the server and run the following command:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

# Need to do this for CentOS
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys 

restorecon -Rv ~/.ssh
  • That's it!

Issues

  • Nokogiri gem installation error: run yum install libxml2 libxml2-devel libxslt libxslt-devel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment