20

I have an ubuntu server with a handful of custom environment variables set in /etc/environment as per the ubuntu community recommendation

When I use php from the command line I can use php's getenv() function to access this variables.

Also, if I run phpinfo() from the command line I see all of my variables in the ENVIRONMENT section.

However, when trying to access the same data inside processes being run by php5-fpm this data is not available. All I can see in the ENVIRONMENT section of phpinfo() is:

USER    www-data
HOME    /var/www

I know the command line uses this ini:

/etc/php5/cli/php.ini

And fpm uses:

/etc/php5/fpm/php.ini

I've not managed to find any differences between the two that would explain why the ENV variables are not coming through in both.

Also if run:

sudo su www-data

and then echo the environment variables I am expecting they are indeed available to the www-data user.

What do I need to do to get my environment variables into the php processes run by fpm?

sungiant
  • 3,152
  • 5
  • 32
  • 49

4 Answers4

24

It turns out that you have to explicitly set the ENV vars in the php-fpm.conf

Here's an example:

[global]
pid = /var/run/php5-fpm.pid
error_log = /var/log/php5-fpm.log

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'
sungiant
  • 3,152
  • 5
  • 32
  • 49
  • 1
    I have the same problem. I'm running php-fpm under the user who set the environment variable correctly. but I set the 'php-fpm.conf' with 'env[MY_ENV_1]=$MYENV1' which use the dollar sign. But I can read the $MYENV1. phpinfo() print out 'no value'. But when I run in CLI. I can read the correct value. – Magic Apr 15 '14 at 02:47
  • env[myvariable] worked when I placed it in the /etc/php/7.4/fpm/pool.d/www.conf file. the php-fpm.conf didn't work. – Panu Oksala Aug 18 '21 at 07:13
15

1. Setting environment variables automatically in php-fpm.conf

clear_env = no


2. Setting environment variables manually in php-fpm.conf

env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'


! Both methods are described in php-fpm.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


Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from the current environment. Default Value: clean env

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp


I found solution in this github discussion .

Andrej Kouril
  • 303
  • 4
  • 9
  • in my case clear_env option doesn't even exist in php 5.3.10, but settings var like you've showed did a trick! – holms Mar 27 '17 at 18:50
  • I would also add that in the case your `php-fpm` is started by `systemd` you may need at first to point it where to get env vars. E.g. in `/lib/systemd/system/php7.2-fpm.service` set `EnvironmentFile=/etc/environment` – origaminal Jun 09 '18 at 11:14
  • 1
    env[MYVAR] = $HOSTNAME did not work for me. Inside PHP, MYVAR got resolved to nothing while 'echo $HOSTNAME' shows a valid hostname. any ideas why? – Sagi Mann Jun 24 '18 at 13:42
1

The problem is when you run the php-fpm. The process not load the environment.
You can load it in the startup script.
My php-fpm is install by apt-get.
So modify the

/etc/init.d/php5-fpm

and add (beware the space between the dot and the slash)

. /etc/profile

and modify the /etc/profile to add

. /home/user/env.sh

In the env.sh. You can export the environment whatever you need.

Then modify

php-fpm.conf

add env[MY_ENV_VAR_1] = 'value1' under the [www] section.
Last. restart the php-fpm. You'll get the environment load by the fpm.

Magic
  • 1,182
  • 2
  • 18
  • 28
1

Adding on to the answers above, I was running php-fpm7 and nginx in an alpine:3.8 docker container. The problem that I faced was the env variables of USER myuser was not getting copied into the USER root

My entrypoint for docker was

sudo nginx  # Runs nginx as daemon
sudo php-fpm7 -F -O  # Runs php-fpm7 in foreground

The solution for this was

sudo -E nginx
sudo -E php-fpm7 -F -O

-E option of sudo copies all env variables of current user to the root

Of course, your php-fpm.d/www.conf file should have clear_env=no

And FYI, if you're using a daemon service like supervisord they have their own settings to copy the env. For example, supervisord has setting called copy_env=True

theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26