在 Phusion Passenger 上使用 Capistrano 设置 Ruby on Rails 应用程序环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5770337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the Ruby on Rails application environment using Capistrano on Phusion Passenger
提问by dangerousdave
I have 2 environments, productionand staging, and I am using Capistrano with capistrano-ext gem.
我有 2 个环境,production和staging,我正在使用 Capistrano 和 capistrano-ext gem。
When I deploy to stagingusing Capistrano and restart passenger, I would like the deployed application to run in staginghowever 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 自动化该过程。
采纳答案by Jeremy Weathers
You can use a capistrano hook to create files on the server or symlink them in from e.g. shared/when deploying.
您可以使用 capistrano 钩子在服务器上创建文件或shared/在部署时将它们符号链接。
For Rails 2.3:
对于 Rails 2.3:
On your web host, create the file shared/preinitializer.rb:
在您的网络主机上,创建文件shared/preinitializer.rb:
ENV['RAILS_ENV'] = 'staging'
Then add this to your Capfile(or possibly config/deploy.rbif you're using a newer version of capistrano with Rails 2.x:
然后将其添加到您的Capfile(或者config/deploy.rb如果您使用的是带有 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
For Rails 3
对于 Rails 3
Due to the changes in Rails 3's initialization sequence, config/preinitializer.rbis not loaded until after config/environment.rbis loaded. So for Rails 3, you want to modify config/environment.rbonly 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/preinitializer.rb加载之后才config/environment.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
回答by Cory
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 乘客,则需要为乘客编辑 RAILS_ENV
Given that you're in a shared environment, you'll probably want to go with the .htaccess route.
鉴于您处于共享环境中,您可能希望使用 .htaccess 路线。
回答by Winfield
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.
解决这个问题的正确方法是在你的乘客配置中设置 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 预初始化器(配置/预初始化器)的顶部放置一个像这样的 TERRIBLE 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.
这是一个可怕的、可怕的黑客。你的旅费可能会改变。
回答by Tanel Suurhans
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 配置中的环境指令。如果您使用的是 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 来切换它。

