在Apache下配置Rails 4应用程序以在子目录下进行生产环境设置

5

我遇到了三个主要问题,非常感谢任何一个能提供帮助的人。

1)如何配置Rails应用程序以将myurl.com/myapp/作为根目录?

我在routes.rb中尝试过:

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

    resources :papers
end 

environment.rb中,我在顶部添加了以下内容。
ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"

这几乎可以工作,但是rake routes不会打印任何关于“/”的路由信息,而GET myurl.com/myapp/会产生ActionController :: RoutingError(没有匹配[GET]“ /”)

2)我需要告诉Apache什么?

我的共享服务器提供商建议将此放置在〜/html/.htaccess

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

使用/fcgi-bin/rails4

#!/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

当我在网站上点击任何链接时,浏览器的URL会改变,例如变成myurl.com/fcgi-bin/rails4/papers/1,而应该是myurl.com/myapp/papers/1。我该如何防止这种情况发生?
3)如何让资源正常工作
我感觉这个问题可能与1)和2)一起解决。然而,现在应用程序尝试执行以下操作:
GET myurl.com/assets/application-5e86fb668d97d38d6994ac8e9c45d45e.css

产生了一个404未找到的错误。这些资源也应该在子目录下,对吧?我该如何告诉Rails将它们放置/查找到那里?

1个回答

3
我会在回答您的问题之前先做一些假设:
  1. 我假设您有一个静态站点位于 myurl.com,并且您只想将Rails应用程序提供在 myurl.com/myapp

  2. 我假设您的共享主机提供商更喜欢使用FastCGI作为提供Rack应用程序(Rails)的方法

基于这些假设,如果您:
  1. 将您的Rails应用程序移动到 ~/html/myapp 文件夹下
  2. ~/html/myapp/public 中添加一个包含以下内容的 .htaccess 文件:
    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. ~/html/myapp/public 中添加一个包含以下内容的 dispatch.fcgi 文件:
    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
应用程序应该可以正常启动并正确路由...
我认为您不需要担心设置 config.action_controller.relative_url_root
在部署应用程序之前,我还会使用 bundle install --deployment 安装所需的gem。
最后,您无法获得根路由的原因是因为您的 config/routes.rb 文件中没有: root :to => 'controller#action' 希望这可以帮助您,如果仍有问题,请查看以下网址:

Dreamhost - Rails 3 and FastCGIFastCGI setup 这两个网页包含了有关使用 ENV['RAILS_RELATIVE_URL_ROOT'] 的一些提示。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接