6

i'm trying to setup my project in azure. For this i created an app service and under settings/configuration i added some "Application Settings", which in my understanding are environment variables. However i deployed my docker image which azure pipelines and everything is okay, but when i connect through ssh to the instance and call 'env' i don't see any of my environment variables.

From the documentation i should be able to call them inside php as every other env variable (getenv..). Maybe i miss something or my understanding of this app settings are incorrect.

Would be great if somebody has an idea about what is wrong, if you need more informations hit me up.

Zero
  • 554
  • 9
  • 22

2 Answers2

4

Partly, you did not make mistake on the way of accessing setting/configuration which set as environment variable. The getenv() is correct.

when i connect through ssh to the instance and call 'env' i don't see any of my environment variables

I think this issue may caused by your script. When you access these setting keys, do not lost Prefixed. This is the important way to access and get these environment variable. For example, if you want to access app settings, the name of the corresponding environment variable should prepended with APPSETTING_.

At this time, the sample script for PHP script should be:

<?php 
  $appsetting = getenv('APPSETTING_{Key}'); echo $appsetting;
?>

Note: The {key} is the key name you configured in Azure app service.

For the configuration which under Connection Strings, it should be added with other prefixed. As you know, when you create these connection strings, you need to choose Type:

enter image description here

For these setting, the connection strings are available as environment variables, prefixed with the following connection types:

SQL Server: SQLCONNSTR_

MySQL: MYSQLCONNSTR_

SQL Database: SQLAZURECONNSTR_

Custom: CUSTOMCONNSTR_

For more details, check this doc: https://learn.microsoft.com/en-us/azure/app-service/configure-common#configure-connection-strings

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • This still gives me null. Calling env as a command on the system should return me every environment variable which is set. Since there are also no APPSETTING prefixed variables, i think the problem is different. https://learn.microsoft.com/en-us/azure/app-service/containers/configure-language-php#access-environment-variables Also related to this article the prefix is not necessary – Zero Jul 10 '19 at 06:21
  • @Zero Does the variable configured under Application setting? Did you set the PHP version under general setting? Refer to this doc: https://github.com/uglide/azure-content/blob/master/articles/app-service-web/web-sites-configure.md#app-settings – Mengdi Liang Jul 10 '19 at 06:42
  • Yes its configured under application setting. Under general setting i don't see any option to set the php version – Zero Jul 10 '19 at 06:57
2

Setting Environment Variables

  1. In Azure Portal, locate your app service
  2. On the left pane, click Configuration
  3. Under Application settings, click "New application setting"
  4. Fill in the name and value for the environment variable
  5. Click "OK", then at the top, click "Save"

Accessing Environment Variables

With PHP

Replace ENV_VAR_NAME with your own environment variable name.

$var = getenv('ENV_VAR_NAME');

With CLI - Linux

You can use your preferred SSH client, but in this case I'll use the one in Azure Portal.

  1. In Azure Portal, locate your app service
  2. On the left pane, click "SSH"
  3. Click "Go"
  4. Type printenv ENV_VAR_NAME, replace ENV_VAR_NAME with your environment variable.
Jenga
  • 149
  • 4
  • 16