Prerequisites
- A functioning Ruby on Rails app.
- A server where you’ll deploy the app. Here, we’ll use an Ubuntu server.
- SSH access to your server, which we’ll address as your_server_ip for the sake of simplicity..
Getting Started
Setting up Capistrano
First, add the Capistrano gem to your Gemfile
:
group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
Now, run bundler:
bundle install
After the gems are installed, run the Capistrano installer:
cap install
This will create several files in your project:
Capfile
config/deploy.rb
config/deploy/production.rb
config/deploy/staging.rb
Capfile
Your Capfile
should look like this:
# Load DSL and set up stages
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin 'Capistrano::SCM::Git'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/puma'
# Loads custom tasks from `lib/capistrano/tasks` if you have any defined.
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
deploy.rb
Next, configure your config/deploy.rb
file:
set :application, "my_app_name" # the name of your app
set :repo_url, "[email protected]:me/my_repo.git" # the URL of your git repository
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/var/www/#{fetch(:application)}"
# Default value for :format is :airbrussh.
# set :format, :airbrussh
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
# append :linked_files, "config/database.yml"
# Default value for linked_dirs is []
# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
# Default value for keep_releases is 5
set :keep_releases, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
# Puma settings
set :puma_init_active_record, true
production.rb
Now configure the config/deploy/production.rb
file:
server 'your_server_ip', user: 'deploy', roles: %w{app db web}
Replace 'your_server_ip'
with the IP address of your server, where you will be deploying the app. And replace 'deploy'
with the user that you want Capistrano to use when logging into the server.
Preparing the Server
Before you can deploy your app, you need to prepare your server. This includes creating a deploy user, installing necessary software & dependencies, then finally setting up the database.
Create a Deploy User
SSH into your server and create a new user (we’ll use deploy
as an example):
sudo adduser deploy
Then, add the deploy user to the sudo group:
sudo usermod -aG sudo deploy
Installing Necessary Software
To install Ruby, Node.js, Yarn, and Rails; run the following command
sudo apt-get update
sudo apt-get install curl gpg -y
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install ruby
rvm --default use ruby
ruby -v
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
npm install --global yarn
gem install rails
Setting Up the Database
You can install any database of your choice. But for simplicity’s sake, here we’ll use PostgreSQL. To install PostgreSQL, run the following command:
sudo apt-get install postgresql postgresql-contrib libpq-dev
Then, create a new PostgreSQL user:
sudo -u postgres createuser -s deploy
Nginx and Puma
To install Nginx, run the following command:
sudo apt-get install nginx
Now, go back in your local project, add the ‘puma’ gem to your Gemfile:
gem 'puma'
Then, run bundler
bundle install
Deploying Your Application
First Time Deployment
With everything set up, you can now deploy your application for the first time:
cap production deploy
The process of deployment may take some time, but upon completion, your application should be up and running.
Subsequent Deployments
For subsequent deployments, all you need to do is run the following command and capistrano will take care of the rest.
cap production deploy
Rollbacks
If an issue arises, you can effortlessly rollback to the previous version:
cap production deploy:rollback
Conclusion
Congratulations! You’ve successfully deployed a Ruby on Rails application using Capistrano. Although this article covers the fundamental aspects of deploying a Rails application with Capistrano, there’s much more to explore. As you gain comfort with the tool, you can begin to investigate more advanced features, like automating database backups, executing custom scripts on the server, and beyond. Happy deploying!