Rails 3

From DreamHost
Jump to: navigation, search

Contents

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.

Rack 1.2.1

Once your site is up and running, you may still see errors like the following:

You have already activated rack 1.2.1, but your Gemfile requires rack 1.2.2

There's a really simple fix for this problem. It's occurring because your application is trying to load up a version of Rack that does not match up with the system's Rack. Just add the following line somewhere in your project's Gemfile and re-bundle it:

gem 'rack', '1.2.1'

If you happen to have any other 'rack' lines, remove those as well.

You can always re-bundle directly on the server from your application directory.

$ bundle install --deployment

That should straighten things right up!

Have fun!

Rails 3.1.x

Passenger fails to start with error message:

You have already activated rack 1.2.1, but your Gemfile requires rack 1.3.5. Consider using bundle exec

This is from Dreamhost support:

Unfortunately, Passenger loses environment variables.  At the moment the
only way to use something other than our server's provided rack will
require you to run your rails app in FastCGI mode (rather than
Passenger), or to have your own hosting VPS where you can change the
entire system's files.

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.

#!/usr/bin/ruby

require 'rubygems'
require 'fcgi'

ENV['RAILS_ENV'] ||= 'production' 

# Set GEM_PATH and GEM_HOME ("user" is your dreamhost user)
ENV['GEM_HOME'] ||= '/home/user/.gems'
require 'rubygems'
Gem.clear_paths

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

Two modifications need to be made to the above file. First, you must specify the path to your .gems directory, which is usually /home/<username>/.gems. So, that changes this line:

ENV['GEM_HOME'] ||= '/home/user/.gems'

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

ENV['GEM_PATH'] = '/usr/lib/ruby/gems/1.8'; ENV['GEM_HOME'] = "#{ENV['HOME']}/.gems";

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

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 dispatcher.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.

Personal tools