home / blog

Rails 4.0.0 with Ruby 2.0 on a Dreamhost shared server

Build ruby from source. RVM won’t work out of the box because it tries to use sudo. It’s quite easy.

cd
mkdir build
cd build

http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz

tar xzf ruby-2.0.0-p247.tar.gz
cd ruby-2.0.0-p247
 ./configure --prefix /home/adamish/ruby   # <=== make sure you install to your own home directory
make install

Make your custom ruby default. Add the following to your ~/.bash_profile

export GEM_HOME="$HOME/.gems"
export GEM_PATH="$GEM_HOME"
export PATH=~/ruby/bin:$PATH

Install rails

gem install rails --no-document

Create dummy site for testing

rails new HorseApp
mv HorseApp/* . # move to top level

Add following lines to Gemfile

# required for JS runtime
gem 'therubyracer'
# required for FCGI wrapper
gem 'fcgi'

Install the new gems

bundle install

Create FCGI wrapper script – public/dispatch.fcgi

#!/home/adamish/ruby/bin/ruby

ENV['RAILS_ENV'] = 'development'
ENV['HOME'] ||= `echo ~`.strip
ENV['GEM_HOME'] = File.expand_path('~/.gems')
ENV['GEM_PATH'] = File.expand_path('~/.gems')

require 'fcgi' 
require File.join(File.dirname(__FILE__), '../config/environment.rb')

class Rack::PathInfoRewriter
  def initialize(app)
    @app = app
  end
  def call(env)
    env.delete('SCRIPT_NAME')
    parts = env['REQUEST_URI'].split('?')
    env['PATH_INFO'] = parts[0]
    env['QUERY_STRING'] = parts[1].to_s
    @app.call(env)
  end
end
Rack::Handler::FastCGI.run  Rack::PathInfoRewriter.new(HorseApp::Application) 

Add following to .htaccess

<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi
</IfModule>
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
</IfModule>

Options +FollowSymLinks +ExecCGI 

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/dispatch.fcgi/$1 [QSA,L] 
 
ErrorDocument 500 "Rails application failed to start properly"

Try it out! You should see the standard “Welcome aboard – You’re riding Ruby on Rails!” development starter page.

This entry was posted in geek and tagged , . Bookmark the permalink.

One Response to Rails 4.0.0 with Ruby 2.0 on a Dreamhost shared server

  1. rafaeldx7 says:

    I need to change this

    RewriteRule ^(.*)$ public/dispatch.fcgi/$1 [QSA,L]

    for that

    RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]

    to work

Leave a Reply

Your email address will not be published.


8 × seven =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>