Out the box, Magento is capable of running multiple Websites from a single installation. Including the ability to assign each website with their own domains.
In this video, I’ll be showing you how to configure Apache Virtual Hosts to connect to your Multi-Store Websites.
1. Introduction
1.1 Prerequisites and Overview
Before we begin, this tutorial assumes that you already have Magento 2 up and running with Apache on Ubuntu. You’ll also be able to copy and paste any of the commands I use, directly from the article linked in the video description below.
And I’d also like to thank Caz and MoHnuT for helping me put this video together during my Twitch Livestream last week.
1.2 Domain Preparation
Before recording this video, I setup 2 new subdomains with my domain provider. Both pointing to my Sandbox Server where Magento is installed.
Note: Even though I have setup subdomains in this example, full domains would work too.
Ref | Note | URL | DNS IP* |
---|---|---|---|
Domain 1 | Original | digitalstartuptutorials.co.uk |
123.123.123.123 |
Domain 2 | New | one.digitalstartuptutorials.co.uk |
123.123.123.123 |
Domain 3 | New | two.digitalstartuptutorials.co.uk |
123.123.123.123 |
(*IP of my web server where Magento 2 is installed)
2. Basic Magento Configuration
Before diving into the Apache configuration, I want to show you the absolute basic setting that need changing in order for this to work. I will not be showing Product, Customer, Store Setup or anything like that.
2.1 Create New Websites/Store/Store View in Magento
So, having logged into my Magento backend, if I navigate to Stores > All Stores, you can see that I’ve setup 2 additional Websites. Each with their own Store and Store View.
Names
Web Site | Store | Store View | URL |
---|---|---|---|
Main Website | Main Website Store | Default Store View | digitalstartuptutorials.co.uk |
One Website | One Store | English | one.digitalstartuptutorials.co.uk |
Two Website | Two Store | English | two.digitalstartuptutorials.co.uk |
Codes
Web Site | Store | Store View | URL |
---|---|---|---|
base | main_website_store | default | digitalstartuptutorials.co.uk |
one | one_store | one_english | one.digitalstartuptutorials.co.uk |
two | two_store | two_english | two.digitalstartuptutorials.co.uk |
When setting up new Websites, Stores and Store Views, I like to give them smart (easily identifyable) names. However, in this example I have left the Main Website details alone to indicate to you that I have not touched them.
Otherwise, I would have relabelled:
- “Main Website Store” to “Main Store”
- “Default Store View” to “English”
And I would have changed the codes:
- "main_website_store to “main_store”
- “default” to “main_english”
Tip: If you haven’t done this yet, you have to create a “Web Site” before a “Store”… And a “Store” before a “Store View”. Otherwise, you won’t be able to link them.
2.2 Update Base URLs for each of the new Websites
Now if we go into Stores > Configuration > Web, you’ll also see that I’ve updated the Base URLs for the 2 new websites.
By clicking the Scope Dropdown and clicking on One Website, you can see that I’ve updated the Default value of:
- Base URL
- Base Link URL
- Secure Base URL
- Secure Base Link URL
to include the “one.” subdomain. And then if we switch the Scope to Two Website, you’ll see that I’ve done the same thing there as well - Adding “two.” into the domain.
Tip: If you haven’t done this bit yourself, and you are following along then be sure to flush the cache. Otherwise, some settings might give you an odd error if you try to change them.
2.3 Key Identifiers
Finally, I also changed the Theme and Logo for each of the 3 stores. Purely to indicate that we are in fact loading different websites during testing. I did this under Content > Design > Configuration and Updated the “parent” records to each Website. To clarify, the “parent” record is the line where all 3 columns have a value.
3. Apache Configuration (HTTP)
Right now, I’m logged into my Web Server as the “superuser”.
3.1 Copy Default Configurations
So, first navigate to your Apache Configuration folder by entering:
cd /etc/apache2/sites-available/
If we list the folder contents using:
ls -la
You’ll see (at the very least) 2 files. One called 000-default.conf
and other being default-ssl.conf
. In my example, I have an extra file called 000-default-le-ssl.conf
. This is because I already have a Let’s Encrypt SSL Certificate installed for the Main Website.
Rather than create new Apache Configuration files from scratch, we can simply copy the default file to create new ones from it.
As I alluded to earlier, I like to name things so that they’re obvious. So, I’ll be naming my new configuration files:
one.digitalstartuptutorials.co.uk.conf
two.digitalstartuptutorials.co.uk.conf
I admit those names appear quite long, but they’re much easer to identify when you have about 10 configuration files sitting there. Thank me later.
So, in order to copy the original and paste it with the new names, type:
sudo cp 000-default.conf one.digitalstartuptutorials.co.uk.conf
and the same again
sudo cp 000-default.conf two.digitalstartuptutorials.co.uk.conf
Now if I list the folder contents with:
ls -la
We’ll see the 2 new files.
3.2 Update and Add New Lines for each conf file
In each of the files we just created, we need to add 4 lines to get Apache Virtual Hosts working with Magento Multi-Domain.
Open the first configuration with the nano text editor by typing:
sudo nano one.digitalstartuptutorials.co.uk.conf
After the line containing “DocumentRoot /var/www/html”, copy and paste the following 4 lines:
ServerName one.digitalstartuptutorials.co.uk
ServerAlias one.digitalstartuptutorials.co.uk
SetEnv MAGE_RUN_CODE "one"
SetEnv MAGE_RUN_TYPE "website"
Out of the 4 lines, you only need to edit the first 3:
ServerName
: Domain URLServerAlias
: Domain URLSetEnv MAGE_RUN_CODE
: Web Site Code (Ref Section 2.1)
Once updated, save your changes by press Ctrl+X to exit, Y to save and Enter to confirm.
Now, let’s to that again for our Two Website. Open the configuration file by typing:
sudo nano two.digitalstartuptutorials.co.uk.conf
Add the 4 lines after “DocumentRoot /var/www/html”:
ServerName two.digitalstartuptutorials.co.uk
ServerAlias two.digitalstartuptutorials.co.uk
SetEnv MAGE_RUN_CODE "two"
SetEnv MAGE_RUN_TYPE "website"
Once you’ve finished making your changes, press Ctrl+X to exit, Y to save and Enter to confirm.
3.3 Enable new Conf files
It’s not just enough to create the configuration files, but we need to enable them too. To do this, run the following:
sudo a2ensite one.digitalstartuptutorials.co.uk.conf
and then:
sudo a2ensite two.digitalstartuptutorials.co.uk.conf
Followed by the restart of apache:
sudo systemctl restart apache2
So long as you’ve not made any syntax errors, you won’t see any error messages.
3.4 Let’s Encrypt setup (Optional)
If you’re not running any SSL Certificates right now, skip this step.
Even if you have a Let’s Encrypt SSL Certificate setup on your original domain - You should run the Let’s Encrypt script again to ensure that the setting you just changed also apply to your Apache SSL configurations. Otherwise, you’ll run into an issue where none of the things we changed will work when the customer connects over HTTPS.
So to regenerate your SSL Certificates and Apache SSL Configuration files, enter:
sudo certbot --apache -d digitalstartuptutorials.co.uk -d one.digitalstartuptutorials.co.uk -d two.digitalstartuptutorials.co.uk
Making sure to include a “-d” flag before list each domain. At the first prompt, enter “E” to Expand and at the second enter “1” to not force redirection.
3.5 Typical SSL setup (Optional)
If you’re not running any SSL Certificates right now, skip this step.
If you have a typical SSL installation like DigiCert, GlobalSign, etc (which I imagine most of you will), then be sure to add those 4 lines to your existing (or new) Apache SSL Configuration files.
4 Deployment and Testing
So, now that the Apache stuff is out the way, we need to switch the the “magento” user:
su magento
And then switch to the Magento directory:
cd /var/www/html
4.1 Deploy Static Content / Clear Cache
And now, let’s start by redeploying Static Content:
bin/magento setup:static-content:deploy en_US en_GB -f
My store contains both US and GB locales. Be sure to change yours if necessary.
Once that’s done, clear the cache:
bin/magento cache:clean
4.2 Testing
And now if you visit each of the domains, you will see the appropriate store load for each one.