Here’s how I got a Sinatra version 1.0 web application to work via CGI using Rack version 1.1.0 behind an Apache httpd version 2.2.15 web server on Fedora 13 Linux. There are many ways to get a client request passed through to your application with httpd — it has a very flexible configuration — but the following is what I did, and should give you the general idea.
First, use the following command to check that httpd has the mod_rewrite module loaded:
$ apachectl -t -D DUMP_MODULES | grep rewrite_module Syntax OK rewrite_module (shared)
Next, edit the relevant httpd configuration file (see the Apache documentation for your system), which for me was the /etc/httpd/conf/httpd.conf file, and add the following configuration items, where [path] is the absolute path to the directory of your Sinatra application, and [url] is the URL you want users to access your application with:
RewriteEngine On RewriteRule ^[url].*$ [path]/dispatch.cgi [QSA,L] <Directory [path]> AddHandler cgi-script .cgi Options +ExecCGI </Directory>
You’ll see that the rewrite rule sends everything to a CGI file named dispatch.cgi along with any query strings. That’s the file that will kick start our application. So, let’s create that file and add the following lines to it:
#!/usr/local/bin/ruby require "rubygems" require "rack" require File.join(File::dirname(__FILE__), "testapp.rb") Rack::Handler::CGI.run(TestApp)
You can see that my ruby executable is in /usr/local/bin, which you can change as necessary. You can also see that I named my application’s main Sinatra::Base class TestApp, and that I named the file holding the TestApp class testapp.rb. Now, let’s create the testapp.rb file, and add the following lines:
require 'rubygems' require 'sinatra' class TestApp < Sinatra::Base get '' do "Hello from Sinatra!" end end
Note that according to the Sinatra documentation, logging is “… disabled by default in Sinatra::Base subclasses” unlike when you run the application with something like ruby testapp.rb. In any event, you should be able to start creating your Sinatra application within TestApp now. Happy coding!