Monday, 14 May 2012

Deploying Ruby on Rails on Plesk Server

Disclaimer: I have been creating a deployment document for deploying Rails on a Plesk server over the past few months, while gathering information and testing I have come across many useful sources, while I would like to credit these sources I have not been very good at logging the website addresses of them. If you believe any of the below information should be credited to yourself or a website you are aware of please send me a link to the site and I will create a “Thanks to” link list at the top of this post. The below information has worked for me on a Plesk 10 system – I cannot guarantee it will work on other versions.

Assumptions: This document assumes you have already:

  • Installed Ruby on Rails via RVM
  • Installed the Phusion Passenger
  • Are using Rails 3.0 or above
  • Are running Plesk on a Linux based server
  • Are using Capistrano for deployment
  • You have created a Plesk domain for your application, created a database and configured your database.yml file for production.
  • You are deploying via github.com


NOTE: In the below example I am deploying the fictional domain foo.bar – you will need to change all references of foo.bar to your domain name. You will also need to edit any code wrapped in double curly brackets

{{}}

Prepare the Rails application:

In order to deploy you need to prepare the application – to do this run

capify .

from the application root. This will create the Capistrano files needed for deployment. The first file to edit is the config/deply.rb file. Below is a copy of the deploy.rb file I use – you will most likely need to edit this to match your production environment.

# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

# Load RVM's capistrano plugin.
require "rvm/capistrano"

set :rvm_ruby_string, '{{1.9.2@rails3}}'
#set :rvm_type, :user  # Don't use system-wide RVM

set :user, "{{username}}"
set :password, "{{password}}"
set :domain, "{{foo.bar}}"
set :application, "{{Foo Bar}}"
set :repository,  "git@github.com:{{username}}/{{gitrepo}}.git"
set :deploy_to, "/var/www/vhosts/foo.bar/rails"

set :scm, :git
# Or: 'accurev', 'bzr', 'cvs', 'darcs', 'git', 'mercurial', 'perforce', 'subversion' or 'none'

role :web, domain                           # Your HTTP server, Apache/etc
role :app, domain                           # This may be the same as your 'Web' server
role :db,  domain, :primary => true         # This is where Rails migrations will run

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

after "deploy:update_code", :bundle_install
desc "Running the bundle install"
task :bundle_install, :roles => :app do
  run "cd #{release_path} && bundle install"
end

Once you have prepared your config/deploy.rb file you will probably also want to configure Capistrano to compile the asset pipeline – this can be done by adding the following to the bottom of the capfile

load 'deploy/assets'

Create the server paths

Now you will need to create your Rails folder on your server – using SSH you can do this by running the following command:

sudo mkdir /var/www/vhosts/foo.bar/rails

(This should be the default location for your site)

Setup and run initial deploy

Right. Now the server file path is configured you can run the deploy setup from your local machine using

cap deploy:setup

Assuming this ran ok you can now deploy the site using

cap deploy:migrate

This will deploy the Rails app and migrate the database.

Configure Apache

Next we need to configure Apache to see the new Rails application – we can do this using a vhost.conf file on the server.

sudo nano /var/www/vhosts/foo.bar/conf/vhost.conf

and add the following to this file:

ServerName foo.bar
DocumentRoot /var/www/vhosts/foo.bar/rails/current/public
RailsEnv "production"
<Directory "/var/www/vhosts/foo.bar/rails/current/public">
AllowOverride All
Options Indexes FollowSymLinks -MultiViews
</Directory>

Next we need to tell Apache to include this new config file using

sudo /usr/local/psa/admin/sbin/httpdmng --reconfigure-domain foo.bar

And test the new configuration

sudo apache2ctl configtest

If all looks ok you can restart apache

sudo /etc/init.d/apache2 restart

You should now be able to browse to your new website

Additional permission changes

You may see an error 500 – if this is the case try the following from your sever:

sudo chown nobody -R /var/www/vhosts/foo.bar/rails/shared/log
sudo chown nobody -R /var/www/vhosts/foo.bar/rails/shared/system
sudo chmod 777 /var/www/vhosts/foo.bar/rails/shared/log/production.log

This should now give you a fully working Rails app hosted alongside Plesk.

comments powered by Disqus