budding_mechanic
04-15-2009, 11:15 AM
I figured out how to run the Sinatra ruby gem on Bluehost. Although I'm using Rails, I've documented this for anyone else.
ASSumptions:
* You have your own [sub]domain and know how to point it from the
public_html directory to another directory in your account.
* You have ssh access.
* No Ruby or Gem environment variables are set. If you've setup
GEM_HOME elsewhere, that's fine, just adjust paths accordingly.
1. Install the Sinatra ruby gem
$ gem install sinatra
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/XXXXXXX/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
Successfully installed rack-0.9.1
Successfully installed sinatra-0.9.1.1
2 gems installed
Installing ri documentation for rack-0.9.1...
Installing ri documentation for sinatra-0.9.1.1...
Installing RDoc documentation for rack-0.9.1...
Installing RDoc documentation for sinatra-0.9.1.1...
2. Verify installation
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'sinatra'
=> true
irb(main):003:0> Gem.path
=> ["/home/XXXXXXXX/.gem/ruby/1.8", "/usr/lib/ruby/gems/1.8"]
Make note of the Gem.path array that lists your local gem directory. Eg.
/home/XXXXXXXX/.gem/ruby/1.8
3. Create Sinatra directory
You probably don't want to put your Sinatra directory in ~/public_html...
Bluehost seems to put Rails apps in ~/etc/rails_apps.
So let's use ~/etc/sinatra/myapp.
$ cd ~/etc
$ mkdir -p sinatra/myapp
4. Setup .htaccess
Paste this file into: ~/etc/sinatra/myapp/.htaccess
Set permissions:
chmod 644 ~/etc/sinatra/myapp/.htaccess
# General Apache options
AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
#Options +FollowSymLinks +ExecCGI
# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
# RewriteCond %{REQUEST_URI} ^/notrails.*
# RewriteRule .* - [L]
# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On
# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
# Alias /myrailsapp /path/to/myrailsapp/public
# RewriteBase /myrailsapp
RewriteBase /
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
# ErrorDocument 500 /500.html
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Ref:
http://helpdesk.bluehost.com/index.php/kb/article/000493
5. Setup dispatch.fcgi
This file is the bread and butter that starts your Sinatra app.
Paste this file into: ~/etc/sinatra/myapp/dispatch.fcgi
Set permissions:
chmod 755 ~/etc/sinatra/myapp/dispatch.fcgi
Edit the path to your gem dir as recorded in step #2.
#!/usr/bin/ruby
#
# Sample dispatch.fcgi to make Sinatra work on Bluehost
#
# http://www.sinatrarb.com/
#
require 'rubygems'
# *** CONFIGURE HERE ***
# Because the fcgi environment is sterile make sure to prepend
# local gem dir to front of gem path. Find gem dir with:
# $ ruby -e 'require "rubygems"; p Gem.path.shift'
Gem.path.unshift("/home/XXXXXXX/.gem/ruby/1.8")
# sinatra should load now
require 'sinatra'
module Rack
class Request
def path_info
@env["REDIRECT_URL"].to_s
end
def path_info=(s)
@env["REDIRECT_URL"] = s.to_s
end
end
end
# Define your Sinatra application here
class MyApp < Sinatra::Application
get '/hi' do
"Hello World!"
end
end
builder = Rack::Builder.new do
use Rack::ShowStatus
use Rack::ShowExceptions
map '/' do
run MyApp.new
end
end
Rack::Handler::FastCGI.run(builder)
6. Testing
Now you need to make the Sinatra directory live.
Symlink your domain to the Sinatra directory.
$ cd ~/public_html
$ ln -s ~/etc/sinatra/myapp mydomain
http://www.mydomain.com/hi (returns "hello world")
http://www.mydomain.com (returns Sinatra error message as no route exists)
Notes:
There might be a better way to do this last step; I've only linked a subdomain, never a top level domain.
Sinatra does have a directory layout that is similar to Rails, but I've ignored that for simplicity.
There's lots more info about Sinatra and Rack.
http://www.sinatrarb.com/
http://rack.rubyforge.org/
Don't use cPanel to setup Sinatra; use command line.
Good luck.
Keywords: sinatra rack ruby rails dispatch.fcgi
ASSumptions:
* You have your own [sub]domain and know how to point it from the
public_html directory to another directory in your account.
* You have ssh access.
* No Ruby or Gem environment variables are set. If you've setup
GEM_HOME elsewhere, that's fine, just adjust paths accordingly.
1. Install the Sinatra ruby gem
$ gem install sinatra
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/XXXXXXX/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
Successfully installed rack-0.9.1
Successfully installed sinatra-0.9.1.1
2 gems installed
Installing ri documentation for rack-0.9.1...
Installing ri documentation for sinatra-0.9.1.1...
Installing RDoc documentation for rack-0.9.1...
Installing RDoc documentation for sinatra-0.9.1.1...
2. Verify installation
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'sinatra'
=> true
irb(main):003:0> Gem.path
=> ["/home/XXXXXXXX/.gem/ruby/1.8", "/usr/lib/ruby/gems/1.8"]
Make note of the Gem.path array that lists your local gem directory. Eg.
/home/XXXXXXXX/.gem/ruby/1.8
3. Create Sinatra directory
You probably don't want to put your Sinatra directory in ~/public_html...
Bluehost seems to put Rails apps in ~/etc/rails_apps.
So let's use ~/etc/sinatra/myapp.
$ cd ~/etc
$ mkdir -p sinatra/myapp
4. Setup .htaccess
Paste this file into: ~/etc/sinatra/myapp/.htaccess
Set permissions:
chmod 644 ~/etc/sinatra/myapp/.htaccess
# General Apache options
AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
#Options +FollowSymLinks +ExecCGI
# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
# RewriteCond %{REQUEST_URI} ^/notrails.*
# RewriteRule .* - [L]
# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On
# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
# Alias /myrailsapp /path/to/myrailsapp/public
# RewriteBase /myrailsapp
RewriteBase /
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
# ErrorDocument 500 /500.html
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Ref:
http://helpdesk.bluehost.com/index.php/kb/article/000493
5. Setup dispatch.fcgi
This file is the bread and butter that starts your Sinatra app.
Paste this file into: ~/etc/sinatra/myapp/dispatch.fcgi
Set permissions:
chmod 755 ~/etc/sinatra/myapp/dispatch.fcgi
Edit the path to your gem dir as recorded in step #2.
#!/usr/bin/ruby
#
# Sample dispatch.fcgi to make Sinatra work on Bluehost
#
# http://www.sinatrarb.com/
#
require 'rubygems'
# *** CONFIGURE HERE ***
# Because the fcgi environment is sterile make sure to prepend
# local gem dir to front of gem path. Find gem dir with:
# $ ruby -e 'require "rubygems"; p Gem.path.shift'
Gem.path.unshift("/home/XXXXXXX/.gem/ruby/1.8")
# sinatra should load now
require 'sinatra'
module Rack
class Request
def path_info
@env["REDIRECT_URL"].to_s
end
def path_info=(s)
@env["REDIRECT_URL"] = s.to_s
end
end
end
# Define your Sinatra application here
class MyApp < Sinatra::Application
get '/hi' do
"Hello World!"
end
end
builder = Rack::Builder.new do
use Rack::ShowStatus
use Rack::ShowExceptions
map '/' do
run MyApp.new
end
end
Rack::Handler::FastCGI.run(builder)
6. Testing
Now you need to make the Sinatra directory live.
Symlink your domain to the Sinatra directory.
$ cd ~/public_html
$ ln -s ~/etc/sinatra/myapp mydomain
http://www.mydomain.com/hi (returns "hello world")
http://www.mydomain.com (returns Sinatra error message as no route exists)
Notes:
There might be a better way to do this last step; I've only linked a subdomain, never a top level domain.
Sinatra does have a directory layout that is similar to Rails, but I've ignored that for simplicity.
There's lots more info about Sinatra and Rack.
http://www.sinatrarb.com/
http://rack.rubyforge.org/
Don't use cPanel to setup Sinatra; use command line.
Good luck.
Keywords: sinatra rack ruby rails dispatch.fcgi