首页 > 解决方案 > Why would I use environment variables for configuring a Symfony app?

问题描述

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:

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

Sorry about my English

标签: phpsymfonydeploymentenvironment-variables

解决方案


这个问题不太适合 SO,但简而言之:可以为任何环境设置环境变量,而不仅仅是操作系统。例如,就像您的 Web 服务器一样。/etc/httpd/conf.d/variables.conf我的主要 Apache 配置中的某些虚拟主机中包含以下内容:

#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"

可以使用权限保护此文件,以确保只有 Web 服务器本身可以读取它们。如果我有多个应用程序或虚拟主机,我可以有多个文件。


推荐阅读