Rails 3.2 on a Shared Dreamhost Server

Introduction

Trying to get Rails 3.2 along with ruby 1.9.3 running on a shared Dreamhost server?

Need to get it running against your Dreamhost MySQL database?

Worry no more. I have gone through the steps to do so.

Note: this tutorial assumes that you have knowledge of the following:

  • You’re using a shared Dreamhost server (if you’re using VPS or dedicated, you don’t need this document because you have full control).
  • How to use the command line
  • You are not using csh or tcsh
  • How to configure the shell you’re running (the .*rc file)
  • Rails 3.2 (this is not a Rails tutorial!)
  • MySQL and getting it going on Dreamhost (this is not a MySQL or a MySQL on Dreamhost tutorial!)

Step 1: Install RVM

Here’s more information about rvm, but it’s basically a package manager for ruby. With it, and some special shell/CGI magic, you too can run ruby 1.9.2 (or even ruby 1.9.3) on a server that’s installed ruby 1.8.x for some reason.

Run the following command to install RVM:

curl -L https://get.rvm.io | bash -s stable --rails

Step 2: Add rvm to Your Path

Add the following line to your .bashrc if you’re using bash,
or to your .zshrc if you’re using zsh, etc etc etc.

export PATH=$HOME/.rvm/bin:$PATH # Add RVM to PATH for scripting
source ~/.rvm/scripts/rvm

Step 3: Install Ruby 1.9.3 as Your Default Ruby

Run the following commands:

rvm install ruby-1.9.3
rvm use default ruby-1.9.3

Now you can use gem like any sane person (don’t use sudo gem;
you want these gems installed locally).

Step 4: Install Rails 3.2 and Assorted Necessary Gems

Run the following commands:

gem install rails
gem install mysql2
gem install fcgi
gem install therubyracer

Step 5: Generate Your Rails App

Do it the usual way.

Step 6: Do Not Use Passenger

You’ll need to use FastCGI on a shared Dreamhost server.

Go to “Manage Domains” on your Dreamhost panel and choose “Edit” next to your domain.

Update the following:

  • Web directory should point to YourAppName/public (replacing YourAppName as appropriate).
  • Make sure the Passenger option is unchecked.

Hit Save.

Wait for the little clock next to your domain on the Manage Domains page to expire.

Step 7: Update Your Gemfile

Your Gemfile will need a few changes now.

Under the line gem 'sqlite3' add the following:

gem 'mysql2'
gem 'fcgi'

Under the line group :assets do uncomment the following:

  gem 'therubyracer', :platforms => :ruby

Step 8: Create dispatch.fcgi in public/

Please replace the following as appropriate.

  • USER (x2) = your user name
  • RUBY (x4) = full version of your ruby, like ruby-1.9.3-p194
  • APPNAME (x1) = app name as found in config/application.rb
#!/home/USER/.rvm/rubies/RUBY/bin/ruby

ENV['RAILS_ENV'] ||= 'production'
ENV['HOME'] ||= '/home/USER'
ENV['GEM_HOME'] = File.expand_path('~/.rvm/gems/RUBY')
ENV['GEM_PATH'] = File.expand_path('~/.rvm/gems/RUBY') + ":" +
    File.expand_path('~/.rvm/gems/RUBY@global')

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

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(APPNAME::Application)

DON’T FORGET TO MAKE IT EXECUTABLE: chmod 755 dispatch.fcgi

Step 9: Add a Welcome Page

You need to do this because FastCGI acts differently from Passenger when it comes
to certain paths, like / .

Run:

rails generate controller Welcome index

Then edit config/routes.rb and add the following route:

  root :to => 'welcome#index'

Step 10: Edit database.yml

Under production: use the following lines, replacing DATABASE, MYSQLHOST, USER, and PASSWORD as appropriate:

  adapter: mysql2
  encoding: utf8
  database: DATABASE
  username: USER
  password: PASSWORD
  host: MYSQLHOST

Step 11: Precompile Your Assets

If you’re getting core dumps when visiting your site, this is why.

Run:

rake assets:precompile

Step 12: Visit Your Site

It may take a while as Rails loads your site for the first time.

Now you should see the following:


<strong>Welcome#index</strong>

Find me in app/views/welcome/index.html.erb

And now you are done.

Advertisements