在 Phusion Passenger 上使用 Capistrano 设置 Ruby on Rails 应用程序环境

发布于 2024-11-03 03:04:45 字数 469 浏览 5 评论 0原文

我有 2 个环境,生产登台,并且我使用 Capistrano 和 capistrano-ext gem。

当我使用 Capistrano 部署到staging并重新启动乘客时,我希望部署的应用程序在staging中运行,但它在默认的生产

运行尝试设置:

set :rails_env, "staging"

在我的部署配方中,但这没有效果。

我知道这可以通过在 Apache 中设置虚拟主机来完成,但我使用的是共享主机,因此无权访问。我的主人提供了这样的建议:

将以下内容添加到environment.rb:ENV['RAILS_ENV'] = 'staging'

但这并不能帮助我使用 Capistrano 自动化该过程。

I have 2 environments, production and staging, and I am using Capistrano with capistrano-ext gem.

When I deploy to staging using Capistrano and restart passenger, I would like the deployed application to run in staging however it runs in the default production

I tried setting:

set :rails_env, "staging"

in my deploy recipe, but this had no effect.

I am aware this can be done by setting a virtual host in Apache, but I am using shared hosting, so don't have access. My host offers this advice:

add the following to environment.rb: ENV['RAILS_ENV'] = 'staging'

but this doesn't help me automate the process with Capistrano.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独守阴晴ぅ圆缺 2024-11-10 03:04:45

设置 :rails_env, "staging" 环境时所做的就是设置迁移环境。换句话说,它是一个仅在运行 capistrano 时设置的环境。如果我理解正确,您想在运行应用程序时更改环境,而不是部署。

为了回答您的问题,我需要知道您如何启动(启动)您的应用程序。

如果您使用 Phusion Passenger,则需要编辑 RAILS_ENV Passenger

鉴于您处于共享环境中,您可能希望采用 .htaccess 路线。

What you are doing when you setting :rails_env, "staging" environment is setting the environment for the migration. In other words, it's a environment that is only set when you're running capistrano. If I understand you correctly, you want to change the environment when running your app, not deploying.

In order to answer your question, I'll need to know how you are launching (starting) your application.

If you're using Phusion Passenger, you'll need to edit your RAILS_ENV for Passenger

Given that you're in a shared environment, you'll probably want to go with the .htaccess route.

埖埖迣鎅 2024-11-10 03:04:45

您可以使用 capistrano 挂钩在服务器上创建文件,或者在部署时从 shared/ 等符号链接它们。

对于 Rails 2.3:

在您的 Web 主机上,创建文件 shared/preinitializer.rb

ENV['RAILS_ENV'] = 'staging'

然后将其添加到您的 Capfile(或者可能是 config/deploy.rb< /code> 如果您在 Rails 2.x 中使用较新版本的 capistrano:

after 'deploy:symlink', 'localize:copy_shared_configurations'
namespace :localize do
  desc 'copy shared configurations to current'
  task :copy_shared_configurations, :roles => [:app] do
    # I put database.yml here to keep deployed db credentials out of git
    %w[
      preinitializer.rb
    ].each do |f|
      run "ln -nsf #{shared_path}/#{f} #{current_path}/config/#{f}"
    end
  end
end

对于 Rails 3

由于 Rails 3 初始化顺序的更改,config/preinitializer.rb 直到 < 之后才会加载code>config/environment.rb 已加载,因此对于 Rails 3,您只想在服务器上修改 config/environment.rb ,您可以使用与 Rails 类似的设置来完成此操作。 2,但使用 config/environment.rb 的符号链接副本,并在尝试符号链接之前添加删除现有文件的步骤。

另一个选择是覆盖服务器上的environment.rb。在你的 config/deploy.rb 中:

after 'deploy:symlink', 'localize:munge_environment'
namespace :localize do
  desc 'munge environment.rb to set staging environment'
  task :munge_environment, :roles => [:app] do
    new_env = "ENV['RAILS_ENV'] = 'staging'\n" + File.read(Rails.root.join('config', 'environment.rb'))
    put new_env, "#{current_path}/config/environment.rb"
  end
end

You can use a capistrano hook to create files on the server or symlink them in from e.g. shared/ when deploying.

For Rails 2.3:

On your web host, create the file shared/preinitializer.rb:

ENV['RAILS_ENV'] = 'staging'

Then add this to your Capfile (or possibly config/deploy.rb if you're using a newer version of capistrano with Rails 2.x:

after 'deploy:symlink', 'localize:copy_shared_configurations'
namespace :localize do
  desc 'copy shared configurations to current'
  task :copy_shared_configurations, :roles => [:app] do
    # I put database.yml here to keep deployed db credentials out of git
    %w[
      preinitializer.rb
    ].each do |f|
      run "ln -nsf #{shared_path}/#{f} #{current_path}/config/#{f}"
    end
  end
end

For Rails 3

Due to the changes in Rails 3's initialization sequence, config/preinitializer.rb is not loaded until after config/environment.rb is loaded. So for Rails 3, you want to modify config/environment.rb only on the server. You could do this with a similar setup like Rails 2 above, but using a symlinked copy of config/environment.rb, and adding the step of deleting the existing file before trying to symlink.

Another option would be to overwrite the environment.rb on the server from capistrano. In your config/deploy.rb:

after 'deploy:symlink', 'localize:munge_environment'
namespace :localize do
  desc 'munge environment.rb to set staging environment'
  task :munge_environment, :roles => [:app] do
    new_env = "ENV['RAILS_ENV'] = 'staging'\n" + File.read(Rails.root.join('config', 'environment.rb'))
    put new_env, "#{current_path}/config/environment.rb"
  end
end
难以启齿的温柔 2024-11-10 03:04:45

解决此问题的正确方法是在 Passenger 配置中设置 Rails 环境。让您的共享托管提供商为您进行设置。在 Apache 中,这是通过 RailsEnv 指令完成的。

如果你真的不能做到这一点,你可以考虑在你的 Rails 预初始化器(config/preinitializer)的顶部放置一个像这样的可怕的黑客:

forced_environment = './config/force_environment'
if File.exists?(forced_environment)
  ENV['RAILS_ENV'] = File.new(forced_environment).readline.chomp
end

...这将在将 Rails 加载到该配置中的字符串之前设置环境/forced_environment 文件。对于您的舞台服务器,您可以将“舞台”设置为环境。

这是一个非常非常可怕的黑客行为。您的里程可能会有所不同。

The right way to solve this is to set the Rails environment in your Passenger config. Get your shared hosting provider to set this up for you. In Apache it's done with RailsEnv directive.

If you REALLY can't do that, you could consider putting a TERRIBLE HACK like this at the top of your Rails pre-initializer (config/preinitializer):

forced_environment = './config/force_environment'
if File.exists?(forced_environment)
  ENV['RAILS_ENV'] = File.new(forced_environment).readline.chomp
end

...which will set the environment before loading Rails to the string in that config/forced_environment file. For your stage server you could set 'stage' as the environment.

This is a terrible, terrible hack. Your mileage may vary.

苏佲洛 2024-11-10 03:04:45

您需要的是 nginx 配置中的环境指令。
如果您使用 Apache,那里应该有类似的指令。 (应该很容易通过谷歌搜索)

server {
        listen                  80;
        passenger_enabled       on;
        rails_env               staging;
        server_name             foo.com;
        root                    /your/app/path;

}

你不能只用 capistrano 来切换它。

What you need is the environment directive in your nginx configuration.
If you are using Apache, there should be a similar directive there. (should be easy to google)

server {
        listen                  80;
        passenger_enabled       on;
        rails_env               staging;
        server_name             foo.com;
        root                    /your/app/path;

}

You cannot toggle this with just capistrano.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文