Camping 1.5 on Shared Hosting

This is how I set up Camping to use FastCGI on Dreamhost. This process should be adaptable to other similar environments.

If you're using Camping 2.0, check out this guide instead.

Steps

  • Set up your own gem path that you can install to and edit manually. You can find a good page about this process here.
  • Install the camping gem (version 1.5.180).
  • Edit the camping gem's camping/fastcgi.rb file.
  • Create a .htaccess file in your website's root directory.
  • Create a dispatch.fcgi file in your website's root directory.

Installing Camping

To get 1.5.180, you need to use _why's gem repository:

gem install camping --source=http://code.whytheluckystiff.net

Edit camping/fastcgi.rb

If your gem path is /home/username/.gems, then the path to this file would be /home/username/gems/gems/camping-1.5.180/lib/camping/fastcgi.rb.

Find the part that says:

if ENV['FORCE_ROOT'] and ENV['FORCE_ROOT'].to_i == 1
  path = req.env['SCRIPT_NAME']
else
  root = req.env['SCRIPT_NAME']
  path = req.env['PATH_INFO']
end

and change the second line, that reads:

path = req.env['SCRIPT_NAME']

to read:

path = req.env['REQUEST_URI']

Also: there's also a bug in exception handling that got fixed after 1.5.180. Fixing this will allow Camping errors to surface to your browser, instead of a plain 500. You need to manually repeat this bugfix. Just change:

rescue Exception => e 

to read:

rescue Exception => exc

.htaccess

This is a basic FastCGI .htaccess file, nothing here is particularly special to Camping.

AddHandler fastcgi-script .fcgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

dispatch.fcgi

This worked for me. Other sites say to supply the path/filename instead of the class to Camping::FastCGI.start, but that only gave me an error.

#!/usr/bin/ruby

ENV['GEM_PATH'] = '/usr/lib/ruby/gems/1.8:/home/your/gems'
ENV['GEM_HOME'] = '/home/your/gems'
ENV['FORCE_ROOT']=1.to_s

require 'rubygems'
require 'camping'
require 'camping/fastcgi'

Dir.chdir '/home/path/to/myapp'

Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'myapp.db'
require 'myapp'
Camping::FastCGI.start Myapp

Notes

  • Make sure your dispatch.fcgi is marked as executable! Run "chmod 755 dispatch.fcgi" if you're not sure.
  • If you followed the Dreamhost guide to making your own gem path, your gem path would be /home/username/.gems.
  • If you're trying to install gems remotely, Dreamhost will probably kill the process before it finishes. For me, using the 'nice' command didn't help. Get the gem files, scp them to your server, and install them locally ("gem install camping-1.5.180.gem"). This means installing dependencies in turn (activesupport, markaby, and metaid before camping).
  • When you first visit your site in a browser, it might hang for 60-120 seconds before timing out with an Internal Server Error, and your error log states that dispatch.fcgi never gave it anything. This might mean it's not set up right, but (sigh) it also might mean you just have to wait a bit. Do a "touch dispatch.fcgi", but if that doesn't work, wait some more. It just started working for me after like 5 or 10 minutes, and that was maddening.