Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YuChengWang's avatar

Environment variables doesn't working on Forge

I have deployed one Laravel 4.2 app on Forge before and everything works fine. I use environment variable to get database connection info like this:

 'host'      => getenv('db_host'),
 'database'  => getenv('db_name'),
'username'  => getenv('db_username'),
'password'  => getenv('db_password'),

But today I try to deploy a new app(Laravel 4.2 on AWS EC2), using the same code above and setting the environment variable on Forge.

During the migration step, it shows the error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

I ssh into the server and run

php artisan env

It shows the right environment:

Current application environment: production

It looks link the environment variable that I set on Forge is not configured correctly on the server.

Any way to fix this?

Thank you!

0 likes
6 replies
step_han's avatar
Level 1

I have the same issue here. I have a reference image http://prntscr.com/7um7u5. Taylor said me: I would suggest including the DotEnv library used by Laravel 4 into your Laravel 5 project (https://github.com/vlucas/phpdotenv). You can load the .env file in your bootstrap/start.php file or similar. This will let you use the convenience of the new environment features while still using Laravel 4.

YuChengWang's avatar

Hi @step_han,

Try to put the following code in start.php

$env = $app->detectEnvironment( function(){
    try {
        if (($env = getenv('APP_ENV')) === false) {
            $dotenv = new Dotenv\Dotenv(__DIR__.'/../');
            $dotenv->load();
            $env = getenv('APP_ENV');
        }
        return $env;
    } catch (Exception $e) {
        return 'local';    
    }
});

reference: https://alfrednutile.info/posts/113

p.s. I think showing your production db config in the image is a bad idea.

1 like
step_han's avatar

I tested with a lot of codes very similar but nothing like your code. It work! thank you @YuChengWang. I also change the location of my env files. It must be in the root like the image here http://prntscr.com/8aihtm not like the forge console recommend.

dnilvincent's avatar

Good to know that you already fixed this man :D #HappyCoding

1 like

Please or to participate in this conversation.