Configuring Multiple Shopware Environments

Shopware is able to handle multiple environments using the SHOPWARE_ENV environment variable. This can be used to load different config files and to separate cache files.

How the environment is handled

This environment variable is picked by shopware.php and bin/console and then propagated to the kernel:

$environment = getenv('SHOPWARE_ENV') ?: 'production';
$kernel = new Kernel($environment);

As you can see, if no environment is given, production is used as the default.

Generated files

The environment is taken into account for the cache directories and log files. Running Shopware with each of the environments dev, production and testing will result in the following directory tree:

var
├── cache
│   ├── dev_201601120732/[...]
│   ├── production_201601120732/[...]
│   └── testing_201601120732/[...]
└── log
    ├── core_dev-2016-01-25.log
    ├── core_dev-2016-01-26.log
    ├── core_dev-2016-01-27.log
    ├── core_production-2016-01-25.log
    ├── core_production-2016-01-26.log
    ├── core_production-2016-01-27.log
    ├── core_testing-2016-01-25.log
    ├── core_testing-2016-01-26.log
    └── core_testing-2016-01-27.log

Config loading

The config loading order is defined in engine/Shopware/Configs/Default.php. The config loader looks for a file named config_[ENVIRONMENT].php first. If that file does not exists it falls back to config.php.

On my development machine all my installations contain a file named config_dev.php. This configuration is usually optimized for development, increases error verbosity and disables some caches etc.

<?php
$defaultConfig = require 'config.php'; // config generated by ant build-unit

return [
    'db' => $defaultConfig['db'],

    'errorHandler' => [
        'throwOnRecoverableError' => true,
    ],

    'front' => [
        'showException' => true,
    ],

    'model' => [
        'cacheProvider' => 'array'
    ],
]

Setting the environment

Apache

You can set the environment for your local installation using the .htaccess:

SetEnv SHOPWARE_ENV dev

This also can be done conditionally for a specific Host or Subshop:

SetEnvIf Host "dev.shopware.in" SHOPWARE_ENV=dev

On my local development machine I configured it in the Apache vhost configuration for all installations:

<VirtualHost *:80>
    DocumentRoot "/home/bcremer/www/"
    SetEnv SHOPWARE_ENV dev
</VirtualHost>

Nginx

When using the shopware-with-nginx configuration:

set $shopware_env 'production';

Ant

$ SHOPWARE_ENV=dev ant build-unit

Shopware Console

This also works for the Shopware Console Commands:

$ SHOPWARE_ENV=dev ./bin/console sw:generate:attributes

Alternatively you can also use the --env option that exists for every console command:

$ ./bin/console sw:generate:attributes --env=dev

Export environment globally

You can also go so far and set the SHOPWARE_ENV environment variable globally for you shell:

# File: $HOME/.profile
export SHOPWARE_ENV=dev
Back to overview
Top