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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法