Balena Device Variables use in PHP script

I searched the doc and tried a lot of possible syntaxes, but cannot find a way to use Balena Device Variables into my PHP scripts. The only way I found was to create a file from my startup script, running into bash and echoing into a config.php script with ${Device_var} value, but wanted to be able to use them directly into PHP.

Is this possible? Any clue? Anyone found how to?

Thanks in adavnce for sharing

Hi there,
Those variables are set as your application container environment variables by the engine. Normally, in PHP, you should be able to access them with getenv function.

If you don’t get the expected value with that function, you might want to check how the entry point of your container is organized. But first, please make sure if getenv('YOUR_VAR_NAME') works for you.

Thank you Roman for your swift reply. No, helaas, that’s one of the think I tried, but I cannot get the variable value with getenv :frowning: or do we need to use a specific syntax to compose the variable name?

Hi @theonlyzby, it would help if you could share the code you are using so we can see where the issue is. That being said the following snippet will first dump all the available environment variables and then echo out the BALENA_APP_ID environment variable so you can see the required syntax:


// Dump all available environment variables

// Get a specific environment variable

Let us know if you are still having issues.

Hi @garethtdavies,

No problem, I can provide you all infos:

  1. Program (I copied your code, but you will see I don’t get values from the Balena Device Variables):
    // Dump all available environment variables
    $val = getenv(‘BALENA_APP_ID’);
    echo(“Value =>”.$val."<=");

  2. The result:

array(2) { [“USER”]=> string(8) “www-data” [“HOME”]=> string(8) “/var/www” } Value =><=

  1. Exemple of Device-Variable

Not to mention, it returns the same empty string when trying to include GAIN or BALENA_GAIN variable.

Thank you again, Henry

Hi @theonlyzby, thanks for the info. Can you also provide the Dockerfile you are using to deploy this code? Are you running this code in an application container? Also note that if you are running in local mode you won’t have access to the device environment variables set via the dashboard but I don’t think this is the case here:

@garethtdavies, thanks for this, but no, I am running in production mode, and my dockerfile is to be found here:

And I am running it into a “Starter” Balena container

Thanks again, Henry

Hey @theonlyzby sorry could you clarify what do you mean by: running it into a “stater” balena container? Did you try running the script Gareth linked above from your /opt/

No, I am running it from my /data/www directory, through PHP, calling it from my browser, served by NGINX & PHP fastCGI. I only meant it was running into a “starter” Balena docker container, in production mode, as asked. My appologies for the introduced confusion :frowning:

Hi @theonlyzby,
Let’s first try to confirm that the environment variables reach your container.
Can you please go to the device summary page, use the webterminal to connect to your application’s container and run env in there.
That should print all the environment variables available in your application’s container (possibly including a few sensitive ones, so don’t paste them in here).
Please check whether the environment variables you did set in the dashboard appear in that list and let us know.
If they indeed appear, then the issue is probably configuring the PHP server to pick them up.

Kind regards,

Pretty nice precision about the working of these variables you give me, @thgreasi !
I indeed get them also with env in bash (so did I with ${var} in my start script).

So, I can tell you now, I have an issue anly in web mode, in PHP CLI mode, I also get all variables available…

After a bit more of research, I found that I need to modify my FPM config file (/etc/php/7.3/fpm/pool.d/www.conf), and NOT clear the env , so uncomment the last line:

; 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

… And start php-fpm with init.d and NOT use service!?!
so, I need to modify my with this:

/etc/init.d/php7.3-fpm start
instead of
service php7.3-fpm start

Thank you ALL for this assisted debug!