Environment variables ignored by PHP-FPM
Wondering why PHP doesn’t see your environment variables? Why getenv('var')
won’t return a value while it does on the commandline?
You’re probably using PHP-FPM.
Cause: The environment, with all its variables, is cleared for FPM. This is default behavior.
This is done in the pool-configuration.
Let’s get that out of the way. It’s actually quite easy.
Solution: Uncomment the line which says clear_env = no
in your pool config like fpm/pool.d/www.conf
:
; 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
Found it on this pretty old thread dating back to 2015 on GitHub.