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 is loaded
in php.ini I have set variables_order = “EGPCS”
restarted httpd (many times)

  • How to Dockerize a PHP app
  • Apache/php return full php error track description
  • Sendmail in DOCKERized PHP-Application doesn't work
  • How do I change the umask of a shared docker volume?
  • Docker CentOS image does not auto start httpd
  • Run docker inside a docker container?
  • in shell if I type “env” I get


    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?

  • Use Prometheus “target relabeling” to extract cAdvisor's Docker image name without tag
  • DLLs to include in mkboundle to create an standalone exe for debian:wheezy
  • How to build and start containers on multiple docker machines
  • Default elasticsearch configuration for docker container
  • Makefile fails executing sudo docker kill
  • Bunyan logger on aws leaks file descriptors
  • 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:


    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:

    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:

    // ...
    require "vendor/autoload.php";
    // ...

    Enjoy it.

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