Jigar KarangiyaJigar Karangiya

Multi Website Domain Setup in Magento 2 using DDEV

JK
Jigar Karangiya
· 2 min read
Multi Website Domain Setup in Magento 2 using DDEV

Within just one single instance of Magento, we can have multiple websites, stores, and storeviews operating on the same Magento backend. This allows limitless flexibility for your eCommerce operations.

In today’s tutorial, We are going to see how we can do multiple domain setups for our Magento project in the DDEV Local environment.

DDEV is an open-source tool for launching local web development environments in minutes. It supports PHP, Node.js, and Python, etc languages.

While developing in the local environment, creating virtual hosts, adding hosts and many more configurations was time-consuming and complex.

But using DDEV you can add and link multiple domains with your Magento 2 projects in a few easy steps.

So, let’s start by following the below the quick steps.

Step 1: Add Additional Domain

To start with a multi-website setup you will be required to add a new domain to your ddev project for your second website.

Suppose, I have firstdomain.ddev.site as my main domain for the main website and I want to link a new domain to my second seconddomain.ddev.site domain.

I will need to define my second domain to the additional_hostnames inside the .ddev/config.yaml file.

yaml
name: firstdomain
type: magento2
docroot: pub
php_version: "7.4"
webserver_type: nginx-fpm
xdebug_enabled: false
additional_hostnames: [seconddomain]
additional_fqdns: []
database:
    type: mariadb
    version: "10.4"
use_dns_when_possible: true
composer_version: "2"
web_environment: []
webimage_extra_packages: [php7.4-imap]

So, to reflect changes we need to restart our project using the below command.

Run
ddev restart

After restart, if you perform ddev describe, you will see the new domain added to the project followed by seconddomain.ddev.site

Now, as our new domain is configured, we need to link it with our second website store.

To do this, you have to add the below code at the start of the .ddev/nginx_full/nginx-site.conf file

text
map $http_host $MAGE_RUN_CODE {
    secondwebsite.ddev.site store_view_code;
}

Note : you have to remove #ddev-generated line from this file in order to allow your customization.

Step 3: Restart the project to reflect nginx configurations

Run
ddev restart

By following the above steps, you will be able to access your second store using seconddomain.ddev.site

If you like this tutorial, Please share it with your team members. 🙂

Don’t forget to check the below ddev related tutorials :

DDEV Docker: How to install additional PHP extensions in the DDEV project?

DDEV Docker: How to change the project PHP version?

DDEV Docker: Configure Cron in Magento 2

Written by Jigar Karangiya, Adobe Commerce & Magento 2 developer.

More about me

Related posts