3

I've been using parameters.yml for years but now it's time to switch to Symfony 4 with environment variables :-)

I have several questions about deploying this on my server. I'm using Nginx + PHP-FPM. The documentation says we can set environment variables on the Nginx config side. Some other blogs advise to set environment variables on the PHP-FPM pool config side. But what about the console, then? How will bin/console know about these environment variables?

The goal is to centralize the settings and having a single edit point. Maybe we could set them on the OS side, but what if I'm running multiple applications on the same server?

In my use case, I have a remote server (FreeBSD or Ubuntu 16.04 for another app) delivering the production environment in /var/www/myapp/prod and the pre-production (with restricted access in the nginx config) in /var/www/myapp/qualif. They share the same environment variable keys but not the same values (different database DSN, for instance). I'm not using docker.

What are your recommendations?

Ben
  • 845
  • 1
  • 8
  • 18
  • 3
    Well, it's quite clear described in the [docs](http://symfony.com/doc/current/configuration/external_parameters.html). From 3.3 the new `Dotenv` component was introduced, which will keep environment variables in `.env` file to be used in local environment. – VisioN Jan 25 '18 at 09:52
  • 1
    Yes, but the documentation says "Symfony Dotenv should only be used in development/testing/staging environments. For production environments, use "real" environment variables." https://symfony.com/doc/current/components/dotenv.html - Maybe in my use case it's safer to use the DotEnv component anyway? – Ben Jan 25 '18 at 10:26
  • The documentation suggests right. It's always a bad practice to keep important constants (e.g. passwords, API keys, etc) in the code. That's why you would need to set environment variables, which are 1) easy to setup for different environments with a single code base; 2) hard to steal if anybody gets access to the code repository. – VisioN Jan 25 '18 at 13:33
  • I have seen this question before but no answer as of yet. At present I am just accepting the overhead of parsing the .env file even in production. Really seems like there should be a fastcgi utility that would read params from a .env file. But I have not yet found one. Might try asking on the Symfony [Slack](https://symfony.com/community) site or maybe ask the nginx community – Cerad Jan 25 '18 at 14:01
  • The other thing is that I'm not positive that you even need to add anything but fastcgi_param APP_ENV prod; for production. It seems like the value of all the other env variables will end up being compiled into the production container. I have not tried this yet but might be worth a shot. – Cerad Jan 25 '18 at 14:28
  • 1
    Again, setting environment variables in Nginx fastcgi_param will prevent your `bin/console` commands to inherit from them, that's why I'm against this practice. I actually parse the .env file in production, which works for both http and cli, and I don't understand why it's a bad practice since the .env file is .gitignored anyway. On my setup, this file only exist on the different deployment targets, not in the VCS. – Ben Jan 25 '18 at 14:51
  • it seems like there is no way without 'copy-paste' at the moment: https://serverfault.com/questions/844088/pass-environment-variables-to-the-php-cli-and-fpm – freshp Mar 02 '18 at 13:02

1 Answers1

0

I usually set the config variables at runtime. Could be on docker run command with -e or in a docker-compose.yml file.

I would run separate containers for different environment-

Carlos
  • 1,411
  • 15
  • 21