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

Ortix's avatar

ENV variables empty on test site using Forge

I have 2 sites on my digitalocean droplet using forge.

The production and the test site. I've created several ENV variables (including the variable ENVIRONMENT = production) on my production site which work fine.

However, nothing works on my test site. Any environment variable I create simply is not available in $_ENV...

currently I have these 3 files: .env.php .env.staging.php .env.test.php (somehow magically created)

and they all contain:

<?php return array (
  'ENVIRONMENT' => 'staging',
);

Yet the $_ENV variable returns an empty array. What's going on?

0 likes
6 replies
mikebronner's avatar

I think the problem here is that your staging server doesn't "know" its staging yet, since you declare its environment in an environment variable that is loaded depending on environment variables to be loaded.

What I do is to set the ENVIRONMENT environment variable (testing, staging, production) in the Forge configuration, then load any other environment variables using the .env.php files. See if that does it for you.

Ortix's avatar

It's not a separate server just to be clear :) It's 1 server with 2 sites. The test/staging (they are the same site, bad naming i know) and the production.

I did set the ENVIRONMENT variable in every possible way on the staging site but it just won't load anything. It's in the nginx config file as well.

Ortix's avatar

I cleared all ENV stuff on my staging/test site and created a new, single ENV variable: DB_HOST.

This created 2 files:

.env.php
.env.staging.php

The .env.php file is just an empty array, the .env.staging.php file contains:

<?php return array (
  'ENVIRONMENT' => 'staging',
  'DB_HOST' => '104.236.56.183',
);

Still the $_ENV variable is empty T_T

EDIT:

if I do this:

 php artisan tinker --env=staging

the $_ENV variable does contain all my (Database) variables...

mikebronner's avatar

Just to be clear: have you gone in Forge Site>Site Details>Environment and set the following:

Forge Settings

If you did that and it still doesn't work, have you verified your environment detection is set up correctly (in /bootstrap/start.php for Laravel 4.2). If so, I'm at a loss as well.

Are you using Laravel 5? If so, it may be a bug, since things are in constant flux in preparation for release.

1 like
Ortix's avatar

I got it to work by removing all the env stuff and starting from scratch. However, I realized that it simply did not work in artisan. Which is a bummer because I was spending so much time testing it in there :')

mikebronner's avatar

The way you get artisan to recognize the correct environment is by implementing the appropriate environment detection in /bootstrap/start.php:

$env = $app->detectEnvironment(array(

 'local' => array('homestead'),

));

That will detect correctly in homestead, as it uses the machine name for detection, and not a domain name. To get it to work with both, you need to use a combination tactic. :)

Please or to participate in this conversation.