Skip to content

Camping on Shared Hosting

peterfairfield edited this page Sep 16, 2010 · 4 revisions

Camping on Shared Hosting
This is how to set up Camping 2.0 to use FastCGI in a shared environment, such as Dreamhost. See these threads to see the Camping community figuring it out.

If you’re using Camping 1.5, check out this guide instead.

If your web host has Phusion Passenger installed (as Dreamhost does), you don’t have to bother with FastCGI – see the the Passenger instructions at the bottom of the page.

Steps
Check out Camping 2.0 from its Github home onto your web server.
Create a .htaccess file in your website’s root directory.
Create a dispatch.fcgi file in your website’s root directory.

.htaccess
This is a basic FastCGI .htaccess file. The last line is the most important.

AddHandler fastcgi-script .fcgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On

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

dispatch.fcgi
Make sure your dispatch.fcgi is marked as executable! Run “chmod 755 dispatch.fcgi” if you’re not sure.
The second part of GEM_PATH should be your host’s installed gems location, the example below is taken from Dreamhost.


#!/usr/bin/ruby

ENV[‘GEM_PATH’] = ‘/path/to/my/gems:/usr/lib/ruby/gems/1.8’
ENV[‘GEM_HOME’] = ‘/path/to/my/gems’

Dir.chdir ‘/path/to/my_app’

require ‘my_app’
MyApp.create

class ApacheFixer
def initialize(app); @app = app; end

def call(env) env[‘SCRIPT_NAME’] = ‘/’ env[‘PATH_INFO’] = env[‘REQUEST_URI’][0..(env[“REQUEST_URI”].index(“?”)||0)-1] @app.call(env) end

end

Rack::Handler::FastCGI.run ApacheFixer.new(MyApp)


Using CGI
If you’re having issues with FastCGI, try to get it working with CGI first. To do this, change the examples above:

In dispatch.fcgi, change “Rack::Handler::FastCGI” to “Rack::Handler::CGI”.
Rename dispatch.fcgi to dispatch.cgi.
Update the last line of .htaccess to point to dispatch.cgi instead of dispatch.fcgi.
Notes for Dreamhost
If you’re having trouble with timeouts, try getting this to work for CGI first. If CGI works, then FastCGI should work, and Dreamhost is just being stupid. Change it back to use FastCGI, and come back later. This worked for me a couple times, and I place the blame on Dreamhost.
Set up your own gem path that you can install to and edit manually. You can find a good page about this process here.
If you followed the guide Dreamhost 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 (i.e. “gem install activesupport-2.1.0.gem”). This means installing dependencies in turn (activesupport, markaby, and metaid before camping).
Notes for MediaTemple Grid Service
The (mt) grid supposedly uses FastCGI internally for php instances, but the recipe above doesn’t work for it.
CGI works great though! The first request will take about one second while the grid warms up and loads ruby and all that stuff, following requests are blazingly fast. This is probably your local cluster unit caching a copy of ruby and camping from the network storage making it go quick. The server also cools off rather quickly however.
To get rubygems working from apache cgi scripts (i.e. outside the rails container) use the following snippet, replacing ##### with the right number for your account. You can find the right number in the Server Guide > System Paths section of the account center, or by running the pwd command in ssh:

$: << ‘/home/#####/data/rubygems/local/lib/site_ruby/1.8’;
ENV[‘GEM_PATH’] = ENV[‘GEM_HOME’] = ‘/home/#####/data/rubygems/gems’
Deploying with Phusion Passenger (mod_rails)
Phusion Passenger is capable of running any Rack app, so if your host supports Rails sites through Passenger, you can use that to deploy your Camping app. The following instructions are specific to Dreamhost, but the basic idea is applicable wherever Passenger is supported.

Check out the latest Camping from git, and put it somewhere on your server.
Put all your app’s files in a directory on your server, and make a public/ directory within that. (Passenger expects to be pointed at a Rails-style public directory. You can serve static files from that directory too, if you like, or it can just be empty.)
Put a config.ru file in your app’s directory. On Dreamhost, it should look something like this:
$:.unshift(‘/path/to/camping/lib’) # unless you built and installed Camping 2.0 as a gem
ENV[‘GEM_PATH’] = ‘/path/to/your/gems:/usr/lib/ruby/gems/1.8’
ENV[‘GEM_HOME’] = ‘/path/to/your/gems’

require ‘rubygems’
require ‘camping’
require ‘your_app’

YourApp.create if YourApp.respond_to? :create

run YourApp
In the Dreamhost web panel, go to “Manage domains”, and edit the domain or subdomain you want to use for this app.
Check the “Ruby on Rails Passenger (mod_rails)” box, and set the web directory to the path to your app’s public directory.
Save your settings, and check back in a couple minutes – your app should be live at the domain or subdomain you specified. (You may have to install Rack or some other gems to your local repository, if you don’t already have them.)
I don’t know how to deploy to a subdirectory on Dreamhost. It’s apparently easy to do with Passenger, but that functionality isn’t exposed through the web panel. I think you could probably make a symlink from /home/yourname/yoursite.com/yourapp to /home/yourname/yourapp/public, and then ask Dreamhost support to set up Passenger for that path. See section 3.2 of the Passenger user’s guide for details.

Clone this wiki locally