douan6815 2018-09-26 18:50
浏览 90
已采纳

为什么我要使用环境变量来配置Symfony应用程序?

Yesterday was the first time I made a deploy in an own server with Symfony 4. Reading the official documentation about deployment, I quote:

Most Symfony applications read their configuration from environment variables. While developing locally, you'll usually store these in a .env file. But on production, instead of creating this file, you should set real environment variables.

Now, since I read on multiple sites that that's the correct way to work in production, I have several doubts about it:

  • What is the meaning of environment variables in production? Why not just have the variables in a file like Wordpress, Django, and others?
  • Why would I like have the credentials of my project's database in a global OS variable???
  • Being globals and accessible by all in the OS, if I would have more than one project with the same env var (for instance, APP_ENV in Symfony), changing its value would affect all my projects! Does It make any sense?

If anyone could throw a little light on the subject, I would appreciate it very much.

Sorry about my English

  • 写回答

3条回答 默认 最新

  • dr200166 2018-09-26 18:59
    关注

    This question is not a great fit for SO, but in brief: Environment variables can be set for any environment, not just the operating system. Like your web server, for example. I have the following in /etc/httpd/conf.d/variables.conf which is included into certain virtual hosts in my main Apache config:

    #Config for web apps
    SetEnv DB_CONNECTION      "mysql"
    SetEnv DB_HOST            "192.168.242.1"
    SetEnv DB_USERNAME        "dbuser"
    SetEnv DB_PASSWORD        "dbpass"
    SetEnv DB_DATABASE        "dbname"
    

    This file can be protected with permissions to ensure only the web server itself can read them. And I can have multiple files, if I have multiple apps or virtual hosts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站