Using environment variables in Drupal code and Drush commands

Acquia Cloud makes available a set of environment variables that you can use both in your application code and in Drush commands. These variables make it easier to write code that is responsive to the environment the code is running in. For example, you may have code that should run in your Production environment, but not in the Development or Staging environments.

Available environment variables

Acquia Cloud includes the following environment variables:

Variable Format Description Values
AH_SITE_GROUP string Specifies the site name, which is also the Unix user.  
AH_SITE_ENVIRONMENT string Indicates an Acquia Cloud environment. dev, test, prod
AH_PRODUCTION string Indicates whether this is a Production environment. 1 for Production, undefined for non-Production
AH_NON_PRODUCTION string Indicates whether this is a non-Production environment 1 for non-Production, undefined for Production
AH_CURRENT_REGION string The Amazon Web Services region in which the web server processing the current request is running.  
AH_REALM string The Acquia Cloud realm in which the application is running. The realm is a required parameter in calls to the Cloud API. The value is prod for Acquia Cloud Enterprise and devcloud for Acquia Cloud Professional. The realm corresponds to the element before hosting.acquia.com in the environment's server names.  
HTTP_X_REQUEST_ID string Every web request received by Acquia Cloud is assigned a unique request ID, which is set in the HTTP header X-Request-ID and is available in the HTTP_X_REQUEST_ID environment variable; for more information, see Using HTTP request IDs.  
PATH string If the environment is configured to use a specific version of PHP, that version of PHP is first in the path (for example, /usr/local/php5.6/bin).  
TEMP string The tmp directory at /mnt/tmp/[site].[env] for the environment. Use this, rather than /tmp, which is smaller and may fill up rapidly.  

Environment variables in .htaccess

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
// Acquia Cloud 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]

For more information, see Redirecting visitor requests with the .htaccess file.

Faking your environment variables for local testing

There may be situations where you want to specify a particular Acquia Cloud environment variable during testing. In these situations, you can force the environment variable by making a change to your local settings.php file. The following example sets the AH_SITE_ENVIRONMENT variable to the dev environment:

$_ENV['AH_SITE_ENVIRONMENT'] = 'dev';

This is used in situations such as overriding Solr core automatic connection switching.

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
      // Acquia Cloud 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'])
   { $conf['apachesolr_read_only'] = "1"; }

For another example, see Setting $base_url without breaking the Development and Staging environments.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Contact supportStill need assistance? Contact Acquia Support