Symfony 4 - automatically change the environment by IP
Symfony 4 - automatically change the environment by IP
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 AppKernel;
use SymfonyComponentDebugDebug;
use SymfonyComponentDotenvDotenv;
use SymfonyComponentHttpFoundationRequest;
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 environmets 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.
2 Answers
2
Why not just use environment variables?
https://symfony.com/doc/current/configuration/external_parameters.html
currently I think the best solution is to remove dotenvcomposer remove symfony/apache-pack and to add a require
in public/index.php and bin/console
composer remove symfony/apache-pack
public/index.php
bin/console
use AppKernel;
use SymfonyComponentDebugDebug;
use SymfonyComponentDotenvDotenv;
use SymfonyComponentHttpFoundationRequest;
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
env.php
$_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 ;))
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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 at 11:15