How to get PHP to be able to read system environment variables

I use PHP as PHP-FPM for Centos. I am trying to follow http://12factor.net/ recommendations for saving settings in environment variables.

I created a file in the /etc/profile.d file that sets the environment variables that I want and the environment variables appear when testing in the CLI via Bash, i.e. Bash script is executed:

echo $SOME_SERVER_SETTING 

shows the correct result.

I set clear_env to false and variables_order in EGPCS , however the set variable is not displayed in PHP either getenv('SOME_SERVER_SETTING') or does var_dump($_ENV)

What other parameters need to be set to allow PHP-FPM to get all server environment variables, and in particular those that are installed through the shell script in /etc/profiles.d on Centos?

+9
source share
4 answers

For security reasons :-)

See /etc/php5/fpm/pool.d/www.conf (debian location may differ from CentOs)

 ; Clear environment in FPM workers ; Prevents arbitrary environment variables from reaching FPM worker processes ; by clearing the environment in workers before env vars specified in this ; pool configuration are added. ; Setting to "no" will make all environment variables available to PHP code ; via getenv(), $_ENV and $_SERVER. ; Default Value: yes ;clear_env = no ; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from ; the current environment. ; Default Value: clean env ;env[HOSTNAME] = $HOSTNAME ;env[PATH] = /usr/local/bin:/usr/bin:/bin ;env[TMP] = /tmp ;env[TMPDIR] = /tmp ;env[TEMP] = /tmp 
+3
source

Tested on Centos 7 with Systemd and PHP 5.6.4

OPEN

 /etc/php.ini 

FIND

 variables_order = "GPCS" 

CHANGE TO

 variables_order = "EGPCS" # http://php.net/manual/en/ini.core.php#ini.variables-order 

OPEN

 /etc/php-fpm.d/www.conf (maybe /etc/php5/fpm/pool.d/www.conf) (do not confuse with /etc/php-fpm.conf) 

FIND IF EXIST

 clear_env = yes 

REPLACE OR ADD

 clear_env = no # http://php.net/manual/en/install.fpm.configuration.php 

OPEN

 /etc/environment 

ADD

 #any variables you need, for example: MY_VAR=1234 

TAKE IN THE SHELL FOR CHECK

 source /etc/environment echo $MY_VAR # 1234 

SHELL RUNNING

 ln -fs /etc/environment /etc/sysconfig/php-fpm systemctl daemon-reload && service php-fpm restart 

... TESTING

OPEN

 index.php # in your project folder, running with php-fpm 

ADD

 var_dump(getenv('MY_VAR'), $_ENV['MY_VAR']);exit; 

LAUNCH IN THE BROWSER

 http://mylink.to.project/index.php string(4) "1234" string(4) "1234" 

ENJOY!

+12
source

You need to read the environment variables from the correct location. PHP creates a super global variable for this: $_ENV Thus, you can access a single variable by accessing a specific element from this variable that contains an array: echo $_ENV['SOME_SERVER_SETTING'];

I do not know why your example above should work on the CLI. The super global variable is a documented location and works.

What could be your problem in that the variables are not configured for the http server process. Typically, scripts like those found in /etc/profile.d are executed during login. Therefore, when the user creates a session within the system. But this will never happen for an HTTP server running as a system server. No login is executed, the script profile is not running, environment variables are not set.

To solve this problem you can

  • set environment variables inside startup script service
  • set variables inside host configuration or .htaccess style .htaccess
  • auto-prepend a script setting variables
+1
source

Environment variables in FastCGI configuration are set as input by an FPM process client, such as NginX. They are sent as headers to the FastCGI server, which interprets them and accordingly sets them for reading using getenv.

If you really use NginX, look for fastcgi_param directives. You can also set environment variables in your php5-fpm pool configuration, depending on your use case.

+1
source

All Articles