Search code examples
phpsymfonysymfony5

How to set up a staging environment based on a symlink to prod configuration, without showing debug information?


I would like to setup a staging environment with the same configuration as the prod environment.

According to the docs, I proceed as follow:

I create a staging symlink that points on prod

  • configure the env in .env: APP_ENV=staging
  • clear the cache: php bin/console cache:clear
  • ask for an URL that does not exist to trigger a 404 error: http://localhost:8080/an-url-that-does-not-exists

When the APP_ENV=prod, my custom error page is render properly, but when APP_ENV=staging, the debug message NotFoundHttpException is rendered? The profiler is not displayed.

What am I missing?


Solution

  • tldr;

    Crate an .env.staging file and use it to set APP_DEBUG to 0. Debug mode and environment are set independently.


    By default, unless you set it explicitly debug mode (APP_DEBUG) is set from the environment automagically.

    This happens in the following steps:

    In your front-controller (index.php, usually) you would find this line:

    (new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
    

    And on DotEnv::bootEnv() you'll find this:

    $debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey], $this->prodEnvs, true);
    

    This will compare your APP_ENV with an array of "environments" that DotEnv, considers "production-like". By default, this array includes only prod.

    You could modify the instance of the DotEnv by calling setProdEnvs():

    (new Dotenv())
        ->setProdEnvs(['prod', 'staging'])
        ->bootEnv(dirname(__DIR__).'/.env');
    

    ... but generally simply disabling debug mode on your .env file would be enough.