Coming Home To Merb

The highlight of RailsConf 2007 for me was discovering Merb on the plane ride up to Portland.

I remember reading the router code and thinking, wow, I can understand this (as opposed to alias_method_chain madness of Rails)! I wrote a sample app and enjoyed playing with this new framework.

Fast-forward to today and my life has been mainly consumed with my startup which is written in Rails. I love Rails but I’ve had the Merb itch ever since…

Luckily a recent micro app allowed me to reacquaint myself with Merb.

Here are a few notes I made while deploying a merb app to Debian Etch using Capistrano and Passenger.

Apache & Passenger

$ sudo apt-get install apache2 apache2-prefork-dev
$ sudo gem install passenger
$ sudo passenger-install-apache2-module

Add to /etc/apache2/apache2.config

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/bin/ruby1.8

Create /etc/apache2/sites-available/myproject

<VirtualHost *:80>
  ServerName www.myproject.com
  DocumentRoot /var/www/apps/myproject/current
</VirtualHost>

Enable site

$ sudo a2ensite myproject
$ sudo /etc/init.d/apache2 restart

Capistrano

deploy.rb

set :application, 'myproject'
set :domain,      'myproject.com'
set :deploy_to, "/var/www/apps/#{application}"
 
set :scm, :git
set :deploy_via, :remote_cache
set :repository,  "zackchandler@depixelate.com:/git/#{application}.git"
 
set :use_sudo, false
 
role :web, domain
role :app, domain
role :db,  domain, :primary => true
 
after 'deploy:update_code', 'deploy:native_gems'
 
namespace :deploy do
  
  # override Rails related callbacks
  task :finalize_update do
  end
 
  task :migrate do
  end
  
  desc 'recompile native gems'
  task :native_gems do
    run "cd #{latest_release};bin/thor merb:dependencies:redeploy"
  end
  
  desc 'restart app'
  task :restart do
    run "mkdir -p #{latest_release}/tmp; touch #{latest_release}/tmp/restart.txt"
  end
  
  desc 'Restart apache'
  task :restart_apache, :roles => :web do
    sudo '/etc/init.d/apache2 restart'
  end
end
$ sudo deploy

Other details

I installed all gems locally for the app like:

$ gem install -i gems/ merb-core --no-ri --no-rdoc
$ gem install -i gems/ rest-client --no-ri --no-rdoc
...

config.ru

begin 
  require 'minigems'
rescue LoadError 
  require 'rubygems'
end
 
if File.directory?(gems_dir = File.join(Dir.pwd, 'gems')) ||
   File.directory?(gems_dir = File.join(File.dirname(__FILE__), '..', 'gems'))
  $BUNDLE = true; Gem.clear_paths; Gem.path.unshift(gems_dir)
end
 
require 'merb-core'
 
Merb::Config.setup(:merb_root   => ".",
                   :environment => ENV['RACK_ENV'])
Merb.environment = Merb::Config[:environment]
Merb.root = Merb::Config[:merb_root]
Merb::BootLoader.run
 
run Merb::Rack::Application.new

That’s it. Passenger was super easy to install and configure.

Thanks to Ezra for hacking up Merb way back when and to all the core team for making Merb a very viable and solid framework.

Comment or question via
FYI: This post was migrated over from another blogging engine. If you encounter any issues please let me know on . Thanks.