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 enable SSL in docker with nginx hosted in Ubuntu
  • How do I Configure a Docker Registry V2 with Apache and a CA certificate?
  • Apache2 in docker container gives 403 on statically served files
  • How can I increase timeouts of AWS worker tier instances?
  • Docker on Cent OS 6 fails to build container after switching to different container directory
  • Using Kubernetes or Apache mesos
  • 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?

  • Docker command with env var prepended (local env var)
  • How to add filter in fluentd
  • aws ecs 403 error to login private registry
  • Meteor, docker and SSL on localhost
  • Running a shell script that runs a python program, then an R program
  • Cleaning half-downloaded docker image
  • 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.