0

I'm trying to query environment variables that i have set my .bashrc file (i'm running Ubuntu 14.04) in a PHP scripts that run's under Apache server.

When i query the getenv('MY_VAR_NAME') or $_ENV['MY_VAR_NAME'] while accessing the page, it seems that those variables are missing..

My guess is: when the script is been executed the user is www-data, so 'MY_VAR_NAME' is not accessible.

Is there any procedures / best practices to this kind of problems ?

Thanks

1
  • you could set an apache-level env var, e.g. SetEnv MY_VAR_NAME value_goes_here in a .htaccess or .conf file. Commented Feb 18, 2015 at 15:13

1 Answer 1

1

My guess is: when the script is been executed the user is www-data, so 'MY_VAR_NAME' is not accessible.

Your guess is correct. :)

Is there any procedures / best practices to this kind of problems ?

What most programmers would do is to have a configuration file containing these variables. You would store the configuration file in some area where the PHP scripts could get to it -- whether that's in a home directory somewhere, in /etc. or in some other place such as in the web root or a directory near it.

Different frameworks take different approaches to the format of the configuration files -- some are PHP scripts, some are YAML files, some are windows/DOS format INI files, some are XML, some are JSON, etc.

Personally I like the idea of doing this:

  • Store in a simple INI file only the configuration required to access the database.
  • Store all of the remaining configuration in a database table, and build an editor for the parts that you need to be able to edit.
  • Cache the contents of the configuration database table in memcached.

Whichever method you use is up to you, however, and will vary depending on the needs of your application.

Sign up to request clarification or add additional context in comments.

4 Comments

First thanks for the detailed answer! seems like adding the environment variables to the "/etc/apache2/envvars" did the trick. Your solution seems useful and scaleable, but it's a bit overhead for what i need. Do you think that there is any risk in adding the variables to the apache/envvars ?
The main issue would be that the envvars file is server wide, so that if you ran multiple sites on the same server it wouldn't be any good because they would all have the same config.
That's actually works for my as those are external services related variables and they will be valid to all sites that are configured on the web server
In that case you have found your answer. As I said "whichever method you use is up to you".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.