Genarito
Genarito

Reputation: 3431

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

Upvotes: 2

Views: 1020

Answers (3)

markrosario
markrosario

Reputation: 99

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.

Upvotes: 1

Devon Bessemer
Devon Bessemer

Reputation: 35337

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.

Upvotes: 1

miken32
miken32

Reputation: 42685

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.

Upvotes: 3

Related Questions