11

I've defined some environment variables like APP_ENV in my /etc/environment file, on my ArchLinux.

If I type printenv, I see them.

I've created this simple test file called… test.php

<?php var_dump(getenv('APP_ENV')); var_dump(getenv()); 

If I run php test.php, everything is OK, I see my ENV variables.

But when I try to access the file via HTTP… there is nothing in my env!

Of course, I've changed the config of /etc/php/php-fpm.d/www.conf to set clear_env = no

These are the affected lines:

; 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 

And I've restarted both php-fpm and nginx services but… still nothing in my env. Script return bool(false).

So… Am I missing something ?

This is my php-fpm version:

php-fpm --version PHP 7.2.6 (fpm-fcgi) (built: May 26 2018 07:45:18) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies 

And my Nginx version

nginx -v nginx version: nginx/1.14.0 

What should I do to access my env variables in a PHP-FPM context ?

Thanks a lot!

1
  • I am experiencing the same problem. Commented Jun 13, 2018 at 18:22

4 Answers 4

9

You can set the environment variable in /etc/php/php-fpm.d/www.conf like this: env[APP_ENV] = development Then you'll be able to get it with getenv('APP_ENV') like you expected.

4
  • 5
    This works, but I don't think the www.conf file is meant to be used like that Commented Sep 21, 2019 at 14:45
  • This blows my mind. I'm not going to put my secrets into a web config file! Commented Mar 22, 2022 at 17:04
  • I agree. Sensitive information shouldn't go there and my suggestion to add it to a .conf file was a quick way to achieve what they needed because it was a server related setting (APP_ENV).I suspect that the original problem is related to which user PHP-FPM is running as Commented Mar 23, 2022 at 16:07
  • The PHP-FPM user is probably not sourcing the environment variables. I can't really come up with another way of doing this. I usually use Kubernetes, which injects the env vars from the pod definition and they do work with all users, in my experience. One thing you can try is to modify the systemctl service to source and export the variables before launching the server, but it's rather hacky. Commented Mar 23, 2022 at 16:16
2

When you type printenv or php test.php, you see environnement variables because they exist.

When you "try to access the file via HTTP… there is nothing in [your] env". Exactly your environnement variables are not set.

Why would you expect a different behaviour? Files like /etc/environment, /etc/profile and /etc/bashrc are only sourced when you use a shell, not when a daemon is ran.

1
  • 2
    how would you centralize env var to use both for php cli scripts and php scripts executed by a webserver? Commented Mar 25, 2021 at 16:02
1

I have worked out something in my production environment that should work. It is an extension of using the php-fpm config files to add environment variables. You can embed environment variables into that config file, so instead of putting secrets directly into the php-fpm config file, it acts as a filter.

[www] env[HOSTNAME] = ${HOSTNAME} env[MODE] = ${MODE} php_admin_value[error_log] = /path/to/logs/PHP-${HOSTNAME}-error.log 
1
  • Can you please update your answer to say where the php-fpm config file is that this text should go in? Commented 17 hours ago
0

A possible workaround is to create a php file such as

<? $_SERVER['APP_ENV]='dev'; 

somewhere on disk, and have it included via php.ini setting auto_prepend_file. Not too clean, but it might help avoiding storing secrets in php-fpm config...

Rationale:

it seems the php documentation for fpm config is not very clear about how clear_env and env supposed to work in the pool config file.

In my own experience:

  • the environment variables which will be made available to the workers when clear_env = no are the ones for the user defined in the pool, which is generally not the root user
  • defining environment variables for that user in places such as .bashrc will not work, as the worker processes will not read the same environment configuration scripts as a shell does
  • it seems that, depending on how php-fpm is actually started (via an init.d script / systemd / direct invocation of the executable via f.e. a custom Docker CMD), the env variables might, or not, show up

Adding insult to injury, it is very hard to get the documented syntax env[APP_ENV] = $APP_ENV to work, so the only way to have a value show up is to harcode it in fpm pool configuration. But that might be not the best solution for all scenarios

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.