How to Configure a Magento Multistore in Docker

How to Configure a Magento Multistore in Docker
How to setup multiple websites, stores and store views in Magento locally with Docker using Warden and Traefik.

Published by Rick Daalhuizen

In warden it is possible to set up multiple domains for your project. Warden uses Traefik and is installed as a Global Service.

Traefik ensures that requests are intercepted and sent to the correct back-end service e.g. https: //app.example1.test https: //app.example2.test. For this it uses an HTTP reverse proxy and a load balancer.

It also has a dashboard to monitor different metrics. This can be found if you go to https: https://traefik.warden.test/.

A reverse proxy is a server that sits in front of web servers and forwards client (e.g. web browser) requests to those web servers.

Traefik Dashboard

Before continuing, make sure you have set up a multistore in Magento. You can read how to do this in my previous blog post “How to Setup a Multistore in Magento 2”.

The following must be configured to set up a multi-domain locally for your Magento Shop.

  1. Sign certificates for your new domains
  2. Configure Nginx and Varnish to handle traffic coming from your new domains
  3. Configure Magento 2 to load different stores or websites by setting the run params.

Step 1: Sign certificates for your new domains

warden sign-certificate <your_new_domain>.test

This does the following.

  1. Update your ssl certificates
  2. Create a new ssl certificate.
  3. Update Traefik

This is the output

❯ warden sign-certificate sportdrinks.test
==> Generating private key sportdrinks.test.key.pem
Generating RSA private key, 2048 bit long modulus
..+++
......................+++
e is 65537 (0x10001)
==> Generating signing req sportdrinks.test.crt.pem
==> Generating certificate sportdrinks.test.crt.pem
Signature ok
subject=/C=US/O=Warden.dev/CN=sportdrinks.test
Getting CA Private Key
==> Updating traefik
traefik is up-to-date
Connecting traefik to example_default network
Connecting tunnel to example_default network
Connecting mailhog to example_default network
Restarting traefik ... done

Step 2: Configure Nginx and Varnish

From the root of your project you create a .warden/warden-env.yml file. This file exceeds the environment templates in warden. To see the complete configuration use: warden env config.

Each environment’s configuration uses a base configuration YAML file and optionally a darwin and linux file to add everything to the base configuration.

Add the following to your .warden/warden-env.yml

version: "3.5"
services:
  varnish:
    labels:
      - traefik.http.routers.${WARDEN_ENV_NAME}-varnish.rule=
          HostRegexp(`{subdomain:.+}.${TRAEFIK_DOMAIN}`)
          || HostRegexp(`{subdomain:.+}.<your_new_domain>.test`)
  nginx:
    labels:
      - traefik.http.routers.${WARDEN_ENV_NAME}-nginx.rule=
          HostRegexp(`{subdomain:.+}.${TRAEFIK_DOMAIN}`)
          || HostRegexp(`{subdomain:.+}.<your_new_domain>.test`)

Then run warden env up -d to update your containers.

Step 3: Configure Magento 2 run params

Create an app/etc/Stores.php and load it via composer.json according to the PSR-4 standard.

Create a file app/etc/Stores.php with the following content:

<?php

use \Magento\Store\Model\StoreManager;
$serverName = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;

switch ($serverName) {
    case app.<your_new_domain>.test':
        $runCode = <code>;
        $runType = <type>;
        break;
}

if ((!isset($_SERVER[StoreManager::PARAM_RUN_TYPE])
        || !$_SERVER[StoreManager::PARAM_RUN_TYPE])
    && (!isset($_SERVER[StoreManager::PARAM_RUN_CODE])
        || !$_SERVER[StoreManager::PARAM_RUN_CODE])
) {
    $_SERVER[StoreManager::PARAM_RUN_CODE] = $runCode;
    $_SERVER[StoreManager::PARAM_RUN_TYPE] = $runType;
}

The run-code and run-time are the store code and type of your Magento Store or Website. You can find this under Stores -> Settings -> All Stores or in one of the store *_ tables.

{
    "autoload": {
        "files": [
            "app/etc/stores.php"
        ]
    }
}

Run composer dump-autoload to regenerate your autoload configuration.

Then restart warden containers and navigate to your new domain app. <your_new_domain>.test.