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

Mick79's avatar

Env variables wiped out when I try to deploy.

I have Forge setup to control an EC2 instance on AWS.

I have Forge connected to my Github.

When I try to deploy my app I am getting a "cannot deploy" error and when I look at the build logs its failing on a database query.

I have added my DB env variables to the Forge Env variables file and saved it. I can reload it and the variables are there. However shortly after I start a deploy the env file becomes empty again and I keep getting the deploy failing because it cannot connect to the database,

Any ideas?

0 likes
6 replies
MohamedTammam's avatar

Clear the deploy script from any commands that connect to the database.

After deploying and installing dependencies access your .env file from Forge and add you variables. Then go to the commands page and run your database commands.

Lastly, put back your database commands in your deployment script.

Mick79's avatar

@MohamedTammam @godmaster

So... a bit more progress and it seems that the real problem is that I can't connect to my database even though the env variables are there and correct. I haven't set a domain yet so I'm accessing the site via the IP address.

Very annoying.

the env variables are as follows:

DB_CONNECTION=mysql
DB_HOST=MYHOST
DB_PORT=3306
DB_DATABASE=DATABASE
DB_USERNAME=USERNAME
DB_PASSWORD=PASSWORD

Am I missing anything?

MohamedTammam's avatar

@Mick79 Go you the database tab, copy the name ( the default is forge). And the password of that DB should've sent to you via email first time you created the server. If you can't find it. Just set a new password to the forge database or create a new one.

Your .env DB variables should look like that.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD="your_database_password"
Mick79's avatar

@MohamedTammam So this is where I think the forge documentation falls down...

Anyway, my issue is I am using a pre-existing database and just setting those connection variables in the environment variables field. This seems to be working fine now but the site is a bit slower than it was in heroku.

my server and my database are both in us-east but... I feel like I'm not doing something I should be doing.

MohamedTammam's avatar

@Mick79 If it works but it's slow, Forge has nothing to do with it. Laravel Forge set up your server, it isn't the server itself.

In general, you should debug to see why it's slow ,and check your server plan/limits in your cloud server provider.

Please close the discussion if your problem got solved.

GodMaster's avatar

Hi Mick, If you execute the "config:cache" command during deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

1 like

Please or to participate in this conversation.