Getting environment variables in PHP-FPM with Nginx

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!


Solution 1:

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.

Solution 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.