Setup Capistrano to deploy Rails application on Amazon EC2 with Git
Posted by Bhushan G Ahire | Posted in capistrano, git, Rails, ruby, Security, Subversion | Posted on 17-02-2010
0
$ 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.
