First of all lets define the environment I am using. I am using Amazon EC2 server which is running Ubuntu and I am going to use Capistrano to deploy my Rails application. I am also using GitHub as my code repository.
Let me describe in a humanly understandable way what will actually be happening. You will call the deployment by syntax such as “cap production deploy” and the whole clever magic will happen at the background. What magic? So here it comes. Capistrano will use your SSH key to connect to your EC2 instance, from there it will connect to your GitHub account and will pull the latest version of your code into a specific folder. Capistrano will afterwards simlink the running application with the latest version which has just been pulled from Git and woalla you have the app. deployed.
Before you can start deploying with Capistrano there are couple of things you need to do. You will need SSH key which use use to connect to your EC2. I actually think that Capistrano requires both the public and private keys to be stored on your computer. You will also need to generate SSH key while connected to you EC2 instance. You will need to store the public version of this key in your GitHub account details. These keys will allows us to connect to EC2 instance and GitHub. That is all we need for safe connections.
I have said that in this example I am running Ubuntu server on my EC2 instance. To generate and store the SSH key on the Ubuntu server you might need to run the commands over ssh agent. You will get there by executing the following command.
ssh-agent bash
Now you should be able to generated and “add” the SSH key. If you are not sure how, check my article about generating SSH keys.
The folder you need to use for you SSH key sits in here – /home/ubuntu/.ssh as you are connected as user: ubuntu
One important note. I have found out that you can’t specify your own name for SSH key and you need to leave the password blank (just hit enter). The key will get the default name.
After you copied the public version of the key to your GitHub account you can double check by using the following command.
ssh git@github.com
You should be invited with your name and you will also get an security note. Just ignore that for now.
Installing Capistrano
The capistrano gem can be installed from here.
After the gem is installed you will need to capify your application. Get into the root of your application and execute this command.
capify .
This will create the necessary files and folders. You will find a file called deployment inside of your config directory.
These is the configuration you will need to add in.
set :application, "the_name_of_your_application"
set :repository, "git@github.com:your_git_hub_name/the_name_of_your_application.git"
set :scm, :git
set :user, "ubuntu"
set :use_sudo, false
#environment = 'production'
ssh_options[:keys] = ["#{ENV['HOME']}/.ssh/ec2key.pem"] // This is the path to your EC2 ssh key
// Deploying to Production
task :production do
role :web, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com"
role :app, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com"
role :db, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com", :primary => true
set :deploy_to, "/mnt/data/quantify/production/" // The folder where you will store your application on EC2. You will need to create these directories on your server before deployment. Note that my application sits in /mnt/data directory which is an Amazon elastic block. Do not deploy your app. in different directory otherwise you might loose all data if the instance experiences problems
set :rails_env, :production
set :current_path, "#{deploy_to}current"
environment = 'production'
end
// I am also running test server on the same machine
// The same task is used to deploy to test/staging environment. There are only two differences. The folder name where the app. will be deployed to and the name of the environment.
task :staging do
role :web, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com"
role :app, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com"
role :db, "YOUR_EC2_ADDRESS.compute-1.amazonaws.com", :primary => true
set :deploy_to, "/mnt/data/quantify/test/"
set :rails_env, :test
set :current_path, "#{deploy_to}current"
environment = 'test'
end
// This code will set permissions and restart the application after deployment
after 'deploy', 'set_permissions'
after 'deploy:setup', 'set_permissions'
task :set_permissions do
sudo "chown ubuntu #{deploy_to}"
sudo "chmod -R g+w #{deploy_to}"
end
namespace :deploy do
task :restart do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
If you are wondering how to setup test server or name servers for your domain name I will describe it in the next article so stay tuned.