This post builds on my previous posts on Ubuntu Lucid VPS Base Build and RVM and Passenger to create a suitable capistrano config file for deploying rails 3 applications.
Assuming you have followed the previous posts, you should be able to run simple rack applications and have a passenger user setup with ssh access. You should also have a rails 3 application that you would like to deploy and ideally this should be using git for source control.
Capistrano configuration
Make sure you have capistrano install:
gem install capistrano
Initialise the rails application:
cd ~/dev/mywebsite
capify .
This will generate some files, along with a sample config/deploy.rb. Replace the file contents with the following, using suitable values where applicable.
# RVM bootstrap
$:.unshift(File.expand_path("~/.rvm/lib"))
require 'rvm/capistrano'
set :rvm_ruby_string, '1.9.2'
set :rvm_type, :user
# main details
set :application, "mywebsite"
role :web, "mywebsite.com"
role :app, "mywebsite.com"
role :db, "mywebsite.com", :primary => true
# server details
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_to, "/var/www/mywebsite"
set :deploy_via, :remote_cache
set :user, "passenger"
set :use_sudo, false
# repo details
set :scm, :git
set :scm_username, "passenger"
set :repository, "git@gitserver:mywebsite.git"
set :branch, "master"
set :git_enable_submodules, 1
# runtime dependencies
depend :remote, :gem, "bundler", ">=1.0.0.rc.2"
# tasks
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
desc "Symlink shared resources on each release"
task :symlink_shared, :roles => :app do
#run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end
after 'deploy:update_code', 'deploy:symlink_shared'
namespace :bundler do
desc "Symlink bundled gems on each release"
task :symlink_bundled_gems, :roles => :app do
run "mkdir -p #{shared_path}/bundled_gems"
run "ln -nfs #{shared_path}/bundled_gems #{release_path}/vendor/bundle"
end
desc "Install for production"
task :install, :roles => :app do
run "cd #{release_path} && bundle install --production"
end
end
after 'deploy:update_code', 'bundler:symlink_bundled_gems'
after 'deploy:update_code', 'bundler:install'
Capistrano deployment
The first time you deploy to the server you should run the deployment setup task:
cap deploy:setup
Thereafter you can deploy using:
cap deploy
Or if you have database migrations to run then use:
cap deploy:migrations
You may get some strange errors or failures when deploying. If you have followed the steps I have mentioned in this and previous articles then you shouldn’t have many problems. Common problems are:
- wrong permissions of
/var/www - wrong permissions of passenger user
- not having rvm installed for passenger users
- not having the basic gems required to use capistrano on the server, simply install them as the passenger user
Apache configuration
You now need to create a virtual host for the website so that apache can handle the requests and pass off to passenger.
Create /etc/apache2/sites-available/mywebsite.com with the following contents:
<VirtualHost *:80>
ServerName mywebsite.com
DocumentRoot /var/www/mywebsite/public
<Directory /var/www/mywebsite/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
Enable the site, and then view it in your browser:
sudo a2ensite mywebsite.com
sudo /etc/init.d/apache2 reload
Comments
blog comments powered by Disqus