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

时间:2021-08-14 23:31:16

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

我有2个环境,制作和舞台演出,我正在使用Capistrano和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

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

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:

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

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

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

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

但这并没有帮助我使用Capistrano自动完成这个过程。

4 个解决方案

#1


2  

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

您可以使用capistrano挂钩在服务器上创建文件,或者将它们符号链接到例如:共享/部署时。

For Rails 2.3:

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

在您的Web主机上,创建文件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:

然后将此添加到您的Capfile(或者如果您使用带有Rails 2.x的更新版本的capistrano,则可能将其添加到config / deploy.rb:

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.

由于Rails 3的初始化序列发生了变化,因此在加载config / environment.rb之后才会加载config / preinitializer.rb。因此对于Rails 3,您只想在服务器上修改config / environment.rb。您可以使用类似的设置(如上面的Rails 2)执行此操作,但使用config / environment.rb的符号链接副本,并在尝试符号链接之前添加删除现有文件的步骤。

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

另一种选择是从capistrano覆盖服务器上的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

#2


16  

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.

设置时正在执行的操作:rails_env,“staging”环境正在设置迁移环境。换句话说,这是一个仅在运行capistrano时设置的环境。如果我理解正确,您希望在运行应用程序时更改环境,而不是部署。

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

如果您正在使用Phusion Passenger,则需要为乘客编辑RAILS_ENV

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

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

#3


2  

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.

解决此问题的正确方法是在Passenger配置中设置Rails环境。让您的共享托管服务提供商为您设置此功能。在Apache中,它完成了RailsEnv指令。

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):

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

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.

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

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

这是一个可怕的,可怕的黑客。你的旅费可能会改变。

#4


-2  

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)

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

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

}

You cannot toggle this with just capistrano.

你不能只用capistrano来切换它。

#1


2  

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

您可以使用capistrano挂钩在服务器上创建文件,或者将它们符号链接到例如:共享/部署时。

For Rails 2.3:

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

在您的Web主机上,创建文件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:

然后将此添加到您的Capfile(或者如果您使用带有Rails 2.x的更新版本的capistrano,则可能将其添加到config / deploy.rb:

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.

由于Rails 3的初始化序列发生了变化,因此在加载config / environment.rb之后才会加载config / preinitializer.rb。因此对于Rails 3,您只想在服务器上修改config / environment.rb。您可以使用类似的设置(如上面的Rails 2)执行此操作,但使用config / environment.rb的符号链接副本,并在尝试符号链接之前添加删除现有文件的步骤。

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

另一种选择是从capistrano覆盖服务器上的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

#2


16  

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.

设置时正在执行的操作:rails_env,“staging”环境正在设置迁移环境。换句话说,这是一个仅在运行capistrano时设置的环境。如果我理解正确,您希望在运行应用程序时更改环境,而不是部署。

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

如果您正在使用Phusion Passenger,则需要为乘客编辑RAILS_ENV

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

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

#3


2  

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.

解决此问题的正确方法是在Passenger配置中设置Rails环境。让您的共享托管服务提供商为您设置此功能。在Apache中,它完成了RailsEnv指令。

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):

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

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.

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

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

这是一个可怕的,可怕的黑客。你的旅费可能会改变。

#4


-2  

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)

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

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

}

You cannot toggle this with just capistrano.

你不能只用capistrano来切换它。