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

17 thoughts on “Rails 3.2 on a Shared Dreamhost Server

  1. I followed this guide, but I can’t seem to get it to work. Ruby 1.9 gives me errors about having to be reinstalled, because I don’t have libyaml installed.

  2. I had to add a .htaccess file in the public director with something like this in it:

    AddHandler fastcgi-script fcgi
    RewriteEngine On
    RewriteBase /
    RewriteCond %{request_uri} !^/favicon.ico
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

  3. I followed this guide to the letter, and used the aforementioned .htaccess file, but I’m getting 500 errors. :( I know it’s gotta be something with the .htaccess file…I literally copied and pasted the contents. Before I had the htaccess file, I was only getting directory index errors. The dispatch.fcgi does not error out. I also tried the .htaccess file on Dreamhost’s wiki, but nothing. :(

  4. I can’t even install ruby-1.9.3…

    ruby-1.9.3-p392 – #compiling…………………………………………………………….
    Error running ‘make -j8’,
    please read /home/thejase/.rvm/log/ruby-1.9.3-p392/make.log
    There has been an error while running make. Halting the installation.
    [miram]$

  5. For me, I was not able to install ruby 1.9.3 as it was prompting me for a sudo password (which I don’t have). The curl command for RVM is currently installing 1.19.0. I was able to make this work only by installing a later version (1.13.0) using this command.

    \curl -L https://get.rvm.io | bash -s — –version 1.13.0

  6. For me, I was not able to run the “rvm install 1.9.3” command as it was prompting me for a sudo password (which I don’t have). As of this writing, the curl command for RVM is installing 1.19.0. I was able to make this work only by installing a later version (1.13.0) using this command.

    \curl -L https://get.rvm.io | bash -s — –version 1.13.0

    After that, everything worked beautifully

  7. Using 1.19 or 1.13 my shared dreamhost is saying I’m missing the following libs: libreadline6-dev, libyaml-dev, automake, libtool, libffi-dev
    How do you get around this?

  8. Thanks for this, it’s awesome!

    For those wondering why this isn’t working more recently, the problem is the new autolibs stuff in rvm. But you don’t need to revert to an earlier version or hack anything, you can just turn it off! When installing, use the following:
    \curl -L https://get.rvm.io | bash -s — –autolibs=read-fail

    Then, to install rubies, you need to disable autolibs with:
    rvm autolibs disable

    That should see you through.

  9. For those having problems, the newest version of rvm comes with something called “autolibs”, which tries to install packages and needs to be turned off. When installing rvm, use:
    \curl -L https://get.rvm.io | bash -s — –autolibs=read-fail

    And then disable autolibs for ruby installs with:
    rvm autolibs disable

Comments are closed.