【问题标题】:Configuring a Rails 4 app for production in a subdirectory under Apache在 Apache 下的子目录中为生产配置 Rails 4 应用程序
【发布时间】:2013-08-04 20:28:08
【问题描述】:

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

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 (No route matches [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 Not Found。资产也应该在子目录下,对吧?我如何告诉 rails 把它们放在那里/找到它们?

【问题讨论】:

    标签: ruby-on-rails apache deployment ruby-on-rails-4 subdirectory


    【解决方案1】:

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

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

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

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

    1. 将您的 Rails 应用程序移至 ~/html/myapp 文件夹下
    2. .htaccess 文件添加到~/html/myapp/public,内容如下:
    AddHandler fastcgi-script .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 安装您的 gem。

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

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

    Dreamhost - Rails 3 and FastCGIFastCGI setup 包括一些关于使用 ENV['RAILS_RELATIVE_URL_ROOT'] 的提示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多