Thunder Data Systems

10 Steps to Capistrano and Plesk Subdomains Deployment

10 Steps to Capistrano and Plesk Subdomains Deployment

We’ve adopted a standard convention for deploying Rails apps that places the files in a subdomain of our clients’ domains. Since we use Plesk, there are some steps we have to take to get Capistrano deploys working. Since we deploy a lot of apps, I decided to write this guide as much for the office as for anyone who may find it useful. Let’s go through the steps:

  1. Create your subdomain in Plesk (ie. myrailsapp)
  2. Navigate to the /domain.com/subdomains/myrailsapp/conf directory
  3. Create a vhost.conf file. Again, we use a convention that creates a new directory within /myrailsapp that holds our rails code. Fittingly, we name the directory /rails. The DocumentRoot points to the public directory of our application. Enter the following into the vhost.conf file:
  4. DocumentRoot /var/www/vhosts/domain.com/subdomains/myrailsapp/rails/current/public 
    RailsEnv production
    
    
  5. If you haven’t already installed the capistrano and capistrano-ext (for multistage environments such as testing and production deploys), you’ll need to get them. The Capistrano tutorials will walk you through that process.
  6. With the gems installed, now we want to capify our code. In your root rails app directory, enter the following command. (You may have to sudo the command depending on your directory permissions.)
  7. capify .
    

    This will create two files: Capfile and deploy.rb in your /config directory.

  8. Open up your deploy.rb file and enter your configuration variables. Again, you can refer to the Capistrano tutorials for help.
  9. If you are only planning to deploy to a single environment, you’re work is done. If you plan a multi-stage environment, you’ll want to set the stages in your deploy.rb file and require the capistrano-ext gem like this:
    set :stages, %w(testing, production)
    set :default_stage, "testing"
    require "capistrano/ext/multistage"
    
    
  10. Within the /config directory, create a new /deploy directory and two new files, testing.rb and production.rb. These files will provide the deploy details for each environment.
  11. Open up your new .rb files and enter the following for each environment:
    
    #############################################################
    #   Application
    #############################################################
    
    set :application, "myrailsapp"
    set :deploy_to, "/var/www/vhosts/domain.com/subdomains/myrailsapp/rails"
    
    #############################################################
    #   Settings
    #############################################################
    
    default_run_options[:pty] = true
    ssh_options[:forward_agent] = true
    set :use_sudo, true
    set :scm_verbose, true
    set :rails_env, "testing" 
    
    #############################################################
    #   Servers
    #############################################################
    
    set :user, "username"
    set :password, "user_password"
    set :domain, "myrailsapp.domain.com"
    server domain, :app, :web
    role :db, domain, :primary => true
    
    #############################################################
    #   Passenger
    #############################################################
    
    namespace :deploy do
      desc "Create the database yaml file"
      task :after_update_code do
        db_config = <<-EOF
        production:    
          adapter: mysql
          encoding: utf8
          username: database_username
          password: database_password
          database: database
          host: localhost
        EOF
        
        put db_config, "#{release_path}/config/database.yml"
    
      end
        
      # Restart passenger on deploy
      desc "Restarting mod_rails with restart.txt"
      task :restart, :roles => :app, :except => { :no_release => true } do
        run "touch #{current_path}/tmp/restart.txt"
      end
      
      [:start, :stop].each do |t|
        desc "#{t} task is a no-op with mod_rails"
        task t, :roles => :app do ; end
      end
      
    end
    
    
    Then add the details of your repository for either subversion or git.
    
    
    #############################################################
    #   GIT
    #############################################################
    
    set :scm, :git
    set :branch, "master"
    set :repository, "git@github.com:your_github_path/MYRAILSAPP.git"
    set :deploy_via, :remote_cache
    ssh_options[:forward_agent] = true
    
    #############################################################
    #   SVN
    #############################################################
    
    set :scm, :svn
    set :branch, "trunk"
    set :scm_user, 'username'
    set :scm_password, "user_password"
    set :repository, Proc.new { "--username #{scm_user} --password '#{scm_password}' --no-auth-cache http://doman.com/path/to/your/subversion/repository/#{application}/trunk" } 
    
    set :deploy_via, :remote_cache
    ssh_options[:forward_agent] = true
    
    
  12. That’s it; you’re ready to deploy. Back at your terminal window, you’ll type the respective environment to which you’re deploy:
cap testing deploy

A final note: make sure the user specified in your deploy files has permission to create directories on the server, otherwise your /releases directories will not be created.

Feel free to ask any questions if you’re having trouble with your Plesk subdomain and Capistrano deployment. Good luck!

3 thoughts on “10 Steps to Capistrano and Plesk Subdomains Deployment

  1. Nicolas

    Hi, Thanks for this, your step 3 saved my night! Plesk is evil with symlinks… Cheers.

Leave a Reply

Your email address will not be published.