Search code examples
phpvariableslaravelenvironmentlaravel-forge

Laravel Forge detecting environment variables in only 3 of 4 deployed sites


they are all configured identically.

the environment is definitely set to production.

the database credentials do not get read at all - it wants to use ''@localhost password: NO as the default.

i set

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => getenv('DB_HOST'),
    'database'  => getenv('DB_NAME'),
    'username'  => getenv('DB_USERNAME'),
    'password'  => getenv('DB_PASSWORD'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

in all 4 of the sites app/config/database.php, and i set the related environment variables in forge. i have tried leaving the environment field blank, and also typing in 'production'. i also tried using $_ENV['environmentvariable'] in database.php.

this gives me a different error of Undefined Index DB_HOST. so clearly the env vars aren't getting read.

i've taken everything down and re-created the repo and the laravel server many times. all 3 other sites are configured identically. it detects the environment in bootstrap/start with

$env = $app->detectEnvironment(function()
{
    return getenv('ENV') ?: 'development';
});

i appreciate any help guys. it works fine on homestead btw.

edit: i can ssh into forge and do whatever i want in any of the sites or databases as well, except in the problem one any php artisan command fails with the 'Access Denied for ''@localhost password: NO', even if i run it with --env="production".


Solution

  • I am not entirely sure if you are using .env files at root to specify different environment vars, but after going through this myself I realized that while when environment is 'local' it reads '.env.local.php', when environment is 'production', it wants '.env.php'.

    It's in the docs, but I forget almost every time. I'm hoping that typing this out will help. :)