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?

1
  • 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 Commented Sep 17, 2019 at 20:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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'].

3 Comments

This is just wrong. $_SERVER is the primary place to read environment variables. Yes, it is very poorly named. Also see this: stackoverflow.com/questions/3780866/why-is-my-env-empty
No, see 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.