Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring a Rails 4 app for production in a subdirectory under Apache

There are three major issues I'm struggling with, I appreciate any help for any of them.

1) How do I configure the Rails app to have myurl.com/myapp/ as the root?

I tried in routes.rb:

scope '/myapp' || '/' do
    # all resources and routes go here
    root :to => "papers#index"

    resources :papers
end 

and in environment.rb, i added this to the top

ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"

This nearly works, except that rake routes doesn't print any route for "/", and GET myurl.com/myapp/ produces ActionController::RoutingError (No route matches [GET] "/")

2) What do it need to tell apache?

The provider of my shared server suggest to put this to ~/html/.htaccess

RewriteEngine on
RewriteRule ^myapp/(.*)$ /fcgi-bin/rails4/$1 [QSA,L]

with /fcgi-bin/rails4 being

#!/bin/sh

# This is needed to find gems installed with --user-install
export HOME=/home/kadrian

# Include our profile to include the right RUBY
. $HOME/.bash_profile

# This makes Rails/Rack think we're running under FastCGI. WTF?!
# See ~/.gem/ruby/1.9.1/gems/rack-1.2.1/lib/rack/handler.rb
export PHP_FCGI_CHILDREN=1

# Get into the project directory and start the Rails server
cd $HOME/rails4
exec bundle exec rails server -e production

When I click any link on the site, the browser url changes e.g. to myurl.com/fcgi-bin/rails4/papers/1, where it should be myurl.com/myapp/papers/1. How can I prevent that?

3) How to get the assets working

I feel like this will be solved somehow together with 1) and 2). However, right now, the app tries to do:

GET myurl.com/assets/application-5e86fb668d97d38d6994ac8e9c45d45e.css

which procudes a 404 Not Found. The assets should be under the subdirectoy too, right? How do I tell rails to put / find them there?

like image 214
kadrian Avatar asked Oct 22 '22 03:10

kadrian


1 Answers

There are a few assumptions I'm going to make in order to try and answer your question.

  1. I'm assuming there is a static site served at myurl.com and that you only want the Rails app to be served on myurl.com/myapp

  2. I'm assuming your shared hosting provider prefers FastCGI as a way to serve Rack apps (Rails)

Based on those assumptions, I believe that if you:

  1. Move your Rails app under the ~/html/myapp folder
  2. Add a .htaccess file to ~/html/myapp/public with contents:

    AddHandler fastcgi-script .fcgi

    Options +FollowSymLinks +ExecCGI 

    RewriteEngine On 

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L] 

    ErrorDocument 500 "Rails application failed to start properly"

  1. Add a dispatch.fcgi file to ~/html/myapp/public with contents:

    ENV['RAILS_ENV'] ||= 'production'
    ENV['GEM_HOME'] = File.expand_path('your_gem_home')

    require 'fcgi' 
    require File.join(File.dirname(__FILE__), '../config/environment.rb')

  1. chmod +x ~/html/myapp/public/dispatch.fcgi

the app should start just fine and route just fine...

I don't think you need to worry about setting config.action_controller.relative_url_root.

I would also install your gems using bundle install --deployment prior to deploying your app.

Finally, the reason you're not getting a root route is because there is no: root :to => 'controller#action' in your config/routes.rb

Hope this helps and if not, check out:

Dreamhost - Rails 3 and FastCGI and FastCGI setup which includes some hints about using ENV['RAILS_RELATIVE_URL_ROOT']

like image 194
Adrian CB Avatar answered Oct 27 '22 07:10

Adrian CB