Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No environment variables are available via PHP-fpm+nginx

I tried to modify php.ini in the following way:

variables_order = "GPCSE"
register_globals = On

But the required PATH variable is neither in $_ENV nor accessible via getenv('PATH').
I'm running Nginx + PHP-FPM on Ubuntu 10.04.

Note: executing the following command in console gives a correct result:

php -r "echo getenv('PATH');"

I guess that PATH is environment variable of bash, but as long as php-fpm not starting via bash it doesn't have required variables. Any way to include them?

Thanks.

Update#1: As temporary solution I found out that PATH variable stored in '/etc/environment' file. So I just going to read it from there. If someone needs a code:

preg_match('/^(PATH)="?([^"]+)"?$/i', file_get_contents('/etc/environment'), $match);
putenv($match[1]."=".$match[2]);
like image 569
Sat Avatar asked Oct 29 '13 13:10

Sat


2 Answers

I came across this problem when upgrading my OwnCloud installation to version 8.1.

They describe a fix in their documentation

Summary is: Locate your www.conf file in your php5-fpm config folder (for Ubuntu this is /etc/php5/fpm/pool.d/www.conf) and uncomment the needed env[PATH] line.

Optionally update the content of the variable with the output of php -r "echo getenv('PATH');"

like image 101
heine Avatar answered Sep 21 '22 00:09

heine


By default, PHP-FPM clears the environment variables (from the 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

You can uncomment the last line to set clear_env to no, but if you prefer, you can set only the environment variables needed:

env[PATH] = $PATH
like image 24
AymDev Avatar answered Sep 19 '22 00:09

AymDev