2

Im having issues accessing OS environment variables in php I have apache/php installed on a centos 6.3 image

in httpd.conf mod mod_env.so is loaded in php.ini I have set variables_order = "EGPCS" restarted httpd (many times)

in shell if I type "env" I get

DB_PORT_28017_TCP_PROTO=tcp 
HOSTNAME=c6188a8bd77f
DB_NAME=/rockmongo/db
DB_PORT_27017_TCP=tcp://172.17.0.36:27017
TERM=xterm
DB_PORT_28017_TCP_PORT=28017
DB_PORT=tcp://172.17.0.36:27017
DB_PORT_27017_TCP_PORT=27017
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/etc/php.d
DB_PORT_27017_TCP_PROTO=tcp
DB_PORT_28017_TCP_ADDR=172.17.0.36
DB_PORT_28017_TCP=tcp://172.17.0.36:28017
SHLVL=1
HOME=/
DB_PORT_27017_TCP_ADDR=172.17.0.36
container=lxc
_=/usr/bin/env
OLDPWD=/etc

which has the variables im after, however if I execute print_r($_ENV); in php I get

Array ( [TERM] => xterm [PATH] => /sbin:/usr/sbin:/bin:/usr/bin [PWD] => / [LANG] => C [SHLVL] => 2 [_] => /usr/sbin/httpd ) 

have also looked in $_SERVER & $GLOBALS.

Interestingly if I execute php -i in shell I see the env variables set correctly in _ENV

I should note im running this in a docker container, however I dont believe it is a issue as variables display correctly in #env & #php -i. I think I have a issue with my httpd/php config

Anyone have advice for this? thanks

dwitz
  • 1,231
  • 3
  • 12
  • 16

3 Answers3

2

I ended up having a few options

  1. if docker container needs to run multiple services, setting env vars to /etc/environment will make them available for all users. I added the following line to my Dockerfile CMD

    CMD ["env | grep _ >> /etc/environment"]

  2. if docker container runs a single service, its best to set the entry point to the desired application, env vars will automatically be passed to application user. this is my Dockerfile CDM & ENTRYPOINT to run apache

    ENTRYPOINT ["/usr/sbin/httpd"] CMD ["-D", "FOREGROUND"]

taco
  • 1,367
  • 17
  • 32
dwitz
  • 1,231
  • 3
  • 12
  • 16
  • 1
    This doesn't work for `php-fpm` however; there you have to manually add environment variables to php-fpm.conf (see http://stackoverflow.com/questions/15428872/environment-variables-and-php), or do this in a script. – jmetz Jun 19 '14 at 12:05
1

Dagon is correct.

Unless you logged in as your web server User (apache?) you may not see the same environment variables. You can see them easily with a phpinfo test file though:

<?php
phpinfo();
?>

Or you can set your own with a .htaccess file:

SetEnv HTTP_MY_VARIABLE "my value";

From dwitz: You can also make the environment variables available system wide with this:

env | grep _ >> /etc/environment

Sorry can't comment yet... So had to create an answer.

arikin
  • 188
  • 11
  • phpinfo shows the same variables as print_r($_ENV); the environment variable is dynamic each time I start the docker image , so I cant set manually. Are you suggesting php cant access the env variables because it is running as apache user? where my shell command 'env' is executed as root – dwitz Jan 22 '14 at 03:12
  • Shell environment variables are set for the user you are logged in as. Which may be different from what php running as the apache user, will see. On the info page look under the PHP Variables section. http://www.php.net/manual/en/reserved.variables.server.php – arikin Jan 22 '14 at 03:38
  • You are correct about user/environment variables availability. if I execute `runuser -l apache -c 'php -i'` or `runuser -l apache -c 'env'` I can not see the environment variables I am after. they seem to only be available for root – dwitz Jan 22 '14 at 03:54
  • On the phpinfo page look under the Apache Environment section. There is an Indices of the elements on the PHP manual link I gave above. – arikin Jan 22 '14 at 03:57
  • Not sure but is MongoDB suppose to set that variable DB_PORT_28017_TCP_ADDR? Like MONGODB_ADDRESS? If so you may want to put that into a config.php file or autoload a class that finds it and sets it for the duration of the session. – arikin Jan 22 '14 at 04:22
  • the variable DB_PORT_28017_TCP_ADDR is set by another docker instance. it refers to the IP address of the docker image running mongodb. I think the issue is with docker or somehow sharing environment variables across multiple users. It is possible to specify the user when starting the docker instance, so I will try setting user = apache. hopefully that will set the environment variables to the apache user – dwitz Jan 22 '14 at 04:36
  • I found a workaround. If I dump env into /etc/environment with `env | grep _ >> /etc/environment` before I start apache environment vars will be shared with the apache user. Thanks for your assistance arikin! I dod not know env vas where unique to each user – dwitz Jan 22 '14 at 04:52
  • A library such as https://github.com/vlucas/phpdotenv that imports from a text file into _ENV, etc can also help. Although it's pretty trivial code, I do prefer to use other people's tested work. – Alister Bulman Jan 22 '14 at 10:02
  • Thanks Alister, thats a handy lib. As it turns out in my case if I set docker entrypoint to httpd, environment vars are passed to apache automatically – dwitz Jan 23 '14 at 09:33
0

Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.

The library is available here: https://github.com/jpcercal/environment

As you say, the environment are already loaded. Then, to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:

<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));

Enjoy it.

João Paulo Cercal
  • 733
  • 1
  • 8
  • 12
  • I tried using the library. works fine when ran php from cli. However, I am not able to access the variables in php with apache. – Sahith Vibudhi Jun 20 '18 at 07:54
  • I don't know about your setup, but using apache you should set the environment variables like that: `SetEnv VARIABLE_NAME variable_value` or you can use another library to set this env vars from a file: https://github.com/vlucas/phpdotenv – João Paulo Cercal Jun 26 '18 at 12:42