How to set global environment variable that applies to all pools in php-fpm?

Discussion in 'Installation/Configuration' started by cbj4074, Apr 14, 2015.

  1. cbj4074

    cbj4074 Member

    I'm attempting to define an environment variable that will be available to all pools that are defined via php-fpm.

    At first, I attempted to define the value at the bottom of php-fpm.conf, like so:

    Code:
    env[MY_VAR] = ABC123-123ABC
    
    I restarted php-fpm, and attempted to test the change with

    Code:
    var_dump($_ENV, getenv('MY_VAR'));exit;
    
    but the result is

    Code:
    array(0) { } bool(false)
    
    Further digging revealed two other configuration changes that may be necessary before this will work:

    1.) Changing variables_order default value in php.ini:

    Code:
    ;variables_order="GPCS"
    variables_order="EGPCS"
    
    2.) Changing clear_env default value in www.conf:

    Code:
    ;clear_env = no
    clear_env = no
    
    I sat back, restarted php-fpm, and thought to myself, "Surely, this will work now..." Nope! :mad:

    I thought that perhaps it is necessary to place the variable definition within the [global] section of www.conf, but then php-fpm won't start, and complains with:

    Code:
    Starting php-fpm ERROR: Array are not allowed in the global section
    
    The only means by which I've been able to make this environment variable available to the worker-pools work are:

    1.) Define the environment variable in a specific pool configuration file, e.g., web2.conf. When I do this, the variable is available using PHP's getenv() function, and is defined as a "proper" environment variable (i.e., it is listed under the "Environment" heading of phpinfo()'s output).

    2.) Define the environment variable in the web-server's (NGINX) configuration, inside fastcgi_params, e.g.:

    Code:
    fastcgi_param MY_VAR "ABC123-123ABC";
    
    With this approach, the value is not a "proper" environment variable (i.e., it is not listed under the "Environment" heading of phpinfo()'s output). It is available only via the $_SERVER superglobal, e.g., $_SERVER["MY_VAR"]. That said, the value is returned when I call getenv('MY_VAR'), because this function checks both $_ENV and $_SERVER for the requested key name.

    I don't like the first approach because I can't specify the environment variable globally; I need to be able to specify it in one place and have it propagate to all individual php-fpm pools.

    I don't like the second approach because it is needlessly broad; other CGI processes (besides php-fpm) will have access to the value. And, it makes the PHP configuration less "portable"; I don't like the idea of having to define PHP-specific configuration directives at the NGINX layer.

    Does anybody know how to define a "global" environment variable in php-fpm's configuration?

    Thanks for any tips!
     
    Last edited: Apr 14, 2015
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    What about changing the php-fpm pool master file of ispconfig, this file is used to craete all new and updated pool files. with the resync tool in ispconfig, you can also do a forced update.
     
  3. cbj4074

    cbj4074 Member

    Hi, Till! Thanks for sharing your insights.

    What you suggest should work perfectly well. I think what you describe may be the only option, because as the php-fpm error message states:

    I'm sure there's a reason for it.

    Thanks for the idea!

    P.S., a bit of a corollary:

    I had actually penned a reply to this shortly after you last replied, but my session timed-out before I hit "Post Reply", and if memory serves, I received some kind of token mismatch error that ultimately caused my post to be lost.

    This problem (losing form posts due to being forced to sign-in again, or similar) has plagued the web since its inception, so I tend to be in the habit of Select-All-and-Copy-ing my replies before I hit "Post". In the event that my post is lost, I can recreate it relatively easily. I forgot to do that in this case. :(

    If you're unfamiliar with this issue, I'll try to reproduce it and if I'm able to do so, I will post the steps.
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    I never lost a post here in the forum since we moved to xenforo in january. Xenforo even remembers when you start a post, then switch to another thread, when you come pack, your post that you havent saved is still in the post field.
     
  5. Nidhi

    Nidhi New Member

    Hi

    I am using nginx with php-fpm and facing the exact same problem here.
    In my case, I am trying to use one of the environment variable set by Elastic beanstalk for my environment, but unless I define it in nginx AGAIN, I am not able to use that variable by my php-fpm application.

    I have seen a lot of links and none of the solutions work.

    Is there a possible solution to it?
     
  6. cbj4074

    cbj4074 Member

    @nidihi

    I haven't used Elastic Beanstalk; where/how does it set the environment variable in question?
     

Share This Page