Search code examples
ruby-on-railsrmagickdreamhostfastcgi

rails 3.2.8 ruby 1.9.3 on DreamHost shared hosting fcgi RMagic issue


Using the following guide

http://spontaneousderivation.com/2012/09/30/rails-3-2-on-a-shared-dreamhost-server/

I managed to get my Rails 3.2.8 application with Ruby 1.9.3 up and running on Dreamhost shared hosting using fcgi (i know it is not the best solution, but I don't want to downgrade my applications to 3.0 and I lack skill and money for a VPS ).

Following the guides on DH wiki:

http://wiki.dreamhost.com/RMagick

http://wiki.dreamhost.com/Image_Magick#Compiling_ImageMagick_on_your_DreamHost_account

I installed my own version of ImageMagic and RMagic as a local gem.

However when I try to run application with RMagic functionality I get the following error in error.log:

'Premature end of script headers: dispatch.fcgi'.

I found out it is occurring when I uncomment the following line in my uploader:

include CarrierWave::RMagick

Running dispatch.fcgi from shell does not report any errors.

I assume that the problem might be caused by LD_LIBRARY_PATH or other env variables (listed in DH wiki) but tried including them in my .bashrc, .bash_profile and dispatch.fcgi but without effect.


Solution

  • This solution is valid as of 3/7/2013. I just used it to deploy a new app to DH.

    In your gemfile...

    gem 'paperclip'
    

    Then run "bundle install" from your app directory.

    In your public/dispatch.fcgi...copy the code below and replace APPNAME with the name listed in your config/application.rb module, USER with your DH username, and RUBY with the specific ruby version listed in the output from "which ruby" on the command line on your DH ssh session.

    #!/home/USER/.rvm/rubies/RUBY/bin/ruby
    
    ENV['RAILS_ENV'] ||= 'production'
    ENV['HOME'] ||= `echo ~`.strip
    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.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(APPNAME::Application)
    

    No special processing is needed to get paperclip working assuming you have a valid install of RVM. Aside from the specifics I mentioned above (specifically the dispatch.fcgi is slightly changed), you can follow this guide, http://spontaneousderivation.com/2012/09/30/rails-3-2-on-a-shared-dreamhost-server/.