在 Phusion Passenger 上使用 Capistrano 设置 Ruby on Rails 应用程序环境
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设置
: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.
您可以使用 capistrano 挂钩在服务器上创建文件,或者在部署时从
shared/
等符号链接它们。对于 Rails 2.3:
在您的 Web 主机上,创建文件
shared/preinitializer.rb
:然后将其添加到您的
Capfile
(或者可能是config/deploy.rb< /code> 如果您在 Rails 2.x 中使用较新版本的 capistrano:
对于 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 中:
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
:Then add this to your
Capfile
(or possiblyconfig/deploy.rb
if you're using a newer version of capistrano with Rails 2.x:For Rails 3
Due to the changes in Rails 3's initialization sequence,
config/preinitializer.rb
is not loaded until afterconfig/environment.rb
is loaded. So for Rails 3, you want to modifyconfig/environment.rb
only on the server. You could do this with a similar setup like Rails 2 above, but using a symlinked copy ofconfig/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
:解决此问题的正确方法是在 Passenger 配置中设置 Rails 环境。让您的共享托管提供商为您进行设置。在 Apache 中,这是通过 RailsEnv 指令完成的。
如果你真的不能做到这一点,你可以考虑在你的 Rails 预初始化器(config/preinitializer)的顶部放置一个像这样的可怕的黑客:
...这将在将 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):
...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.
您需要的是 nginx 配置中的环境指令。
如果您使用 Apache,那里应该有类似的指令。 (应该很容易通过谷歌搜索)
你不能只用 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)
You cannot toggle this with just capistrano.