1: Create a new Rails app - we'll call is 'deploytest'
$ rails deploytest
$ cd deploytest
2: Create a local Git repository for it
$ git init
$ git add *
$ git commit -a -m 'initial commit'
$ git status
3: Create a couple of Capistrano files
$ capify .
4: Edit config/deploy.rb
# The name of your app
set :application, "deploytest"
# The directory on the EC2 node that will be deployed to
set :deploy_to, "/var/www/apps/#{application}"
# The type of Source Code Management system you are using
set :scm, :git
# The location of the LOCAL repository relative to the current app
set :repository, "."
# The way in which files will be transferred from repository to remote host
# If you were using a hosted github repository this would be slightly different
set :deploy_via, :copy
# The address of the remote host on EC2 (the Public DNS address)
set :location, "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
# setup some Capistrano roles
role :app, location
role :web, location
role :db, location, :primary => true
# Set up SSH so it can connect to the EC2 node - assumes your SSH key is in ~/.ssh/id_rsa
set :user, "root"
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]
The only account on a default EC2 instance is root. You probably want to create a second user that is responsible for your application.
5: Copy your SSH public key to your EC2 node
$ scp -i ~/my-ec2-keypair ~/.ssh/id_rsa.pub root@ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:/root/.ssh/authorized_keys2
NOTE the filename authorized_keys2 - not authorized_keys!!
6: Setup the EC2 node for Capistrano deployment.
From your LOCAL machine, not the EC2 node:
$ cap deploy:setup
7: Finally, deploy your application
$ cap deploy
You will see lots of output and with this dummy application some of those will report errors/warnings. Don't worry about that for now.
8: Check that the Deployment was successful
Connect to the EC2 node with SSH the regular way, cd to the app directory and check that everything is there. If that is all working then you are ready to deploy a real application and add custom tasks for managing the database, restarting the server etc.
Bear in mind that Capistrano add new 'releases' of your software in separate directories and symlinks the 'current' directory to the latest. So the root of your deployed application is the 'current' subdirectory.
Hope this will help you setting up your ec2 instance with capistrano.
I got a very good plugin at http://code.google.com/p/capistrano-mailer/.
Just copied the details below to have use in future.
It is a Capistrano Plugin AND a Rails Plugin
Ever wanted to be emailed whenever someone on the team does a cap deploy of trunk or some tag to some server. Wouldn't it be nice to know about it every time a release was deployed? For large rails projects this type of coordination is essential, and this plugin makes sure everyone on the need to know list is notified when something new is deployed.
This plugin is an extension to Capistrano.
That means it registers itself with Capistrano as a plugin and is therefore available to call in your recipes.
If you are looking to roll your own email integration into capistrano then try this pastie: http://pastie.org/146264 (thanks to Mislav Marohni?). But if you want to take the easy road to riches then keep reading
-- figurative "riches" of course, I promise nothing in return for your using this plugin
Requirements
- Rails 2.0.2
- Capistrano 2.1.0 - 2.2.0
Installation
./script/plugin install http://capistrano-mailer.googlecode.com/svn/trunk/capistrano_mailer
1. Install the plugin.
2. Add this line to the top of your deploy.rb:
require 'vendor/plugins/capistrano_mailer/lib/capistrano_mailer'
3. Add a cap_mailer_settings.rb file to your config/ directory:
require 'vendor/plugins/capistrano_mailer/lib/cap_mailer'
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.default.com",
:port => 25,
:domain => 'default.com',
:perform_deliveries => true,
:user_name => "releases@default.com",
:password => "mypassword",
:authentication => :login }
ActionMailer::Base.default_charset = "latin1"
CapMailer.template_root = "vendor/plugins/capistrano_mailer/views/"
CapMailer.recipient_addresses = ["dev1@default.com"]
CapMailer.sender_address = %("Capistrano Deployment" <releases@default.com>)
CapMailer.email_prefix = "[MYSITE-CAP-DEPLOY]"
CapMailer.site_name = "MySite.com"
CapMailer.email_content_type = "text/html"
4. Add these two tasks to your deploy.rb:
namespace :show do
desc "Show some internal Cap-Fu: What's mah NAYM?!?"
task :me do
set :command, task_call_frames.first.task.fully_qualified_name
puts "Running #{command} task"
end
end
namespace :deploy do
...
desc "Send email notification of deployment"
task :notify, :roles => :app do
show.me
mailer.send(rails_env, repository, command, deploy_to, host)
end
...
end
Make sure you've defined rails_env, repository, deploy_to and host. command is defined by the show:me task above.
The only required parameters to mailer.send are rails_env, repository, command and deploy_to. The complete set of possible parameters is:
mailer.send(rails_env, repository, command, deploy_to, host = nil, ip_address = nil, output = nil)
If anyone has a cool way of recording the output into a capistrano accessible variable, so that it can be shoved into the release email that would be an excellent contribution!
5. Then add the hook somewhere in your deploy.rb:
after "deploy", "deploy:notify"
6. Enjoy and Happy Capping!