Thursday, June 14, 2012

My introduction to Passenger, nginx and Sinatra - "Not found"

I spent way too long getting passenger and nginx working with my Sinatra app.  My problem was, that I had set up a middleware class.  Instead of using the "top level" of the file to define the responses, I created a class deriving from Sinatra::Base.  So for example I had this for my webmain.rb:

require 'sinatra/base'

class MyApp < Sinatra::Base
   set :sessions, true
   set :foo, 'bar'

   get '/' do
      'Hello world!'
   end
end

This worked fine when running the application with the rackup command, but in passenger, it just said "not found".  Not a very helpful error message at all.  There was nothing else on the page (if nginx has an error, it says 'nginx' centred on the page.), so apparently the problem was in sinatra.  This is the config.ru file that I had:

require 'rubygems'
require 'sinatra'
require File.join(File.dirname(__FILE__), 'webmain.rb')


run Sinatra::Application

Can you see what's wrong?  I couldn't either.  Apparently no-one else had been dense enough to have this problem on stack overflow or anywhere else I could find with google.  After trying different config.ru files and using strace on the nginx and passenger processes, I gave up for the night.

The next evening after getting home from work, I came to a realisation.  I changed the config.ru file to this:

require 'rubygems'
require 'sinatra'
root_dir = File.dirname(__FILE__)
require File.join(root_dir, 'webmain.rb')

run MyApp
I needed to actually run the class I created. Success!

Note that the File.join stuff is needed because I'm using ruby 1.9, which uses relative paths for "require"s that aren't in the installed directories.

Versions of the software I'm using:

Passenger: 3.0.12
Sinatra: 1.3.2
ruby: 1.9.3p0


Wednesday, June 13, 2012

The rvm sudo gem install gotcha

The title of the post says it all.  Ruby Version Manager (RVM) is handy for quickly switching between different versions of ruby, but if you have been using a system installation, your muscle memory may have you typing this to install a gem:
sudo gem install

It doesn't work as you might expect with rvm. It installs the gem in the gem repository of the system ruby.

I got caught out by this, and I hope others can learn from my mistakes.

So if you have done:

rvm use 1.9.2

Then to install a gem, you should go

gem install


Saturday, June 9, 2012

BrowserID with Ruby and Sinatra

I have been playing with Mozilla's browserid (soon to be called Mozilla Persona), which is a service to manage authentication with your email address as the login. I am using the ruby gem called sinatra/browserid to access it. When moving my code to a new computer, I started getting these errors:
application error TypeError at /_browserid_assert can't convert nil into String file: common.rb location: initialize line: 148
It seems to be a problem with a change in ruby 1.9.3 http.post which returns a single res instead of a res, body pair. I found a fork of the gem that fixes this problem. These are the commands to install the forked gem from github:
git clone https://github.com/passcod/sinatra-browserid.git gem build sinatra-browserid.gemspec sudo gem install sinatra-browserid-0.3.1.gem
(omit the sudo if you use rvm).