Report this

What is the reason for this report?

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

Posted on November 6, 2015

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

Thank you, will try

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.