This document contains information about using Cloud Platform environment variables in your application.
Cloud Platform makes environment variables available for your use in both your application code and Drush commands. With these variables, you can write code that responds to the environment in which it runs. For example, you may want to run certain commands only in your production environment, but not in the development or staging environment.
Additional environment variables are available for Pipelines.
Important
Applications on Cloud Platform have a special include statement in the settings.php file like the following example:
if (file_exists('/var/www/site-php')) {
require '/var/www/site-php/sitename/sitename-settings.inc';
}
If you use environment variables in settings.php, you must place references to the environment variables after the preceding include statement, to ensure they take effect in only the desired contexts.
You can also use environment variables in your .htaccess file. In the following example, we redirect HTTP requests to HTTPS only on the production environment, and not in the development or staging environments:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{ENV:AH_SITE_ENVIRONMENT} prod
// Site Factory may require a different value for
// %{ENV:AH_SITE_ENVIRONMENT} depending on site configuration
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Faking your environment variables for local testing
During testing, you may want to force a Cloud Platform environment variable to contain a specific value by making a change to your local settings.php file. The following example sets the AH_SITE_ENVIRONMENT variable to the dev environment:
This document contains information about using Cloud Platform environment variables in your application.
Cloud Platform makes environment variables available for your use in both your application code and Drush commands. With these variables, you can write code that responds to the environment in which it runs. For example, you may want to run certain commands only in your production environment, but not in the development or staging environment.
Additional environment variables are available for Pipelines.
Important
Applications on Cloud Platform have a special include statement in the settings.php file like the following example:
if (file_exists('/var/www/site-php')) {
require '/var/www/site-php/sitename/sitename-settings.inc';
}
If you use environment variables in settings.php, you must place references to the environment variables after the preceding include statement, to ensure they take effect in only the desired contexts.
You can also use environment variables in your .htaccess file. In the following example, we redirect HTTP requests to HTTPS only on the production environment, and not in the development or staging environments:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{ENV:AH_SITE_ENVIRONMENT} prod
// Site Factory may require a different value for
// %{ENV:AH_SITE_ENVIRONMENT} depending on site configuration
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Faking your environment variables for local testing
During testing, you may want to force a Cloud Platform environment variable to contain a specific value by making a change to your local settings.php file. The following example sets the AH_SITE_ENVIRONMENT variable to the dev environment:
The site name or Unix user concatenated with the environment name.
HTTP_X_REQUEST_ID
string
The unique request ID assigned to every web request received by Cloud Platform. This is set in the HTTP header X-Request-ID. For more information, see Using HTTP request IDs.
The location of all the executable for the environment. If you configure an environment to use a specific version of PHP, that version of PHP is first in the path.
TEMP
string
The temporary directory for the environment. This directory is located at/mnt/tmp/[site].[env]. Use this variable instead of /tmp because /tmp is smaller and may fill up rapidly.
AH_GIT_REF
string
The deployed branch or tag name, used for readable identification in logs and error trackers.
AH_GIT_SHA
string
The exact 40-character commit hash, used as a precise, permanent identifier for strict version tracking.
Examples
For example, the following statement would let you switch code based on the environment:
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
switch ($_ENV['AH_SITE_ENVIRONMENT']) {
case 'dev':
// do something on dev
break;
case 'test':
// do something on staging
break;
case 'prod':
// do something on prod
// Site Factory may require a different value depending
// on site configuration
break;
case 'ra':
// do something on ra - necessary if a
// Remote Administration environment is present
break;
}
}
else {
// do something for a non-Acquia-hosted application
// (like a local dev install).
; }
To make Acquia Search read-only on your Development and Staging environments, so the search index doesn’t have duplicate copies of content:
// place this after the Acquia require line
if (!isset($_ENV['AH_SITE_ENVIRONMENT']) ||
'prod' != $_ENV['AH_SITE_ENVIRONMENT']) {
$settings['acquia_search']['read_only'] = TRUE;
}
The site name or Unix user concatenated with the environment name.
HTTP_X_REQUEST_ID
string
The unique request ID assigned to every web request received by Cloud Platform. This is set in the HTTP header X-Request-ID. For more information, see Using HTTP request IDs.
The location of all the executable for the environment. If you configure an environment to use a specific version of PHP, that version of PHP is first in the path.
TEMP
string
The temporary directory for the environment. This directory is located at/mnt/tmp/[site].[env]. Use this variable instead of /tmp because /tmp is smaller and may fill up rapidly.
AH_GIT_REF
string
The deployed branch or tag name, used for readable identification in logs and error trackers.
AH_GIT_SHA
string
The exact 40-character commit hash, used as a precise, permanent identifier for strict version tracking.
Examples
For example, the following statement would let you switch code based on the environment:
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
switch ($_ENV['AH_SITE_ENVIRONMENT']) {
case 'dev':
// do something on dev
break;
case 'test':
// do something on staging
break;
case 'prod':
// do something on prod
// Site Factory may require a different value depending
// on site configuration
break;
case 'ra':
// do something on ra - necessary if a
// Remote Administration environment is present
break;
}
}
else {
// do something for a non-Acquia-hosted application
// (like a local dev install).
; }
To make Acquia Search read-only on your Development and Staging environments, so the search index doesn’t have duplicate copies of content:
// place this after the Acquia require line
if (!isset($_ENV['AH_SITE_ENVIRONMENT']) ||
'prod' != $_ENV['AH_SITE_ENVIRONMENT']) {
$settings['acquia_search']['read_only'] = TRUE;
}