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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?