mongrel_cluster snippets

Installing Rails, mongrel and mongrel_cluster on Debian

Tagged rails, ruby, debian, install, sqlite3, mongrel, mongrel_cluster  Languages bash

DRAFT...

Install RubyGems

http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz

tar zxvf rubygems-1.0.1.tgz

cd rubygems-1.0.1

ruby setup.rb

Install Rails

gem install rails

Install sqlite3 (optional)

apt-get install sqlite3 libsqlite3-dev
gem install sqlite3-ruby

Install mongrel and mongrel_cluster

$ gem install mongrel mongrel_cluster

$ mongrel_rails cluster::configure -e production \
  -p 8000 \
  -a 127.0.0.1 \
  -N 3 \
  -c /var/www/xyz/current


$ mongrel_rails cluster::start


$ useradd -g www-data -d /var/www mongrel

Surviving reboots

sudo mkdir /etc/mongrel_cluster

sudo ln -s /var/www/xyz/config/mongrel_cluster.yml /etc/mongrel_cluster/xyz.yml

sudo cp /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/

sudo chmod +x /etc/init.d/mongrel_cluster

sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults

mongrel_cluster_ctl status

Stale pids

If your mongrels crash or if you kill them, mongrel_cluster won't start your mongrels because mongrel_cluster believes the processes are still running, instead mongrel_cluster complains and does nothing:

** !!! PID file tmp/pids/mongrel.8000.pid already exists.  Mongrel could be running already.  Check your log/mongrel.8000.log for errors.
** !!! Exiting with error.  You must stop mongrel and clear the .pid before I'll attempt a start.

To fix this simply add the --clean switch to the /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster startup script:

mongrel_cluster_ctl start -c $CONF_DIR --clean

How to use Vlad the Deployer with git, nginx, mongrel, mongrel_cluster and Rails

Tagged vlad, deployer, deploy, capistrano, nginx, mongrel, mongrel_cluster  Languages ruby

This is a draft...

Installing Vlad the Deployer

gem install vlad

Configuring Vlad the Deployer

Add this to the end of RakeFile:

begin
  require 'rubygems'
  require 'vlad'
  Vlad.load :scm => :git
rescue LoadError => e
  puts "Unable to load Vlad #{e}."
end

Note that we're telling Vlad to use git. This snippet gives you a quick introduction on how to use git with Rails.

Creating the deployment recipe

If you're uncertain what these variables mean, have a look at the docs. This folder is also worth a look, and don't forget to take a peek at the vlad source code.

#
# General configuration
#
set :ssh_flags,             '-p 666'
set :application,           'xxx.com'
set :domain,                '127.0.01'
set :deploy_to,             '/var/www/xxx.com'
set :repository,            '/var/lib/git/repositories/xxx.com/.git/'


#
# Mongrel configuration
#
set :mongrel_clean,         true
set :mongrel_command,       'sudo mongrel_rails'
set :mongrel_group,         'www-data'
set :mongrel_port,          9000
set :mongrel_servers,       3

#set :mongrel_address,       '127.0.0.1'
#set(:mongrel_conf)          { '#{shared_path}/mongrel_cluster.conf' }
#set :mongrel_config_script, nil
#set :mongrel_environment,   'production'
#set :mongrel_log_file,      nil
#set :mongrel_pid_file,      nil
#set :mongrel_prefix,        nil
#set :mongrel_user,          'mongrel'

#
# Customize Vlad to our needs
#
namespace :vlad do
  #
  # Add an after_update hook
  #
  remote_task :update do
    Rake::Task['vlad:after_update'].invoke
  end

  #
  # The after_update hook, which is run after vlad:update
  #
  remote_task :after_update do
  # Link to shared resources, if you have them in .gitignore
  #  run "ln -s #{deploy_to}/shared/system/database.yml #{deploy_to}/current/config/database.yml"
  end

  #
  # Deploys a new version of your application
  #
  remote_task :deploy => [:update, :migrate, :start_app]
end

Setup the server

$ rake vlad:setup

This will create the necessary folders and mongrel_cluster configuration file.

Deploy the application

Now deploy the application with vlad:deploy, which is a custom rake task that we added to the deployment recipe:

$ rake vlad:deploy

Copying your SSH public key to the remote server

Vlad uses ssh for executing commands on the remotely, and rsync for copying the build to your server, which means you'll quickly grow tired of typing your password each time a command is run.

This problem is solved by copying your public SSH keys to the remote server, this snippet explains how to do exactly that.