Search code examples
ruby-on-railsdebuggingdreamhostfastcgi

Debugging a rails app deployment to Dreamhost with fcgi


I installed ruby 2.1.2 and rails 4.1.5 on a shared dreamhost server. I succesfully managed to deploy a bare-bones application with fcgi by following this guide. I then tried to deploy an application that I had previously developed by using capistrano 3. cap production deploy works fine, and so does rails console production.

The problem is that whenever I try to go to my application's url, I get a 500 error, which only says "Rails application failed to start properly". I tried following this other guide for troubleshooting, but I didn't get very far.

As far as I can tell, my public/dispatch.fcgi file seems to be working fine:

#!/home/myuser/.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(MyApp::Application)

I tried running the file itself and I only get a prompt with no errors (which seems to imply that the app is fine).

My log/production.log file is empty. My server logs only show several lines with the unhelpful message [Mon Sep 01 17:45:14 2014] [error] [client 201.246.73.121] Premature end of script headers: dispatch.fcgi, which at least tells me that dispatch.fcgi is being called. Googling only told me that I might be missing the fcgi gem, but that is not the case:

$ bundle show fcgi
/home/myuser/.gems/gems/fcgi-0.9.2.1

Just in case, here's my Gemfile, excluding the test and production environmens:

source 'https://rubygems.org'
ruby '2.1.2'
gem 'therubyracer'
gem 'fcgi'
gem 'mysql'
gem 'rails', '4.1.2'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'spring',        group: :development
gem 'bootstrap-sass'
gem 'high_voltage'
gem 'slim-rails'
gem 'savon', '~> 2.5.1'
gem 'spreadsheet', '~> 0.9.7'

Any ideas on how to debug this? Thanks


Solution

  • I had this problem today - that it's difficult to understand what's going wrong with dispatch.fcgi. Errors are obscured behind "Rails application failed to start properly" in your browser and "Premature end of script headers: dispatch.fcgi" in your logs.

    If you take this line in dispatch.fcgi:

    Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(MyApp::Application)
    

    ...and replace it with these lines:

    wrappedApp = Rack::Builder.new do
      use Rack::ShowExceptions
      use Rack::PathInfoRewriter
      run MyApp::Application
    end
    Rack::Handler::FastCGI.run wrappedApp
    

    ...then you'll get descriptive error pages when you load a page from the app. This should enable you to figure out what the actual problem is.