环境变量管理的最佳实践 [英] Best Practice for Environment Variable Management

查看:125
本文介绍了环境变量管理的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本地机器开发Zend的MVC和我使用的是DreamHost的网站升级。由于每 Zend的快速启动,在我的本地机器我在该项目的公共目录设置我APPLICATION_ENV发展从一个.htaccess文件,像这样的:

I'm developing a Zend MVC on my local machine, and am using a DreamHost site for staging. As per the zend quick start, on my local machine I set my APPLICATION_ENV to development from within a .htaccess file in the public directory of the project, like this:

的myproject /公/的.htaccess包含此行,

myproject/public/.htaccess contains this line,

SETENV APPLICATION_ENV发展

"SetEnv APPLICATION_ENV development"

这本地工作就好了。但是,当我尝试使用我的app_env在我的临时服务器它不存在使用。这线程的建议,也许suexec的是在后台运行。我做了一些挖掘,果然, Dreamhost的模块定制环境变量的。

This works just fine locally. But when I try to use my app_env in my staging server it's not there to use. This thread suggested that perhaps suexec was running in the background. I did some digging and sure enough, Dreamhost blocks custom environment variables.

我尝试的第一个解决方法,卫生署建议,以prependingHTTP_到APPLICATION_ENV,然后添加在公立/ index.php的另一个条件来检查原始和Dreamhost的版本。

I tried the first workaround that DH suggested, by prepending "HTTP_" to "APPLICATION_ENV", and then adding another conditional within public/index.php to check for both the original and the dreamhost version.

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', 
  (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 
  (getenv('HTTP_APPLICATION_ENV') ? getenv('HTTP_APPLICATION_ENV') : 'production')));

这似乎并没有为我工作。即使在SSH,当我在命令行中运行ENV,我application_env没有列出。因为我不工作与CGI脚本我没有打扰与第二和第三个解决方法。

This didn't seem to work for me. Even in ssh, when I run 'env' in the command line, my application_env isn't listed. Since I'm not working with CGI script I didn't bother with the second and third workarounds.

TL;博士

所以,据我所知,我的选择是要么创建自己的配置系统(app_env保存到不同的文件,加载该文件中的公共/ index.php文件这似乎是更多的工作是值得考虑的选项2 ),或者只是硬$ C C的app_env到默认位置$ - 意义,而不是在它上面的code例如生产,也只是说'升级'。如果我去了后者,我不得不的.gitignore我的公开/ index.php文件,它只是似乎有些奇怪。在此之前,我从来没有改变该文件,并会认为它的更好,如果我不惹它,更不用说完全留给我的仓库外。 思考?建议?另外的解决方法?

So, as far as I understand it, my options are to either create my own config system (save app_env to a different file, load that file in public/index.php which seems like more work than it's worth considering option 2) or just 'hard' code the app_env into the default position - meaning, instead of 'production' in the code example above it would just say 'staging'. If I went with the latter, I'd have to .gitignore my public/index.php file, which just seems a little strange. Before this, I've never had to change that file and would assume that it's better if I don't mess with it, let alone completely leave it outside of my repository. Thoughts? Suggestions? Further workarounds?

推荐答案

我也经历过同样的问题,因为你。然而,这是不一样大,因为它似乎。一旦index.php文件设置它永远不会改变,因此它是安全的离开它的版本控制。

I have experienced the same problem as you. However, it is not as big as it seems. Once index.php is set up it never changes, so it is safe to leave it out of version control.

硬$ C C APPLICATION_ENV对您的每一个开发,分期和生产服务器,而忘记了它的定义$。

Hard code the the definition of APPLICATION_ENV on each of your dev, staging and production servers and forget about it.

我一直在发展与ZF两年了,我不记得我最后一次做了更改的index.php以外将其设置为每个服务器,这是改变只是1行。

I have been developing with ZF for two years now and I can't remember the last time I made a change to index.php other than setting it up for each server and that is to change just 1 line.

TL;博士 它不是值得努力不是硬在您的情况编码APPLICATION_ENV做其他的。

tl;dr Its not worth the effort to do other than hard coding APPLICATION_ENV in your situation.

这篇关于环境变量管理的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆