ruby backend via apache passenger rack

Tue, Mar 24, 2009

I’ve been using PHP for yonks for backend stuff for UIs and when I noticed Ruby I also noticed it was nowhere near as easy to use from Apache as PHP. Until now that is. Using Phusion Passenger, you can set up script handlers for UIs using Ruby:

For the following to work you need rubygems in your path:

$PATH = /PATH/TO/rubygems/lib/ruby/gems/1.8/bin:${PATH}
export APXS2=/PATH/TO/APACHE2/bin/apxs
gem install passenger
When you run the above, you might get some guff about “wrong constant name ./DOC/TEMPLATE/HORO” but I just ignored this as it’s the docs. You’ll also get stuff to do with Apache. Add this to your httpd.conf:
LoadModule passenger_module /PATH/TO/rubygems/lib/ruby/gems/1.8/ 
gems/passenger-2.1.2/ext/apache2/ PassengerRoot /PATH/TO/rubygems/lib/ruby/gems/1.8/gems/passenger-2.1.2 PassengerRuby /PATH/TO/ruby/bin/ruby
To get a rack app running behind passenger in a sub directory of your website, go to your DocumentRoot and create the structure:
cd ${DocumentRoot} (from apache config)
mkdir rack-hello-world
mkdir rack-hello-world/public
cd rack-hello-world
create a file called in the rack-hello-world directory and put the following in it:
app = lambda { |env| [200, { ‘Content-Type’ => ‘text/html’ }, ‘Hello World’] }
run app
Now, in DocumentRoot, create a new symlink:
ln -s rack-hello-world/public rhw
back in apache, add this to your httpd.conf next to DocumentRoot:
RackBaseURI /rhw
and restart apache. You can now view your Rack Hello World at the URL:

comments powered by Disqus