php - 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:
- 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
解决方案
这个问题不太适合 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 服务器本身可以读取它们。如果我有多个应用程序或虚拟主机,我可以有多个文件。
推荐阅读
- java - Kerberos POST 请求使用 Java 返回 401 错误
- c# - 处理函数
在 autofac 中注册 - c++ - 为什么直接传递'this'指针来归档错误,但另一个相同类型的指针是可以的?
- bitbake - /dev/fd/ 套接字或管道链接失败,没有丢失 /dev/fd 链接
- python-2.7 - 在用户资源对象上接收 keyError: 'deletionTime'
- apache-spark - 压缩火花输出文件
- r - 使用许多特殊字符将 R 连接到 HANA db 的问题
- rsyslog - rsyslog 服务器生成以 IP 地址而不是 access_log 命名的日志文件
- postgresql - 如何根据用户查询数据在视图中设置动态“位置”?
- python - 使用 Python 将两个 CSV 文件与案例合并