Results 1 to 5 of 5

Thread: Sinatra Howto

  1. #1

    Default Sinatra Howto

    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

    Code:
    $ 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

    Code:
    $ 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.

    Code:
    /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.

    Code:
    $ cd ~/etc
    $ mkdir -p sinatra/myapp
    4. Setup .htaccess

    Paste this file into: ~/etc/sinatra/myapp/.htaccess

    Set permissions:
    Code:
    chmod 644 ~/etc/sinatra/myapp/.htaccess
    Code:
    # 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.p...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:
    Code:
    chmod 755 ~/etc/sinatra/myapp/dispatch.fcgi
    Edit the path to your gem dir as recorded in step #2.

    Code:
    #!/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.

    Code:
    $ 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

  2. #2
    Join Date
    Nov 2009
    Posts
    1

    Question

    I don't understand the line:

    Code:
    ln -s ~/etc/sinatra/myapp mydomain
    What info do I write instead of mydomain? Let's assume I want the action to happen in an add-on domain such as "public_html/foo".

    Sonja

  3. #3
    Join Date
    Nov 2009
    Posts
    2

    Default

    I get this error

    Code:
    user@domain.com [~/public_html]# gem install sinatra
    Successfully installed sinatra-0.9.4
    1 gem installed
    Installing ri documentation for sinatra-0.9.4...
    Installing RDoc documentation for sinatra-0.9.4...
    
    user@domain.com [~/public_html]# gem install rack
    Successfully installed rack-1.0.1
    1 gem installed
    Installing ri documentation for rack-1.0.1...
    Installing RDoc documentation for rack-1.0.1...
    
    user@domain.com [~/public_html]# irb
    irb(main):001:0> require 'rubygems'
    => true
    irb(main):002:0> require 'sinatra'
    LoadError: no such file to load -- sinatra
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    	from (irb):2

  4. #4
    Join Date
    Nov 2009
    Posts
    3

    Default

    Quote Originally Posted by AbeEstrada View Post
    I get this error

    Code:
    user@domain.com [~/public_html]# gem install sinatra
    Successfully installed sinatra-0.9.4
    1 gem installed
    Installing ri documentation for sinatra-0.9.4...
    Installing RDoc documentation for sinatra-0.9.4...
    
    user@domain.com [~/public_html]# gem install rack
    Successfully installed rack-1.0.1
    1 gem installed
    Installing ri documentation for rack-1.0.1...
    Installing RDoc documentation for rack-1.0.1...
    
    user@domain.com [~/public_html]# irb
    irb(main):001:0> require 'rubygems'
    => true
    irb(main):002:0> require 'sinatra'
    LoadError: no such file to load -- sinatra
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    	from (irb):2
    Um, what does that mean? Anybody else try the tutorial?
    Last edited by roso; 11-23-2009 at 03:38 PM.

  5. #5
    Join Date
    Jun 2011
    Posts
    1

    Default

    Quote Originally Posted by AbeEstrada View Post
    I get this error

    Code:
    user@domain.com [~/public_html]# gem install sinatra
    Successfully installed sinatra-0.9.4
    1 gem installed
    Installing ri documentation for sinatra-0.9.4...
    Installing RDoc documentation for sinatra-0.9.4...
    
    user@domain.com [~/public_html]# gem install rack
    Successfully installed rack-1.0.1
    1 gem installed
    Installing ri documentation for rack-1.0.1...
    Installing RDoc documentation for rack-1.0.1...
    
    user@domain.com [~/public_html]# irb
    irb(main):001:0> require 'rubygems'
    => true
    irb(main):002:0> require 'sinatra'
    LoadError: no such file to load -- sinatra
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    	from (irb):2
    Judging by the error output from irb, it looks like you are running the system irb which is probably requiring the system gems, not the local gems in your user directory ( ~/.gem/). Have you modified your ~/.bash_profile or ~/.bashrc to accept a local gem path ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •