Capistrano

Capistrano

Capistrano

Capistrano is an open-source tool for executing scripts at the remote servers. Most commonly, it is used for deploying apps via SSH connection. Capistrano is written in Ruby as a component of the Ruby on Rails framework, therefore, it is widely used for Ruby apps deployment. Nevertheless, it can easily work with other programming languages, e.g. PHP.

Thus, in this instruction we will discover how to deploy a PHP application remotely, via the Capistrano tool. Initially you will need:

  • an already created PHP environment with Apache application server;
  • SSH public key generated and added to your Jelastic dashboard;
  • GIT repository with PHP application you would like to deploy (for now Capistrano 3 tool supports GIT VCS type only);
  • local copy of this project at your computer.

Let’s get started!

Note: commands below should be executed at your local machine’s user, similar to one you’ve used during SSH key pair generation, in order to avoid permission/connection errors.

Install Capistrano

1. For using Capistrano, you need to have Ruby installed at your local computer. Therefore, execute the appropriate command:

apt-get install ruby rubygems

2. Then, install the Capistrano tool by entering the following command:

gem install capistrano

3. Ensure you have the config folder in the local directory with your project (as it is a default folder with configurations for Ruby on Rails). Create this folder if you don’t have it.

mkdir {path_to_your_project}/config

Capify Your Application

After installation, you need to capify your application, i.e. configure Capistrano for app deployment. To do this, navigate to the root folder of your local PHP project and execute the next command:

cap install

This will create new files and directories in your project:

  • Capfile is the main Capistrano file that takes care of the required configs and globs for custom tasks.
  • config/deploy/ folder with two files (staging.rb and production.rb) for an environment’s specific deployment settings.
  • config/deploy.rb Ruby script which contains application configurations and Capistrano instructions.
  • lib/capistrano/tasks/ folder for your custom tasks.

Tip: As an option, you can try the dedicated capistrano-jelastic gem, maintained by gerado-navarro, for automating your Rails apps’ deployment to Jelastic PaaS&IaaS.

Set Custom Configurations

1. Navigate to the config/deploy.rb file and configure it corresponding to your settings. Initially it looks like following:

# config valid only for Capistrano 3.1
    lock '3.2.1'
    
    set :application, 'my_app_name'
    set :repo_url, '[email protected]:me/my_repo.git'
    
    # Default branch is :master
    # ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
    
    # Default deploy_to directory is /var/www/my_app
    # set :deploy_to, '/var/www/my_app'
    
    # Default value for :scm is :git
    # set :scm, :git
    
    # Default value for :format is :pretty
    # set :format, :pretty
    
    # Default value for :log_level is :debug
    # set :log_level, :debug
    
    # Default value for :pty is false
    # set :pty, true
    
    # Default value for :linked_files is []
    # set :linked_files, %w{config/database.yml}
    
    # Default value for linked_dirs is []
    # set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
    
    # Default value for default_env is {}
    # set :default_env, { path: "/opt/ruby/bin:$PATH" }
    
    # Default value for keep_releases is 5
    # set :keep_releases, 5
    
    namespace :deploy do
    
      desc 'Restart application'
      task :restart do
        on roles(:app), in: :sequence, wait: 5 do
          # Your restart mechanism here, for example:
          # execute :touch, release_path.join('tmp/restart.txt')
        end
      end
    
      after :publishing, :restart
    
      after :restart, :clear_cache do
        on roles(:web), in: :groups, limit: 3, wait: 10 do
          # Here we can do anything such as:
          # within release_path do
          #   execute :rake, 'cache:clear'
          # end
        end
      end
    
    end

Modify the next strings: