简体   繁体   English

Symfony 4,.env文件和制作

[英]Symfony 4, .env files and production

.env files are very handy with docker, kubernetes, etc .env文件非常方便docker,kubernetes等

But what if I have simple nginx server without any orchestration and a pack of cron workers and a pack of daemons(systemd/supervisord/etc)? 但是,如果我有简单的nginx服务器没有任何编排和一包cron worker和一包守护进程(systemd / supervisord / etc)怎么办?
I can write these env variables to nginx server section, but I have to set up hundreds of env variables to each cron worker or daemon. 我可以将这些env变量写入nginx服务器部分,但我必须为每个cron worker或daemon设置数百个env变量。

I found a quick solution: using symfony/dotenv component in production. 我找到了一个快速的解决方案:在生产中使用symfony / dotenv组件。
But it seems to me dirty. 但在我看来很脏。 Who can suggest better solution? 谁能提出更好的解决方案?

First of all, not all variables need to be specified using environment variables. 首先,并非所有变量都需要使用环境变量来指定。 Keep variables that do not differ per system in a separate yaml file. 在单独的yaml文件中保留每个系统不同的变量。

When you have just one environment per server you can specify the environment variables globally in /etc/environment . 如果每个服务器只有一个环境,则可以在/etc/environment全局指定环境变量。 (Might be different depending on your Linux flavour) (根据你的Linux风格可能会有所不同)

Personally I find that using DotEnv poses more difficulties than solutions when you run multiple environments on the same server. 我个人发现,当您在同一台服务器上运行多个环境时,使用DotEnv会比解决方案带来更多困难。 Specifying the variables in a global configuration like /etc/environment doesn't work in that case. 在这种情况下,在/etc/environment全局配置中指定变量不起作用。

Specifying the environment variables in nginx isn't a solution either since, as you mentioned, they won't be picked up by cron, supervisor, the console, etc. For me, this was the reason to completely remove DotEnv and work with the good old parameters.yaml file again. 在nginx中指定环境变量也不是解决方案,因为正如你所提到的,它们不会被cron,supervisor,控制台等接收。对我来说,这就是完全删除DotEnv并使用它的原因。旧的parameters.yaml文件再次。 Nothing will stop you from doing that. 没有什么能阻止你这样做。

Another solution however is to keep using DotEnv in your development environment and to include a separate parameters.yaml in production. 然而,另一个解决方案是在开发环境中继续使用DotEnv,并在生产中包含单独的parameters.yaml You can then define the environment variables as follows: 然后,您可以按如下方式定义环境变量:

parameters:
  env(APP_ENV): prod
  env(APP_SECRET): 3d05afda019ed4e3faaf936e3ce393ba
  ...

A way to include this file is to put the following in your services.yaml file: 包含此文件的方法是将以下内容放在services.yaml文件中:

imports:
    - { resource: parameters.yaml, ignore_errors: true }

This way, the import will be ignored when no parameters.yaml file exists. 这样,当没有parameters.yaml文件存在时,将忽略导入。 Another solution is to add a line to configureContainer() in your Kernel class: 另一种解决方案是在Kernel类中为configureContainer()添加一行:

$loader->load($confDir.'/parameters'.self::CONFIG_EXTS, 'glob');

if you want to centralize your environment variables for cli and fpm you can define them once in your system. 如果要集中cli和fpm的环境变量,可以在系统中定义一次。 And then reference them all in your php-fpm.conf : 然后在php-fpm.conf引用它们:

....
[www]
env[APP_VAR1] = $APP_VAR1
env[APP_VAR2] = $APP_VAR2
...

In that way you can avoid using DotEnv in production which is encouraged by best practices. 通过这种方式,您可以避免在最佳实践鼓励的生产中使用DotEnv。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM