Rails 3

From DreamHost
Jump to: navigation, search

Troubleshooting

Having trouble getting a Rails 3 site up and running? Here's some troubleshooting tips to help you get on the right track.

Have RVM? It's not going to fly with Passenger. The following will quickly get you back on track for the rest of the troubleshooting:

 rvm use system
 export GEM_PATH=/usr/lib/ruby/gems/1.8
 export GEM_HOME=~/.gems

So now you're ready to start troubleshooting. Change your path to include the server gem binaries as well as your own local gems.

 export PATH=~/.gems/bin:/usr/lib/ruby/gems/1.8/bin:$PATH

Now you can actually get around to finding out what's been failing with your site that Passenger may not be telling you. Enter your Rails directory and run a 'rails console' with Bundler.

$ cd yourdomain.com # probably yourproject/current for Capistrano
$ bundle exec rails console

If the console loads up without a problem, then congratulations! It looks like you have all the dependencies for your Rails application, and you can move ahead to troubleshooting your Rails application itself!

However, if it does print out an error, it's more than likely missing a gem. If that's the case, you just need to install the missing gem. For example...

$ gem install mysql2 -v 0.3.7

Re-run the 'rails console' command and continue until you have all of the gems installed.

If your application is still having any trouble loading at this point, the following command should bundle up all of the required gems directly into your Rails application for the web server:

$ bundle install --deployment

There's one more thing you'll want to make sure you have set up for your Rails application.

Using FastCGI

Passenger is the preferred method of running Rails application at Dreamhost, but perhaps you are trying to run something that will not work with Dreamhost's Passenger install. Don't worry though, you're still in luck!

You can run your application with FastCGI! While this is not recommended, it may be the only way to run your application (on shared).

Common Instructions

1. Make sure that FastCGI is enabled on your domain.

To do that, go to the Domains > Manage Domains section of your panel and then click the "Edit" link next to your domain. On the next page make sure that under the Fully Hosted section the "PHP mode" is set to "PHP 5.x.x FastCGI". The next option you will need to verify is that Passenger is NOT enabled. If you had to make any changes you will need to press the Save Changes button.

2. From your shell account, go to the home directory, type:

rails new nameofyourrailsproject

If you want to have a rails application at top-level of your domain, go to the control panel and set the folder of that domain to "nameofyourrailsproject/public"

Rails 3 no longer creates a dispatch.fcgi when creating an application skeleton. Here is a generic dispatch you can use for ruby 1.8.

#!/usr/bin/ruby

# Dreamhost clears environment variables when calling dispatch.fcgi, so set them here 
ENV['RAILS_ENV'] ||= 'production'
ENV['HOME'] ||= `echo ~`.strip
ENV['GEM_HOME'] = File.expand_path('~/.gems')
ENV['GEM_PATH'] = File.expand_path('~/.gems') + ":" + '/usr/lib/ruby/gems/1.8'

require 'rubygems'
Gem.clear_paths
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(X::Application)  # REPLACE X WITH YOUR APPLICATION'S NAME (found in config/application.rb)

You will need to specify your ruby app name on the last line by replacing 'X::Application' with '<App name>::Application'

You can install RVM to use other Ruby versions. You must change the paths in your dispatch.fcgi. Here is an example using RVM Ruby 1.9.3

#!/home/username/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

ENV['RAILS_ENV'] ||= 'production'
ENV['HOME'] ||= `echo ~`.strip
ENV['GEM_HOME'] = File.expand_path('~/.rvm/gems/ruby-1.9.3-p194')
ENV['GEM_PATH'] = File.expand_path('~/.rvm/gems/ruby-1.9.3-p194') + ":" +
    File.expand_path('~/.rvm/gems/ruby-1.9.3-p194@global')

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(X::Application) # REPLACE X WITH YOUR APPLICATION'S NAME (found in config/application.rb)

You will need to specify your ruby app name on the last line by replacing 'X::Application' with '<App name>::Application'

Rather then adding the GEM_HOME here, you can add these variables to your config/environment.rb file.

ENV['HOME'] ||= `echo ~`.strip
ENV['GEM_PATH'] = File.expand_path('~/.gems') + ":" + '/usr/lib/ruby/gems/1.8'
ENV['GEM_HOME'] = File.expand_path('~/.gems')

Next, make sure that your fcgi script is executable

chmod +x X/public/dispatch.fcgi

This is paired with a .htaccess file to forward requests to the dispatch.fcgi.

<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"

The dispatch.fcgi and .htaccess file both need to be placed in your application's /public folder.

5. Visit Your App!

No need to start an extra server like webrick or mongrel (which are not allowed on our shared servers), you can check out your site now by visiting your domain with a web browser.

Troubleshooting

1. Application Failed to Start

This is a very generic error message, so the best way to figure out what's going on is to run your dispatch file manually through the shell:

cd public/
./dispatch.fcgi

If running dispatch.fcgi from the command line results in

undefined method `downcase' for nil:NilClass

in your log/production.log file try running:

REQUEST_METHOD=GET ./dispatch.fcgi

or possibly:

export REQUEST_METHOD=GET
ruby --debug dispatch.fcgi

If you see the HTML for a 500 Error that's usually OK, but you may see a Rails error trace instead. That'll usually point to a syntax error or other goof in the code. But if the dispatch file runs without any output you'll need to check your app's logs in the log/ directory to see what's causing the problem.

tail -50 log/production.log
tail -50 log/development.log
tail -50 log/fastcgi.crash.log

Note: It is strongly discouraged to run your Rails app in Development mode and FastCGI. There's some pretty wicked memory leaks that can result.

If you see the following error when you run your dispatch file, it means that your dispatch file isn't using Unix-friendly line endings:

: bad interpreter: No such file or directory

You can fix that by opening the file in a text editor like vi or nano and then saving it in Unix format, or using the dos2unix command to convert the file like so:

dos2unix public/dispatch.fcgi

Other error messages are usually self-explanatory. And in the absolute worst-case scenario, you'll at least have something to toss at the fine folks that work for DreamHost Tech Support. You can contact them at any time in the Support > Contact Support section of your panel.