How to set global environment variables for PHP

What you want to do is use an Apache configuration file. You will need access to a configuration folder and the httpd.conf file (or modified version). You can then configure the httpd.conf to dynamically load configuration files using this approach

Include conf.d/*.conf

Inside the conf.d folder you place your specific environment configuration files.

server-environment-dev.conf example:

SetEnv ENVIRONMENT "local"

server-environment-prod.conf example:

SetEnv ENVIRONMENT "production"

These settings will show up in your php code as available environment variables. This approach allows you to keep your vhost files, .htaccess, and your other configuration files agnostic of the environment.

etc/environment, etc/profile.d, .bash_profile, .profile, etc files are not readable by PHP in Apache due to some process/user limitations. You can bash, smash, crash the variables all you want and see them set in your terminal but they will not show up in phpinfo() unless you set it via one of the Apache configuration files.

For NodeJS you can start the app passing in your environment variable or you can set the NODE_ENV in multiple ways include your .bash_profile and possibly etc/environment file if you want to be user agnostic.

A good read for Node:
http://www.hacksparrow.com/running-express-js-in-production-mode.html

Leave a Comment