FastCGI not working with Rails 4 shared hosting (Phusion Passenger)
Asked Answered
E

1

10

I am using Rails 4.1.6 and ruby 2.1.8p440.

Currently, I want to speed up performance and add configure FastCGI.

Problem: FastCGI doesn't work and slows down my RoR APP.

So far I have this directory structure:

  • blogs (Rails 4 app)
  • public_html
    • .htaccess
    • dispatch.fcgi

My .htaccess file:

PassengerAppRoot "/home/individualki/blogs"
PassengerBaseURI "/"
PassengerRuby "/home/individualki/rubyvenv/ror/2.1/bin/ruby"
RailsEnv development

SetEnv RAILS_RELATIVE_URL_ROOT /dispatch

RewriteEngine On

RewriteRule ^(stylesheets/.*)$ - [L]
RewriteRule ^(javascripts/.*)$ - [L]
RewriteRule ^(images/.*)$ - [L]

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]

My dispatch.fcgi file:

#!/home/individualki/rubyvenv/ror/2.1/bin/ruby



ENV['RAILS_ENV'] ||= 'development'  
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 '../home/individualki/blogs/config/boot.rb'  
require '../home/individualki/blogs/config/environment.rb'

class Rack::PathInfoRewriter  
    def initialize(app)
        @app = app
    end

    def cadsll(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(blogs::Application)  

After that I restarted server with

touch tmp/restart.txt

Then accessed my web app and realized that it is not working properly. Content is loaded but without any assets - images, css or js files. App have become much slower.

After that I checked my Cpanel and saw that my app now uses maximum proceses 40/40 and CPU usage is up. Those things never happened before (at leat I didn't saw it)

enter image description here

My log file:

Started GET "/dispatch/assets/parsley.js?body=1" for 85.254.76.76 at 2016-09-24 12:57:06 +0300
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (75.0ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.6ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (367.4ms)

ActionController::RoutingError (No route matches [GET] "/dispatch/assets/parsley.js"):
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.3) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.3) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  /opt/passenger/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /opt/passenger/lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'


  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (44.4ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.4ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (282.0ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (25.0ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.6ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (135.4ms)


Started GET "/dispatch/assets/application.js?body=1" for 85.254.76.76 at 2016-09-24 12:57:07 +0300

ActionController::RoutingError (No route matches [GET] "/dispatch/assets/application.js"):
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.3) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.3) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  /opt/passenger/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /opt/passenger/lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'


  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (30.7ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.4ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (147.0ms)


Started GET "/dispatch/assets/co.js?body=1" for 85.254.76.76 at 2016-09-24 12:57:07 +0300

ActionController::RoutingError (No route matches [GET] "/dispatch/assets/co.js"):
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.3) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.3) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  /opt/passenger/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /opt/passenger/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /opt/passenger/lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'


  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (31.6ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.6ms)
  Rendered /home/individualki/rubyvenv/ror/2.1/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (150.2ms)

Thanks in advance for any help.

Ethelstan answered 24/9, 2016 at 10:2 Comment(0)
D
0

The slowness is likely a result of being run in development mode, and from all the asset 404s.

Assuming you got your configs from here: http://danielstrunk.me/blog/2014/07/01/configuring-rails-projects-fastcgi-dreamhost-shared-hosting/

It looks like your paths might not be configured correctly. Here's what the article says about it:

Here’s where things tripped me up a bit. Dreamhost doesn’t allow webrick to run on shared hosting accounts, so I had to figure out an alternative. Luckily, it’s pretty easy to get setup once you know what to look for. In the root directory of the domain you’re looking to install Redmine / your Rails app into, create a public folder. You’ll be creating two files here: dispatch.fcgi and .htaccess. Here’s what your dispatch.fcgi should look like:

dispatch.fcgi

#!/usr/bin/ruby

# Dreamhost clears environment variables when calling dispatch.fcgi, set again
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 '../your/app/config/boot.rb'
require '../your/app/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(YourApp::Application)

.htaccess

<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"
Darmit answered 2/7, 2017 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.