1

We have three development servers (dev1, dev2, stage) and one production server and some symfony projects.

We would like to change the environment automatically. Currently my hack looks like this in 'public/index.php'

use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/../vendor/autoload.php';

//----------- hack start ------------

$_SERVER['APP_ENV'] = 'prod';
$_SERVER['APP_DEBUG'] = 0;
$_SERVER['APP_SECRET'] = '67d829bf61dc5f87a73fd814e2c9f629';

$localIP = getHostByName(getHostName());
if ($localIP === '81.4.552.97') {
     $_SERVER['APP_ENV'] = 'dev1';
     $_SERVER['APP_DEBUG'] = 1;
     $_SERVER['APP_SECRET'] = '67d829bf61dc5f87a73fd814e2c9f629';
}

...

//----------- hack end ------------

// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
    (new Dotenv())->load(__DIR__.'/../.env');
}

if ($_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))) {
    umask(0000);

    Debug::enable();
}

// Request::setTrustedProxies(['0.0.0.0/0'], Request::HEADER_FORWARDED);

$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev')));
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

and I have three new environments under 'config/packages/*' including the configuration for database and email.

Is this the right way? Maybe some others also have this problem. I just tested it under dev1, it seems to be running.

miken32
  • 42,008
  • 16
  • 111
  • 154
ABSimon
  • 651
  • 1
  • 6
  • 18

3 Answers3

4

The easiest way, probably, is to set environment by HTTP server. For Apache, in .htaccess:

SetEnvIf Remote_Addr 1\.2\.3\.4 APP_ENV=dev
SetEnvIf Remote_Addr 1\.2\.3\.4 APP_DEBUG=1

The rest is in .env.dev

Cheery
  • 16,063
  • 42
  • 57
0

Why not just use environment variables?

https://symfony.com/doc/current/configuration/external_parameters.html

Sebi Ilie
  • 86
  • 5
  • hello, we want to have version where you just install it and it runs everywhere (dev, stage, prod). With SOA (service oriented architecture) you have multiple symfony projects, ~ 5 devolopers work on different machines on different projects and dotenv is not good for production. – ABSimon Jun 30 '18 at 11:15
0

currently I think the best solution is to remove dotenv composer remove symfony/apache-pack and to add a require in public/index.php and bin/console

use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/../env.php';   
require __DIR__.'/../vendor/autoload.php';

// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
    (new Dotenv())->load(__DIR__.'/../.env');
}
...

in this env.php I can declare all server vars for the environments

$_SERVER['APP_ENV'] = 'xxx';
$_SERVER['MAILER_URL'] = 'null://localhost';
$_SERVER['APP_SECRET'] = '425017d316ee2a08e54c6f2bfc59ff8d';
$_SERVER['DATABASE_URL'] = 'xxx';

I think this is a nice and fast solution. (if php is not installed someday, the apache server will not send a file with all configs ;))

ABSimon
  • 651
  • 1
  • 6
  • 18