2

I have some problems setting a environment variable in the php-fpm pool config file.

When I set env[SOMENAME] = somevalue in my /etc/php5/fpm/pool/www.conf file php can not get the value with $_SERVER['SOMENAME']. Are there some config settings I have to set in apache/php to get the value from that environment variable?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sili
  • 175
  • 4
  • 14
  • I think you are pulling the cart before the horse. The title of your question is about setting environment variables in Apache and seeing the effect in PHP-FPM. But your actual question appears to be setting environment variables _exclusively_ in php-fpm. Which is it? I'm going to answer the title, because this is what google finds – Otheus Sep 17 '19 at 20:45

3 Answers3

2

Merge Fiete's answer with Kufner's and you get the correct one.

Somewhere in Apache, you want to set an environment variable somehow:

SetEnv SOME_VAR "a value"

or with Rewrite:

RewriteRule ^ - [E:SOME_VAR="a value"]

Your PHP script (according to phpinfo()) will have this variable for you in the $_SERVER array.

Otheus
  • 785
  • 10
  • 18
0

You can set your environment variables in /etc/apache2/envvars. Only add the following line in the config file and restart your apache.

export SOMENAME=somevalue

after that you can access it via $_SERVER['SOMENAME'] in PHP.

Fiete
  • 1,332
  • 11
  • 12
-1

In Apache configuration, you can use SetEnv directive. And for PHP FPM, you have it right.

But environment variables are not available in $_SERVER, they are in $_ENV variable. So you should see your somevalue in $_ENV['SOMENAME'].

Josef Kufner
  • 2,851
  • 22
  • 28
  • This is just wrong. `$_SERVER` is the primary place to read environment variables. Yes, it is very poorly named. Also see this: https://stackoverflow.com/questions/3780866/why-is-my-env-empty – jlh Jan 14 '22 at 08:53
  • No, see https://www.php.net/manual/en/reserved.variables.environment.php. But if you run PHP as module in Apache, the env variables are emulated and it may be a bit different. If you run PHP as CGI, FPM, or CLI, then you get `$_ENV`. The `$_SERVER` contains much more data than only the env variables so you cannot be sure from where the values arrived. – Josef Kufner Jan 14 '22 at 13:16
  • Yes, `$_SERVER` contains additional variables, not just the environment. But the environment is always in there, which is unfortunately not true for `$_ENV`, depending on the PHP configuration. – jlh Jan 14 '22 at 15:22