Environment Variables ( PHP Deployment ) - Need Help / Any Advice?

Hi,

I’m having a bit of a tough time with something on this great platform of yours and was hoping someone here might be able to lend a hand. I’m struggling to set up an environment variable for my MongoDB connection, and it’s been a bit of a headache.

I did reach out to support, and they got back to me asking if I’m still facing the issue or if I’ve figured it out by now. I’m not sure how to respond to that - still stuck, unfortunately! I’m not the type to bother support without good reason, but I guess we all need a little extra help sometimes.

Here’s what I’ve been trying to do:

  1. I went into the ‘Deployments/Environment’ section and put in MONGO_HOST='MY SECRET STRING'.
  2. Hit the ‘Sync’ button after that.
  3. Then, I deployed the app in the deployments section.
  4. In my PHP script, I’m trying to get the variable with getenv('MONGO_HOST').

No luck though, it’s coming up empty no matter what I try - single quotes, double quotes, you name it. It’s been over two days of trial and error now.

I’m hoping someone here might have run into something similar or knows a trick or two. Any advice or tips would be super appreciated!

Thanks a bunch in advance!

Cheers.

P.S. It’s so strange, because the rest of the platform has been extremely intuitive and easy to use! This issue really stands out as an anomaly.

Hello @webify,

Welcome to the Cleavr forum!

For your PHP project, do you have any packages installed that reads from a .env file? Such as the dotenv package as an example - symfony/dotenv - Packagist

Just wanted to check that first since some frameworks don’t have that installed out of the box.

Hi and thank you for the welcome and answer.
no i do not use any packages, vanilla php.
right now i just want to get it to work to understand how your platform works and when i have a grip on it i will use more sophisticated methods.
for now i simply use this:
i tried to boil down the probem and added some error reporting and it fails reading the .env file
“No .env file found”

if (!file_exists(DIR . ‘/.env’)) {
// .env file not found
exit(‘No .env file found’);
}

NGINX and PHP_FPM doesn’t know about the $_ENV variables. So you can install a library like Adam suggests or you can try to set them in .php block from your Nginx config:

fastcgi_param SOMEENV test;

But it’s better to use a PHP library. I use this standalone project: GitHub - vlucas/phpdotenv: Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.

1 Like

Thank you
I had a PATH problem/error once i corrected the PATH used the right one it all fell into place an worked.
thanks a lot for your help!