Cannot retrieve environment variables with getenv()

Lauris

Member
Aug 17, 2016
6
1
51
Latvia
cPanel Access Level
Root Administrator
I added environment variables to /home/user1/.bashrc and they are available with printenv command but not in PHP with getenv().
What I am doing wrong?

Content of ".bashrc":

Code:
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi


export SENTRY_ENVIRONMENT=example
export SENTRY_DSN=https://example
Content of php file:

PHP:
<?php
echo getenv('SENTRY_DSN');
?>
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,498
1,970
363
cPanel Access Level
Root Administrator
Hey there! This isn't really a cPanel issue, but I think we can get this working for you!

You need to declare the variable first - try something like this in the PHP file:

Code:
<?php

$value = getenv('SENTRY_ENVIRONMENT');
echo "$value"

?>
That should print out "example" if things are working as they should in the .bashrc
 

Lauris

Member
Aug 17, 2016
6
1
51
Latvia
cPanel Access Level
Root Administrator
Hey there! This isn't really a cPanel issue, but I think we can get this working for you!

You need to declare the variable first - try something like this in the PHP file:

Code:
<?php

$value = getenv('SENTRY_ENVIRONMENT');
echo "$value"

?>
That should print out "example" if things are working as they should in the .bashrc
It does not work. Empty output.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,498
1,970
363
cPanel Access Level
Root Administrator
In my test, I have the following in the .bashrc file:

Code:
export RANDOM_VALUE="stuff"
and then this code in the phptest.php file:

Code:
<?php

$value = getenv('RANDOM_VALUE');
echo "$value"

?>
With both of those things in place I am able to execute the script:

Code:
$ php phptest.php
stuff
If that isn't working, there is likely some misconfiguration in your environment.
 

Lauris

Member
Aug 17, 2016
6
1
51
Latvia
cPanel Access Level
Root Administrator
From CLI it is working as it should, but if I execute that php fine in browser it does not show nothing.
Do I need Apache module for that?

I am using latest cPanel version and php 7.3 and 8.0
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,498
1,970
363
cPanel Access Level
Root Administrator
This type of programming is a bit beyond the scope of cPanel Support, as you end up getting into environment variables and whether or not they are global, local only to BASH, local to only that session, and many other things. As I am not a PHP developer, I certainly don't claim to be all-knowing in how that gets handled.

There are also ways to have PHP call a BASH script, such as what is outlined here:


so that might be a good option for you depending on what you want to accomplish.
 

Lauris

Member
Aug 17, 2016
6
1
51
Latvia
cPanel Access Level
Root Administrator
I got it working by registering my App in cPanel > Application Manager and defined env. variables there.
Not sure if this is correct way to define environment variables per hostnames, but at least it looks quite easy to set up.
 
  • Like
Reactions: cPRex