在Apache下的子目录中配置Rails 4应用程序以进行生产

kad*_*ian 5 apache deployment ruby-on-rails subdirectory ruby-on-rails-4

我正在努力解决三个主要问题,我感谢任何帮助.

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

我试过routes.rb:

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

    resources :papers
end 
Run Code Online (Sandbox Code Playgroud)

environment.rb,我把它添加到顶部

ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"
Run Code Online (Sandbox Code Playgroud)

这几乎可以工作,除了rake routes不打印"/"的任何路径,并GET myurl.com/myapp/产生ActionController::RoutingError (No route matches [GET] "/")

2)告诉apache需要什么?

我的共享服务器的提供商建议将其添加到 ~/html/.htaccess

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

/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
Run Code Online (Sandbox Code Playgroud)

当我点击网站上的任何链接时,浏览器网址会改变,例如myurl.com/fcgi-bin/rails4/papers/1,应该在哪里myurl.com/myapp/papers/1.我怎么能防止这种情况?

3)如何使资产运作

我觉得这将与1)和2)一起以某种方式解决.但是,现在,该应用程序尝试:

GET myurl.com/assets/application-5e86fb668d97d38d6994ac8e9c45d45e.css
Run Code Online (Sandbox Code Playgroud)

这招致了一个404 Not Found.资产也应该在子目录下,对吧?如何告诉rails在那里放/找到它们?

小智 3

为了尝试回答你的问题,我将做出一些假设。

  1. 我假设有一个静态站点,myurl.com并且您只希望在其上提供 Rails 应用程序myurl.com/myapp

  2. 我假设您的共享托管提供商更喜欢 FastCGI 作为服务机架应用程序 (Rails) 的方式

基于这些假设,我相信如果您:

  1. 将 Rails 应用程序移至该~/html/myapp文件夹下
  2. 添加一个包含内容.htaccess的文件~/html/myapp/public
    AddHandler fastcgi-脚本.fcgi

    选项 +FollowSymLinks +ExecCGI

    重写引擎开启

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

    ErrorDocument 500“Rails 应用程序无法正确启动”

  1. 添加一个包含内容dispatch.fcgi的文件~/html/myapp/public
    ENV['RAILS_ENV'] ||= '生产'
    ENV['GEM_HOME'] = File.expand_path('your_gem_home')

    需要“fcgi”
    需要 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在部署您的应用程序之前安装您的 gems。

最后,您没有获得根路线的原因是因为 root :to => 'controller#action'您的config/routes.rb

希望这会有所帮助,如果没有,请查看:

Dreamhost - Rails 3 和 FastCGI以及FastCGI 设置,其中包括一些有关使用的提示ENV['RAILS_RELATIVE_URL_ROOT']