When setting env variables, how to get them using PHP?

November 6, 2015 9.3k views
DigitalOcean PHP

I’m using this command to set environmental variable
export COMMIT_ID='x'

When I use printenv I can see it defined. However, PHP can’t see it using getenv('COMMIT_ID')… What am I doing wrong?

3 Answers

Hi there!

What is probably happening is that you set the environment variable with a regular system user (the one you are logged in), but as the web server runs with another user (usually www-data), you don’t have access to that variable from inside your PHP code.

If you are using Ubuntu, you might try setting the variable in the file /etc/environment, so it will be available to all users in the system. This might help: https://help.ubuntu.com/community/EnvironmentVariables#System-wideenvironmentvariables

you can set all your environmental variables in /etc/environment and access them from php using getenv function

Have another answer? Share your knowledge.