繁体   English   中英

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

[英]Configuring a Rails 4 app for production in a subdirectory under Apache

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

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

当我点击网站上的任何链接时,浏览器网址会更改为例如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在那里放/找到它们?

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

  1. 我假设有一个静态的网站在提供服务myurl.com而你只想要Rails应用程序须送达myurl.com/myapp

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

根据这些假设,我相信如果你:

  1. 将您的Rails应用程序移动到~/html/myapp文件夹下
  2. .htaccess文件添加到~/html/myapp/public ,内容如下:
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. 使用内容将dispatch.fcgi文件添加到~/html/myapp/public
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和FastCGI以及FastCGI设置 ,其中包含有关使用ENV['RAILS_RELATIVE_URL_ROOT']一些提示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM