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 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题