How To Automate Ruby On Rails Application Deployments Using Capistrano

Ruby on Rails | April 2, 2025

Tired of repetitive, mundane tasks when deploying your Ruby on Rails applications? Manual server updates, code uploads, and configuration changes can drain your development joy. Capistrano, a powerful task automation tool, is here to streamline your deployment process.

This guide builds upon previous knowledge from “Automating Deployments With Capistrano: Getting Started” and “How To Deploy Rails Apps Using Passenger With Nginx on CentOS 6.5.” It will walk you through setting up a robust CentOS server environment with Nginx and Passenger, and then automating deployments using Capistrano.

Prerequisites:

  • Familiarity with Git (see DigitalOcean’s Git resources).
  • Understanding of basic server administration.
  • Prior knowledge of Capistrano (recommended: read the introductory article).
  • Knowledge of deploying Rails apps with Passenger and Nginx.

Glossary

  • Capistrano: A Ruby-based automation tool for application deployment.
  • CentOS: A Linux distribution used for the deployment server.
  • Nginx: A high-performance web server.
  • Passenger: A web application server that integrates with Nginx.
  • RVM: Ruby Version Manager.
  • EPEL: Extra Packages for Enterprise Linux.
  • Git: A distributed version control system.

1. Preparing The Deployment Server

1.1 Updating And Preparing The Operating System

  1. Update system packages: $ yum -y update
  2. Install development tools: $ yum groupinstall -y 'development tools'
  3. Add the EPEL repository: $ sudo su -c 'rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'
  4. Update again: $ yum -y update
  5. Install necessary libraries: $ yum install -y curl-devel nano sqlite-devel libyaml-devel

1.2 Setting Up Ruby Environment and Rails

  1. Install RVM: $ curl -L get.rvm.io | bash -s stable
  2. Source RVM: $ source /etc/profile.d/rvm.sh
  3. Reload RVM: $ rvm reload
  4. Install Ruby 2.1.0: $ rvm install 2.1.0
  5. Install Node.js: $ yum install -y nodejs
  6. Install Bundler and Rails: $ gem install bundler rails

1.3 Downloading And Installing App. & HTTP Servers

  • Swap Space (if needed):
    • Create swap file: $ sudo dd if=/dev/zero of=/swap bs=1M count=1024
    • Format swap file: $ sudo mkswap /swap
    • Enable swap: $ sudo swapon /swap
  • Phusion Passenger:
    • Install Passenger: $ gem install passenger
  • Nginx:
    • Compile Nginx with Passenger module: $ passenger-install-nginx-module (Follow on-screen instructions).

1.4 Creating The Nginx Management Script

  1. Create script: $ nano /etc/rc.d/init.d/nginx
  2. Paste the provided script content.
  3. Make script executable: $ chmod +x /etc/rc.d/init.d/nginx

1.5 Configuring Nginx For Application Deployment

  1. Edit Nginx configuration: $ nano /opt/nginx/conf/nginx.conf
  2. Add passenger_app_env development; in the http block (for testing).
  3. Comment out the default location / block.
  4. Set application root and enable Passenger:
    Nginx

    root /home/deployer/apps/my_app/public;
    passenger_enabled on;
    
  5. Restart Nginx: $ /etc/init.d/nginx restart
  6. Check Nginx status: $ /etc/init.d/nginx status

1.6 Downloading And Installing Capistrano

  • Install Capistrano: $ gem install capistrano

1.7 Creating A System User For Deployment

  1. Add user: $ adduser deployer
  2. Set password: $ passwd deployer
  3. Grant sudo privileges: $ nano /etc/sudoers
  4. Add deployer ALL=(ALL) ALL.

2. Preparing Rails Applications For Git-Based Capistrano Deployment

2.1 Creating A Basic Ruby-On-Rails Application

  1. Create application: $ rails new my_app
  2. Enter directory: $ cd my_app
  3. Generate scaffold: $ rails generate scaffold Task title:string note:text
  4. Migrate database: $ RAILS_ENV=development rake db:migrate
  5. Start server (for testing): $ rails s

2.2 Creating A Git Repository

  1. Initialize repository: $ git init
  2. Add files: $ git add .
  3. Commit changes: $ git commit -m "first commit"
  4. Add remote: $ git remote add origin [email protected]:user123/my_app.git
  5. Generate SSH key: $ ssh-keygen -t rsa
  6. Add SSH key to GitHub.
  7. Set Git user info:
    • $ git config --global user.name "user123"
    • $ git config --global user.email "[email protected]"
  8. Push to GitHub: $ git push -u origin master

3. Working With Capistrano To Automate Deployments

3.1 Installing Capistrano Inside The Project Directory

  • Install Capistrano files: $ cap install

3.2 Working With config/deploy.rb Inside The Project Directory

  1. Edit config/deploy.rb: $ nano config/deploy.rb
  2. Add application settings:
    Ruby

    set :application, 'my_app'
    set :scm, :git
    set :repo_url, 'https://github.com/user123/my_app.git'
    set :deploy_to, "/home/deployer/apps/my_app"
    set :pty, true
    set :format, :pretty
    

3.3 Working With config/deploy/production.rb Inside The Project Directory

  1. Edit config/deploy/production.rb: $ nano config/deploy/production.rb
  2. Add server settings:
    Ruby

    role :app, %w{[email protected]}
    role :web, %w{[email protected]}
    role :db, %w{[email protected]}
    server '162.243.74.190', user: 'deployer', roles: %w{web}
    set :ssh_options, {
      forward_agent: false,
      auth_methods: %w(password),
      password: 'user_deployers_password',
      user: 'deployer',
    }
    

3.4 Deploying To The Production Server

  • Deploy: $ cap production deploy

This single command, executed from your local development machine within your Rails application’s directory, triggers the entire Capistrano deployment process. Here’s what happens behind the scenes:

1. Connection to the Deployment Server:

  • SSH Connection: Capistrano uses SSH (Secure Shell) to connect to your production server. The connection details (hostname/IP address, username, password or SSH key) are obtained from the config/deploy/production.rb file.
  • Authentication: Capistrano authenticates using the specified method (password or SSH key). In the provided example, password authentication is used. For better security, SSH keys are strongly recommended.

2. Source Code Retrieval:

  • Git Repository Access: Capistrano accesses your application’s source code from the Git repository specified in config/deploy.rb.
  • Code Cloning/Updating:
    • If this is the first deployment, Capistrano clones the repository to the deployment server.
    • For subsequent deployments, it fetches the latest changes from the repository.
  • Branch/Tag Selection: Capistrano can be configured to deploy a specific branch or tag. By default, it deploys the master branch.

3. Deployment Process:

  • Release Creation: Capistrano creates a new release directory on the server, typically within the releases directory inside the deploy_to path. Each release is timestamped, allowing you to easily roll back to previous versions.
  • Code Deployment: The source code is copied into the new release directory.
  • Dependency Installation: Capistrano executes bundle install to install the required Ruby gems based on your application’s Gemfile.
  • Database Migrations: If your application uses a database, Capistrano can automatically run database migrations using rake db:migrate.
  • Asset Precompilation: For Rails applications, Capistrano precompiles assets (CSS, JavaScript) using rake assets:precompile.
  • Symbolic Linking: Capistrano creates symbolic links to shared resources (e.g., database configuration files, uploaded files) from the release directory.
  • Current Release Update: Capistrano updates the current symbolic link to point to the new release directory, effectively making the new version live.

4. Post-Deployment Tasks:

  • Application Restart: Capistrano can be configured to restart your application server (e.g., Passenger, Puma) to load the new code.
  • Custom Tasks: You can define custom tasks in your config/deploy.rb file to perform any additional actions required for your application, such as clearing caches or running specific scripts.
  • Cleanup: Capistrano typically cleans up old releases, keeping only a specified number of recent deployments.

In essence, the cap production deploy command automates the following:

  • Connecting to your production server.
  • Retrieving the latest code.
  • Setting up the application in a new release directory.
  • Installing dependencies and running necessary tasks.
  • Switching the current release to the new version.
  • Restarting the application.

This automation significantly reduces the risk of errors and streamlines the deployment process, allowing you to deploy your applications quickly and reliably.