Running Rails 4 on Dreamhost shared hosting

2.3k views Asked by At

Ruby on Rails is fantastic, but getting up and running with it can be a frustrating experience, especially for a new developer. I happen to have a book that does Rails 4, but Dreamhost shared hosting supports Rails 3.2. Making web apps and not being able to deploy them to the hosting I already pay for makes me sad, but learning the differences between 3.2 and 4 while actually learning the language is no fun.

4 > 3.2, so it seems like getting Rails 4 up and running on Dreamhost shared hosting would be the way to go. Turns out there are instructions online for doing this, I've run into a multitude of problems trying to follow them. Working on the command line logged into a remote server is hard in part because you'll get error messages that, when Googled, don't produce the answer you're likely to need, or assume knowledge you don't have.

How can I get this to work so I can get on with learning Rails?

2

There are 2 answers

0
Alesh Houdek On
  1. Log into SSH: The first thing you need to do is log into your Dreamhost server on the command line. Open Terminal (on the Mac) and type ssh -l [username] [hostname]. Username here is not the username you log into your web panel with, it's the username you see under "Manage Users" in the web panel. You may need to switch that user account to "Shell user." (You will also need the password for this account.) Hostname is the url of your website. I type ssh -l alesh alesh.com.
  2. Download Ruby: You're going to be downloading and installing lots of stuff and you'll want a temporary directory to work in, so follow Adam's instructions and type

    cd
    mkdir build
    cd build
    

    Next you're supposed to type http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz, but you'll get a No such file or directory error. You need to use wget, and you may as well go and get the latest version of Ruby. Or, in my case, I wanted version 1.9.3 to match what I'm doing on my local machine. Point your web browser to http://cache.ruby-lang.org/pub/ruby/ and select a version. I'm not sure what the best strategy is for picking, but I do know that 1.9.3 and 2.0.0 both work with Rails 4. Copy the link for the file, ending in .tar.gz, and paste it into terminal. You'll end up with something like this:

    wget http://cache.ruby-lang.org/pub/ruby/ruby-1.9.3-p551.tar.gz
    

    At this point when I tried going on and compiling Ruby I got error messages and the whole thing fell apart. You can skip the next step and return to it if you have trouble down the line, but you'll have to reinstall Ruby.

  3. Download and install YAML: Ruby wants YAML. Unfortunately, the instructions I found for installing YAML also didn't work, for the same reason we have so much trouble getting Ruby working: we don't have full access to sudo on our shared Dreamhost server. But this should work:

    wget http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
    tar xzf yaml-0.1.5.tar.gz
    cd yaml-0.1.5
    ./configure --prefix /home/alesh/ruby
    make
    make install
    

    (remember to substitute your home director or user name between /home/ and /ruby. A whole bunch of stuff will scroll by, but you should not see any terrible error messages.

  4. Install Ruby: Now Ruby should install without errors. Substitute whatever version you downloaded in these instructions. Again, some of these steps will take quite a while to run:

    cd ..
    tar xzf ruby-1.9.3-p551.tar.gz
    cd ruby-1.9.3-p551
    ./configure --prefix /home/alesh/ruby
    make install
    

    Again, you should not see any major error message.

  5. Edit your bash profile: You can do this from the command line, but it's easier to open an FTP client to your Dreamhost home directory, and edit the file .bash_profile. You may need to enable show hidden files. add these lines to the end and save it:

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

    Totally optional, but while you're in here you can set a variable to modify your command prompt. I added the line PS1=' \w$ ', which sets my prompt to be the directory I'm in, plus adds four leading spaces so I can easily find my commands if I need to scroll back through the terminal output. The possibilities are many.

  6. Install Rails: gem install rails --no-document is supposed to be the next step, but that gave me an error message too. I think that may be because of Ruby 1.9.x, in which case gem install rails --no-ri --no-rdoc should work. I ended up getting it to work with

    gem install rails
    

    This will install the latest version of Rails. I believe you can specify a version like this: gem install rails:4.0.0.

Update: That part seems to be successful. I can run Rails from the command line and it reports that it's version 4.2.1. When I create a project and test-run it, following the rest of Adam's instructions, I get Rails application failed to start properly. But I'm still working on it. Will report back.

http://alesh.com/rails/demo/

That's as far as I've gotten for now! Will edit this answer when I can. In the meantime, see Adam's blog post for how to proceed, and please add corrections, suggestions, and questions here.

0
Jem On

I found those instructions as well. They, like most other documentation for rails on Dreamhost, are somewhat... outdated.

I finally got my app online and I posted all of the following to codepen, but I really want to share what I did since it took me weeks to figure it all out.

This is a comprehensive list of what I did to make Ruby 2.2.1p85 and Rails 4.2.4 work on a Dreamhost shared server. This is done by starting with a brand new hosted domain from scratch. This is NOT to be used with a VPS using Passenger.

First, set up the dreamhost web hosting by adding a new user and giving that user shell access. You also need to uncheck the disallow FTP (user should be able to use FTP).

The Web Directory will be /home/username/<domain-name>/public

PHP mode (currently) PHP 5.6 FastCGI(default)

Check Extra Web Security

Leave Passenger UNCHECKED

Using the instructions I found here: http://adamish.com/blog/archives/775 I did the following:

Go to the website http://cache.ruby-lang.org/pub/ruby/ and find the version of ruby you want to use with the extension *.tar.gz and download it to your system. Use FTP (such as filezilla) to upload that file to your server in a new folder named build.

Log into your shell account by going to terminal and typing ssh <username>@<domain-name> This will prompt for your password. Enter it and go to your build folder.

Type: tar xzf <your-ruby-filename.tar.gz>

type: cd <your-uncompressed-ruby-folder>

type: ./configure --prefix /home/<username>/ruby

type: make install

When that is complete (and it takes a while) go to your home directory by typing cd ~ and opening your .bash_profile (I used vim, nano should work, too).

Add the following to .bash_profile:

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

Now you can install rails by typing: gem install rails -v <Rails-version-number>

On your personal machine, I recommend clearing your cache before uploading. You don't have to, but it will save some time. You can do this by typing rake tmp:cache:clear

You can now upload your rails application either by FTP or git, but make sure you are in the directory /home/<username>/<domain-name>

When all of your files are uploaded, first access your Gemfile via nano or vim.

Uncomment the gem 'therubyracer' line and add gem 'fcgi'. Save it and type bundle install.

Navigate to your <domain-name>/public folder and create two new files, .htaccess and dispatch.fcgi

your .htaccess file should look like this:

<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 ^(.*)$ dispatch.fcgi/$1 [QSA,L]

ErrorDocument 500 "Rails application failed to start properly."

and your dispatch.fcgi file like this:

#!/home/<username>/ruby/bin/ruby
ENV['RAILS_ENV'] = 'production'
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(<application-name>::Application)

Note: application-name may be different than your domain-name.

This is what worked for me. I'm still a new to Rails and haven't used any sort of database with this. I urge anyone with concerns about this setup to comment or edit this post, as there is no good current documentation for using modern Ruby on Rails with a Dreamhost shared server.

UPDATE

While this method works and gets everything talking, as soon as I set up a robust application it was getting killed by their procwatch. Their support told me I was using too much RAM, but wouldn't tell me how much I was using or what their limit was. I've since switched hosting services and have had no problems.