Skip to content

Environment variables

Following the 12-factor app methodology, and especially the Configuration section, environment variables should be used to store environment-specific configuration, such as database credentials, API keys, and other configuration values that change between environments.

config.php / core_config_data

Magento / Adobe Commerce allows to override any system configuration using environment variables, following the patterns below.

The slash character of config paths should be replaced with double underscores:

Config pathEnvironment variableScope
dev/js/merge_filesCONFIG__DEFAULT__DEV__JS__MERGE_FILESdefault
web/unsecure/base_urlCONFIG__STORES__MYSTORECODE__WEB__UNSECURE__BASE_URLmystorecode store
web/secure/base_urlCONFIG__WEBSITES__MYWEBSITECODE__WEB__SECURE__BASE_URLmywebsitecode website

WARNING

These environment values only take precedence over values in config.php and the database (core_config_data).

NOTE

You need to use encrypted values for sensitive configuration (ie. payment gateway credentials) environment variables.

IMPORTANT

Magento / Adobe Commerce caches the configuration values. If you change the value of an environment variable, you need to clear the cache to see the changes.

In a regular Magento / Adobe Commerce deployment, you might need to define the following environment variables for your config, in addition to the ones you've defined in your env.php file:

Variable nameDescription
CONFIG__DEFAULT__WEB__UNSECURE__BASE_URLFully qualified base URL of your Magento, ending with a /
CONFIG__DEFAULT__WEB__COOKIE__COOKIE_DOMAINCookie domain

Elasticsearch / OpenSearch

Because Elasticsearch / OpenSearch settings are stored in the database, you need to define the following environment variables:

Variable nameDescriptionDefault value
CONFIG__DEFAULT__CATALOG__SEARCH__ENGINEEngine to use: elasticsearch7 (legacy) or opensearchelasticsearch7
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__SERVER_HOSTNAMEServer hostname or IP address127.0.0.1 / localhost
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__SERVER_PORTServer port9200
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__INDEX_PREFIXIndexes name prefixmagento2
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__ENABLE_AUTHEnable basic authentication0
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__USERNAMEAuthentication username
CONFIG__DEFAULT__CATALOG__SEARCH__<ENGINE>__PASSWORDAuthentication password

NOTE

Magento / Adobe Commerces uses different config paths for Elasticsearch and OpenSearch. You need to define the correct one for your environment, replacing <ENGINE> with ELASTICSEARCH7 or OPENSEARCH in the environment variables.
You may omit declaring some of these variables if you are using the default values.

env.php

Magento / Adobe Commerce does not offer a mechanism similar to the one above for values of env.php.

Easy way to be able to define database connection info and such using environment variables, is taking advantage of PHP's getenv() function:

php
<?php
    ...
    'db' => [
        'table_prefix' => '',
        'connection' => [
            'default' => [
                'host' => getenv('MAGENTO_DATABASE_HOST'),
                'dbname' => getenv('MAGENTO_DATABASE_NAME'),
                'username' => getenv('MAGENTO_DATABASE_USERNAME'),
                'password' => getenv('MAGENTO_DATABASE_PASSWORD'),
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;',
                'active' => '1'
            ]
        ]
    ],
    ...

You may also define defaults for those environment variables in your env.php file, using PHP's ternary operator ?::

php
<?php
    ...
    'db' => [
        'connection' => [
            'default' => [
                'host' => getenv('MAGENTO_DATABASE_HOST') ?: 'localhost',
                'dbname' => getenv('MAGENTO_DATABASE_NAME') ?: 'magento',
                'username' => getenv('MAGENTO_DATABASE_USERNAME') ?: 'root',
                'password' => getenv('MAGENTO_DATABASE_PASSWORD') ?: 'root',
                ...
            ]
        ]
    ],
    ...

The following configuration sections should be updated to use environment variables:

  • db
  • cache
  • session
  • queue

See next page for examples.

Released under the MIT License.
This project is not affiliated with, endorsed by, or sponsored by Adobe Inc. "Magento" and "Adobe Commerce" are trademarks of Adobe Inc.
All trademarks and registered trademarks are the property of their respective owners.