Overview
2017-03-13: For now this is draft version.
This page provides some notes on configuration of primarily the host server to enable the Multi-site feature of Magento 2.
Context
Here are the top-level topics that relate to Magento's multi-site features
- The business motivations for multiple sites and how to implement them
- the features that you might wish to differ between your multiple sites, such as range of products, pricing, language and so on.
- how those features map onto actual Magento features in its Global-Website-Store-Storeview hierarchy
- where to configure those features in Magento
- The technical aspects which launch Magento and tell it to present a particular one of the multi sites
- Different domain names for each Magento site
- How a browser request to a particular domain results in Magento presenting a particular site
- The several different configuration strategies that could be used
The first of the above topics is an important one, and calls for some coverage of the business considerations, and documentation of the features Magento offers at each level of the hierarchy. That's not the topic of this page, though I did take a stab at mapping out the hierarchy and its features here.
This page tackles only the second of these topics: the technical aspects by which Magento is invoked and presents a particular site. This is a topic about which there is a shortage of official authoritative info, and a surplus of random blog and forum postings. There's considerable confusion because there are several different (what I call) "configuration strategies" to achieve the desired effect. Each strategy requires attending to several pretty simple configuration details, but they must be coordinated within a strategy, and not confused with details from a different strategy.
Preliminaries
To get Magento installed and working for multi-sites consists essentially of getting Magento installed for single-site use, and then making a few configuration embellishments. This page is dedicated mainly to those embellishments, so I will just list here the base Magento installation topics that we'll assume you know, or are able to discover elsewhere from other docs and study:
- Set up a local computer or rent hosting service. Presumably linux.
- Including getting the one or more ethernet ports working.
- Familiarity with using linux command line (ls, cd, cp, edit using vi or nano, permissions)
- Installation of web server (Apache or Nginx)
- And basic configuration
- To serve static pages, if only for testing
- To invoke Magento
- Some method of installing Magento 2 suitable to your platform
- And its dependencies, like MySQL, PHP, possibly an MTA to send email. See Magento docs.
- Register a domain name
- Subscribe to a DNS service and configure it (probably using online web form) to point to your platform
- Terminal access to the platform (ssh, putty etc if remote)
- Understanding of TLS/SSL certificates so your site can serve pages over secure https protocol (important for ecommerce!)
- How to get a certificate
- What to put in the web server's config file(s) so that the web server listens for https requests and uses the certificate.
All of these topics are documented thoroughly elsewhere, most being basic web server admin tasks, so I won't detail them here.
That said, most of these steps will need some additional attention for multi-site, which is the topic of this page.
Basic principles
The task at hand:
Web admin revisions for multiple domains
- Register multiple domain names
- If the multi domain names are just subdomains of your existing domain (shoes.mysite.com, hats.mysite.com), then no registration needed, as subdomains are just configured in DNS)
- Set up DNS for them, possibly pointing them all to a single IP address of your server
- (Which is possibly even on shared hosting, where many sites, not just yours, share the same IP address.)
- Create web server config files for each of the multiple sites.
- This may be where to perform the environment variable step, described later
- It is the place to add info for each SSL cert, if using https. (But it's just a repetition of what you do for a single site and SSL.)
- If you're using shared hosting, you won't have direct access to the web server config files, so there will be some sort of web interface to accomplish similar goals.
Magento-specific revisions for multiple sites
- Configure Magento itself with settings and content data for multiple sites. This is covered in Magento's docs, for example starting here:
- For the current discussion, the main point-of-interest is that each separate website or store that you configure has associated with it a website code or store code. These are the identifiers by which Magento can be instructed to launch a particular Magento site, as will be seen in the following sections.
- Configure the web server so that in response to incoming requests, when it launches Magento, it provides one of those particular website or store codes to Magento to tell it which of its multi-sites to display. That's the topic of the rest of this page. The main idea is: the website or store code is communicated via:
- Either: A couple of shell environment variables (MAGE_RUN_CODE, MAGE_RUN_TYPE)
- There are a couple of places where these could be set; you only need to set them in one place
- Or: A couple of parameters in the index.php file that starts Magento: (PARAM_RUN_CODE, PARAM_RUN_TYPE)
- If you use this strategy, you don't need the environment variables.
Now on to the details.
Detect the request's domain, invoke Magento with website/store code
There are multiple ways to detect the requesting domain and set website/store code information in order to tell Magento which site to present. The following diagram shows several alternative ways to configure this, indicating the config (and php) files involved.
Though the diagram may look daunting, you only need to make the choices and configuration for one of the paths from left to right, and in that path there's one specific place to make the relevant config changes for multi-site Magento. However that config does need to coordinate with what happens before and after, (and should not conflict with config elsewhere in the chain of events).
I diagrammed all (or most) of the possible config strategies here, so that (a) you can see that there are different choices, and (b) you can appreciate which choice someone is talking about in a random blog or forum post and identify whether it represents a complete config strategy, or rather some incomplete or haphazard combination of pieces from different strategies.
Several different config strategies
You only need to configure one of these left-to-right paths, which means setting some configuration in one place only.
Click to enlarge

Strategy: Use the web server's config files
Click to enlarge

Strategy: Use the index.php file that launches Magento
Click to enlarge

Details: Multi-site using the web server's config files
Apache
Todo: example config file and description
Nginx
Todo: example config file and description
Details: Multi-site using single .htaccess file
Todo: example index.php file and description
Issues:
- Applicable only to Apache, as Nginx does not use .htaccess files
- Can be blocked by suexec security measure on shared hosting, possibly elsewhere. See this stackexchange post.
Details: Multi-site using the index.php file
Invocation of the correct Magento site can be implemented in the index.php file. This is described here.
In my view, this is a pretty clean solution.
- It would be implemented the same regardless of which web server is used.
- Avoids idiosyncratic handling of .htaccess files on some hosting providers (ie: with some Apache options)
- Implements the necessary config within the realm of php files, which you will usually have access to, even if your hosting provider doesn't provide access to web server config files.
Troubleshooting the environment variable strategy
If you use a config strategy that sets the environment variables you can test that it's taking effect by doing the following.
Create a php file (say "envt_var_test.php") in the web server's docroot directory:
<html> <head> <title>Environment variable test</title> </head> <body> <?php echo '<p>Environment variables</p>'; ?> <pre> <?php print_r($_SERVER); ?> </pre> </body> </html>
|
Now browse to this file at each of the domains you've configured:
- http://clothing.mybiz.com/envt_var_test.php
- http://furniture.mybiz.com/envt_var_test.php
In each case, among the environment variables you should see the values for MAGE_RUN_CODE and MAGE_RUN_TEST that you configured for each domain.
Additional issues
TODO
References
- https://en.wikipedia.org/wiki/Multihoming
- https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
- https://httpd.apache.org/docs/2.4/vhosts/examples.html