Deploying Rails App to AWS EC2 Ubuntu Server With Capistrano
Techie January 2023
Introduction
Deploying application updates takes multiple steps. Performing all these steps
every time you want to deploy application updates is time-consuming and error-prone.
This section will guide you on how to automate the deployment of application updates
through Capistrano.
Capistrano is a popular task automation tool among Ruby developers. Once Capistrano is set up,
deploying further application updates only takes a single command.
The first thing you need to do is to install Capistrano into your rails project.
i). Open the Gemfile and add:
ii) Install the gem bundle and initialize Capistrano:
2 . Editing Capfile
Capfile is the Capistrano entry point. It defines what recipes to load. You must edit it to load the
recipes you need.
We will want Capistrano to automatically run bundle install, and we will want Capistrano to
automatically tell Passenger to restart our app. These are taken care of by the capistrano-bundler and
capistrano-passenger recipes. So make sure that the following lines are uncommented:
3 . Editing config/deploy.rb
The next step is to edit config/deploy.rb. This file contains configuration values that control how the
loaded recipes should do their jobs. It also defines additional commands to be executed on servers. You
must edit it according to your situation.
4 . Editing config/deploy/production.rb
The next step is to edit config/deploy/production.rb. This file defines the servers that Capistrano should
deploy to, in the form of SSH login information.
Our server is on Amazon EC2, so be sure to uncomment the set :ssh_options call and point the
keys option to your Amazon EC2 key file. Also set the auth_methods option to %w(publickey
password).
5 . Setting up a basic directory structure
Run the following commands on the server to setup a basic directory structure that Capistrano can
work with.
6 . Create initial configuration files
The app expects a config/database.yml and a
config/secrets.yml files. The contents of these
configuration files are only known to the server and persist across
application releases. The shared directory is the perfect place to place them.
And as configured before in config/deploy.rb, Capistrano will
automatically create symlinks within the release directory to those files.
i) On the server, create the shared/config directory and add database.yml and
secrets.yml files inside:
ii). Then fix and tighten permissions:
iii). edit database.yml file
iv). On your development machine, generate the secret key and add it to
config/initializers/secret_token.rb file.
v). Add the secret key in the secrets.yml file on the server
7 . Deploying a new release
You are now ready to deploy a new release using Capistrano!
On your local computer, make a random change in your application, then commit and push your changes.
Next, run Capistrano to start the deployment:
You have now automated deployments using Capistrano!