douan6815
2018-09-26 18:50 阅读 81
已采纳

为什么我要使用环境变量来配置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 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.

    点赞 评论 复制链接分享
  • dongyuanguang3893 dongyuanguang3893 2018-09-26 19:24

    What is the meaning of environment variables in production? Why not just have the variables in a file like Wordpress, Django, and others?

    The first thing to understand is how many different environments could be running your code. You could have production, staging, testing, and local environments. Does it really make sense to have completely different configuration files on each that cannot be synced to your VCS repo because of conflicts?

    You still do have configuration files in Symfony and other frameworks. However, some settings are dependent on the environment while others are not. The ones that are dependent on the environment can be set from environmental variables. This way you can sync your configuration files but still maintain environment specific settings.


    The rest of your concerns can be explained by the fact that environmental variables do not have to be global. Web servers can define environmental variables in various contexts, such as using SetEnv in a specific virtualhost as miken32 suggests.

    点赞 评论 复制链接分享
  • duanmi3476 duanmi3476 2018-09-27 01:28

    What is the meaning of environment variables in production? Why not just have the variables in a file like Wordpress, Django, and others?

    • environment variables are values that change (and only change) as you move your app from one server to another

    • these are values unique to that server or that cluster like for example the server's hostname or the hostname/IP it uses to connect to a database

    you group these values that change together so that when you deploy to different environments/different groups of servers like testing environment,staging environment/production environment your app automatically adjusts

    Why would I like have the credentials of my project's database in a global OS variable???

    as answered previously there are other ways to set it. like in your webserver or only in the virtual block of your webserver

    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?

    Yes, because usually if a server is marked as "testing environment" or "UAT environment" everything you deploy there should behave the same (especially if it is a a production server).

    It's just that organizing deployments and servers like that make management a lot easier compared to having mixed kinds of purpose for each server.

    点赞 评论 复制链接分享

相关推荐