Project

General

Profile

[Solved] Unable to read env used in setenv.add-environment on php-fpm server

Added by Grundor over 6 years ago

I'm trying to set up a Lighttpd + php-fpm env where the php-fpm is running on a separated machine.

The application requires env variables set by vhost. When I used unixsocket it worked very well but php-fpm over tcp its not working as expected.

At Lighttpd configuration I have:

These configs:

- fastcgi.conf:
    fastcgi.server = ( ".php" =>
                        (
                            (  
                               "host" => "10.0.0.114",    
                               "port" => "9000",   
                               "check-local" => "disable",    
                               "disable-time" => 0,  
                               **"bin-environment" => ("MY_VAR" => "FOO"),  
                               "bin-copy-environment" =>   ( "PATH", "SHELL", "USER","LD_LIBRARY_PATH","APPLICATION_ENVIRONMENT","APPLICATION_REV"),**  
                               "broken-scriptfilename" => "enable",  
                               "fix-root-scriptname" => "enable",  
                               "x-sendfile" => "enable",  
                               "allow-x-send-file" => "enable"  
                             )
                           )
                   )
- vhost.conf:
    (...)  
    **setenv.add-environment = (  
          "APPLICATION_ENVIRONMENT" => var.env_name,  
          "APPLICATION_REV" => var.tdp_rev  
    )**  
    (...)
- php-fpm.conf
    [www]  
    listen = 10.0.0.114:9000  
    listen.allowed_clients = 127.0.0.1,10.0.0.101,10.0.0.114  
    user = centos  
    group = centos  
    clear_env = no  
    chroot = /  
    chdir = /app/www  
    **env[HOSTNAME] = $HOSTNAME  
    env[APPLICATION_ENV] = $APPLICATION_ENV  
    env[APPLICATION_REV] = $APPLICATION_REV  
    env[MY_VAR] = $MY_VAR**  

When I try to get this env on PHP using getenv [http://php.net/manual/function.getenv.php] or $_ENV these variables are empty.

If it's a limitation because of php and lighttpd is running in distinct servers, there is another way to transfer information from vhost (lighttpd) to php such as happens with SERVER_SOFTWARE variable?


System Info:
lighttpd: lighttpd/1.4.45
OS: CentOS7


Replies (3)

RE: Unable to read env used in setenv.add-environment on php-fpm server - Added by gstrauss over 6 years ago

The code in lighttpd which produces the FastCGI request is the same code whether the backend socket is a unix domain socket or a remote TCP server, so I expect the problem is elsewhere.

https://serverfault.com/questions/813368/configure-php-fpm-to-access-environment-variables-in-docker
suggests trying getenv() or $_SERVER in your PHP instead.

https://github.com/docker-library/php/issues/74
describes trouble accessing environment variables when running php-fpm under docker. (See also the links within)

BTW, for security reasons, it is generally discouraged to pass PATH SHELL LD_LIBRARY_PATH environment variables.

RE: Unable to read env used in setenv.add-environment on php-fpm server - Added by Grundor over 6 years ago

Thanks for your time to answer my question.

The problem happened because I have a second setenv.add-environment inside of $SERVER["socket"] == ":443" condition, the second setenv.add-environment call overwirde all previus add envs.

For example:

 (...)  
    setenv.add-environment = (  
          "APPLICATION_ENVIRONMENT" => var.env_name,  
          "APPLICATION_REV" => var.tdp_rev  
    )
    (...)
    $SERVER["socket"] == ":443"{
      setenv.add-environment = (  
          "HTTPS" => "on",  
          "REQUEST_SCHEME"=> "https" 
     )
    }

Is it an expected behavior or a bug?

    (1-3/3)