Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fzrhrs/87542ee10b2ddc336fce5312a23b77da to your computer and use it in GitHub Desktop.
Save fzrhrs/87542ee10b2ddc336fce5312a23b77da to your computer and use it in GitHub Desktop.
Part 2 of 3: Rails Deployment Ubuntu 24.04 (2024).md

Summary:

  1. Add Capistrano and other relevant gems to Gemfile.
  # Deployment
  gem 'capistrano'
  gem 'capistrano-rvm'
  gem 'capistrano-rails'
  gem 'capistrano-bundler'
  gem 'capistrano3-puma', '>=6.0.0.beta.1'
  
  # Deployment optional
  gem 'capistrano-rails-console'
  gem 'capistrano-rails-tail-log'
  gem 'capistrano-rails-db'
  gem 'capistrano-rake', require: false

Make sure to add these gems in development group in your Gemfile.

Refer to individual gem instruction on how to install these gems.

To capify your project, run:

bundle exec cap install

You should then have the following:

├── Capfile
├── config
│   ├── deploy
│   │   ├── production.rb
│   │   └── staging.rb
│   └── deploy.rb
└── lib
    └── capistrano
            └── tasks

Now, go to your Capfile and make sure to have this:

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
require 'capistrano/scm/git'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'

# Includes additional tasks from other gems in the Gemfile
require 'capistrano/rails/console'
require 'capistrano/rails_tail_log'
require 'capistrano/rails/db'
require 'capistrano/rake'

# Load the SCM plugin appropriate to your project:
install_plugin Capistrano::SCM::Git
install_plugin Capistrano::Puma, load_hooks: false # Default puma tasks without hooks
install_plugin Capistrano::Puma::Systemd

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Your deploy.rb should have something like this:

# config valid for current version and patch releases of Capistrano
lock '~> 3.19.1'

# Change these
set :repo_url,        [URL_REPO_IN_SSH]
set :user,            [USER]

# Don't change these unless you know what you're doing
set :pty,             false
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :nginx_use_ssl,   true

## Defaults:
set :format,        :pretty
set :log_level,     :debug
set :keep_releases, 5

## Linked Files & Directories (Default None):
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb', 'config/master.key')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads', 'sockets')

## Capistrano Puma
# set :puma_enable_socket_service, true

namespace :deploy do
  after 'deploy:restart', 'deploy:cleanup'
end

And your deploy/[ENVIRONMENT].rb file should be similar to this:

set :stage,           [STAGE]
set :rails_env,       [RAILS_ENVIRONMENT]
set :branch,          [BRANCH_YOU_WANT_TO_TARGET]

set :application,     [APPLICATION_NAME]
set :deploy_to,       "/home/#{fetch(:user)}/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/sockets/puma.sock"

server [IP ADDRESS], user: fetch(:user), roles: [:web, :app, :db], primary: true
  1. Run cap [stage] deploy:check and just follow the errors.
  2. When everything looks good, try deploy by typing: cap [stage] deploy.
  3. Next, configure Nginx setting for the application. Ask Google to help you with this.
  4. Do not forget to restart Nginx everytime you make changes to the conf file.
  5. Check the logs (app log, puma log, Nginx log) for errors.

The next part will document the systemd configuration for Puma 6 and above.

Rails Deployment Ubuntu Series:

  1. Part 1 of 3: Rails Deployment Ubuntu 24.04 (2024)
  2. Part 2 of 3: Rails Deployment Ubuntu 24.04 (2024)
  3. Part 3 of 3: Rails Deployment Ubuntu 2404 (2024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment