0

I'm facing an issue where php or apache is unable to load variables from /etc/environment, and only if php code is run trough browser.

I have the php-fpm/www.conf clear_env set to no

clear_env = no

and line added to /etc/sysconfig/php-fpm

EnvironmentFile=/etc/environment

After restart I'm able to see variables when running

php -r "print_r(\$_ENV);"
sudo -u apache env
sudo -u apache php -r "print_r(\$_ENV);"

But if I open test.php in the browser they are not there

<?php
 $output = shell_exec('env');
 echo '<pre>';
 echo $output;
 print_r($_ENV);
 print_r($_SERVER);
 print_r(getenv());
 echo '</pre>';
 phpinfo();
?>

I cannot for my life figure out why it only fails on browser where clearly apache user can see variables from /etc/environment

UPDATE

I was unable to work out why it would work on one vm and not the other. So decided to go a different way. I now inject environmental variables to /etc/php-fpm.d/env.conf when there is a change in /etc/environment, using below script:

fpm_file="/etc/php-fpm.d/env.conf"
if [[ ! -f "$fpm_file" ]]; then
    sudo touch $fpm_file
    echo '[www]' | sudo tee -a $fpm_file > /dev/null
fi
i=0;
if [[ -f "$fpm_file" ]]; then
    vars=$(sudo grep -v '#' /etc/environment | sed '/^[[:space:]]*$/d' | sed -e 's/^/env[/' | sed 's/=/]=/g')
    while read -r line; do
        chk=$(grep -F "$line" $fpm_file | wc -l )
        if (( "$chk" == 0 )); then
            echo "$line" | sudo tee -a $fpm_file > /dev/null
            ((i++))
        fi
    done <<< "$vars"
    if (( "$i" > 0 )); then 
        echo "New enviromental variables found, added to php, restarting php-fpn..."
        sudo service php-fpm stop
        sleep 5
        sudo service php-fpm start
        echo "All done!"
    fi
fi

I run that script via .bashrc on login. That works well and I can maintain env vars in one file instead of multiple. Not adding this as answer as the original issue is still present and unresolved.

Blackegg13
  • 35
  • 7
  • try with getenv and share the result – JRichardsz Nov 27 '22 at 02:54
  • I have tried with getenv, you can see in test.php,it gives same output as $_ENV. – Blackegg13 Nov 27 '22 at 08:51
  • Are you developing a php script or a web? How do you plan to run it ? – JRichardsz Nov 27 '22 at 14:18
  • getenv needs an argument like: getenv("some_key"). I tried your script and it prints all the registered env vars https://user-images.githubusercontent.com/3322836/204140715-c868fa38-db6c-4274-b135-88e2051868f8.png – JRichardsz Nov 27 '22 at 14:35
  • Yes it prints what it can access. I did it that way to see if the variables are even available. They are not. But somehow on my other vm, a virtual copy, they do show up. I did as well checked the php option variables_order and it is set to "EGPCS". Nothing. I'm unable to move to docker unfortunately, outside my power. What is really baffling is the fact that directly on command line running the php code as apache user does show my variables, riddle me that – Blackegg13 Nov 27 '22 at 15:49
  • Same php code shows all my variables on the other vm. – Blackegg13 Nov 27 '22 at 15:53

1 Answers1

1

You are using php script from browser means you are using web server like apache or nginx. If you are using apache then you need to set env varaibles in apache vHost file using SetEnv keyword like below

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin abc@example.com
        ServerName api.example.com
        ServerAlias api.example.com
        DocumentRoot /vagrant/codebase/example

        SetEnv APPLICATION_ENV production
        SetEnv APPLICATION_CONFIG user

    </VirtualHost>
</IfModule>

.

In nginx you need to set like below

location / {
...
   fastcgi_param   APPLICATION_ENV  production;
   fastcgi_param   APPLICATION_CONFIG user;
...
}

Hope this helps

Crue
  • 21
  • 2
  • Ok, let me throw a curveball, I have two identical vm's with apache, I have checked setup on both and they are exactly the same. One of them can access environmental variables the other can't. So there must be a setting or something I'm missing on affected vm – Blackegg13 Nov 27 '22 at 08:48
  • Your issue is common in manual infrastructures. Is docker and option for you? – JRichardsz Nov 27 '22 at 14:41