ruby-on-rails - 在 Apache 下的子目录中为生产配置 Rails 4 应用程序

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

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

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) 如何让 Assets 工作

我觉得这会以某种方式与 1) 和 2) 一起解决。但是,现在,该应用程序尝试执行以下操作:
GET myurl.com/assets/application-5e86fb668d97d38d6994ac8e9c45d45e.css

产生 404 Not Found . Assets 也应该在子目录下,对吗?我如何告诉 Rails 在那里放置/找到它们?

最佳答案

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

  • 我假设在 myurl.com 上有一个静态站点。并且您只想在 myurl.com/myapp 上提供 Rails 应用程序
  • 我假设您的共享主机提供商更喜欢 FastCGI 作为服务 Rack 应用程序(Rails)的一种方式

  • 基于这些假设,我相信如果您:
  • 将您的 Rails 应用移到 ~/html/myapp 下文件夹
  • 添加 .htaccess文件到 ~/html/myapp/public内容:

  • AddHandler fastcgi-script .fcgi

    选项 +FollowSymLinks +ExecCGI

    重写引擎开启

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

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

  • 添加 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')

  • 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 and FastCGIFastCGI setup其中包括一些关于使用 ENV['RAILS_RELATIVE_URL_ROOT'] 的提示

    关于ruby-on-rails - 在 Apache 下的子目录中为生产配置 Rails 4 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047261/

    相关文章:

    Angular 8 未能部署到 Github Pages -

    ruby-on-rails - 如何在 Rails 中使用 ruby​​-progressbar?

    css - 如何在 CSS 文件中从 Ruby 插入值

    apache - https htaccess 排除一些 url

    php - UTF-8 html5网页编码

    java - Web服务器部署问题

    ruby-on-rails - Ruby on Rails : Using Refile Gem, 显示图像而不裁剪或扭曲图像

    ruby-on-rails - 这个渲染方法在哪里定义的?

    apache - 尽管有多个虚拟主机,但所有流量都定向到单个目录

    python - 我的 celery redis 任务在 heroku 服务器上的 django 应用程序中不起作用