29

I have read the question/answers here but I don't understand how to set variables in /etc/environment. If I edit the file, do I need to restart my machine or simply log out my current user (or log in a new one?).

I want to set a global variable to denote that websites on my machine are in 'development' or 'testing' mode. I don't want to have to set this for every project (whether it uses PHP, Java/Tomcat, NodeJS, etc). I'm aware that (for Apache) I can set the environment variable in the following ways:

  1. directly from php with putenv() (this seems useless since I want to avoid logic that tries to figure out what server the files are on)
  2. using .htaccess SetEnv ENVIRONMENT 'local' (this would require me to duplicate this file/code for every server, not ideal)
  3. using a Virtual Host directive SetEnv ENVIRONMENT 'local' (if I'm using a virtual host, which in nearly all cases I am, but again requires me to copy/paste code over and over again)
  4. in httpd-conf SetEnv ENVIRONMENT 'local' (this will only apply to apache, and I would like it to apply to NodeJS servers as well)

I'm not saying I can't do #4 (and apply #3 selectively to NodeJS servers). But I'm thinking that this is a good reason to use /etc/environment. As I said above, I have edited the file (after first creating it) and tried the following combinations, none of which seemed to work:

ENVIRONMENT='local'
ENVIRONMENT=local
export ENVIRONMENT='local'
export ENVIRONMENT=local

I say that none of them worked because I did not find the variable in output from:

print_r($_SERVER);
print_r($_ENV);
echo(getenv('ENVIRONMENT'));
Community
  • 1
  • 1
alyda
  • 559
  • 1
  • 8
  • 17
  • I am having a similar problem and could not find a proper solution. The issue seems to be that the environment variables that you see via the console don't persist on the server and are therefore not available by your application. It would be great if someone could give advice how to persist these variables. (Set variable on deployment time, read it on runtime) – PepeNietnagel Oct 03 '20 at 21:20

3 Answers3

53

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

nopuck4you
  • 1,730
  • 14
  • 20
2

So I would assume you have a global config file somewhere. Why not put a constant in that file that you can change? Would be far easier that trying to set something on the server level.

define('ENVIRONMENT', 'testing');
if(ENVIRONMENT == 'testing') {
   echo 'We\'re just testing';
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 3
    Yes, but that file is tracked in git and pushed via git to a server. I could put that variable in a separate, untracked file but I would have to do that for all of my local websites, which is why I figure that's the point of something like /etc/environment, its for my entire environment and should be accessible to all of my sites within the environment of my machine. Since I use heroku, it's super simple to set an environment variable `$ heroku config ENVIRONMENT=staging` it should be just as easy to do so on my local machine (and have the setting be permanent). – alyda Oct 31 '13 at 17:36
0

If you still can't get your environment variables:

you may need to edit your real httpd.conf in

~/Library/Application Support/appsolute/MAMP PRO/

instead of

/Applications/MAMP/conf/apache/

Also you may need to use getenv() instead of $_ENV

Sebastien Horin
  • 10,803
  • 4
  • 52
  • 54