Cant access environment variables in php

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)

  • Use Php in Docker Apache
  • Want to ssh into a running docker container running inside CentOs Image
  • Docker. Send mail from host
  • Error connecting to python server running in boot2docker (centos container | Windows 7.1 host)
  • Docker centos7 image - unable to run apache
  • remote server returned 400 bad request for apache2 in ubuntu
  • 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

  • Point different domains to different Docker containers on a single EC2 instance?
  • Creating a testing infrastructure with Pytest , Selenium Grid and Docker
  • Dockerfile - CMD does not run the shell script
  • Docker Experimental Checkpoint/Restore Implement tutorial
  • docker 1.12 swarm mode: how to connect to another container on overlay network and how to use loadbalance?
  • How to add module to Wildfly using CLI
  • 3 Solutions collect form web for “Cant access environment variables in php”

    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.

    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"]

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.