Skip to content
Home » News » How to Set Up Multi-Store Functionality in Magento 2

How to Set Up Multi-Store Functionality in Magento 2

Setting up a multi-store configuration in Magento 2 allows you to manage multiple storefronts from a single administrative panel. This is particularly useful if you want to operate stores with different domains, themes, product catalogs, or customer groups. Here’s a step-by-step guide to setting up multiple stores in Magento 2:

Step 1: Enable Multi-Store Mode in Magento 2

Before setting up multiple stores, ensure that Magento is configured to allow multi-store functionality.

  1. Log in to the Magento Admin Panel.
  2. Navigate to Stores > Settings > Configuration.
  3. In the left menu, under General, click General.
  4. Expand the Store Information section.
  5. Find the Single-Store Mode option.
  6. Set it to “No” (to enable multiple stores).
Enable Single-Store Mode option in  Magento 2
  1. Click Save Config.
  2. Flush the cache by going to System > Cache Management and clicking Flush Magento Cache.

Step 2: Create a New Website

Magento uses a hierarchy: Websites → Stores → Store Views. Each website can have multiple stores, and each store can have multiple store views.

  1. Go to Stores > Settings > All Stores.
All Stores section in Magento 2 admin panel
  1. Click Create Website.
Stores section in Magento 2 admin panel
  1. Fill in the following details:
    • Name: Enter a unique name for the new website.
    • Code: Enter a unique identifier (used in configurations).
    • Sort Order: (Optional) Define the order of appearance.
Create Website option in Magento 2 Admin panel
  1. Click Save Website.

Step 3: Create a New Store

Each website must have at least one store, where product categories are assigned.

  1. Go to Stores > Settings > All Stores.
  2. Click Create Store.
Create Store option in Magento 2 Admin panel
  1. Configure the store settings:
    • Web Site: Select the website created in Step 2.
    • Name: Enter a unique name for this store.
    • Root Category: Select or create a new root category for this store (e.g., if you want different product categories per store).
    • Store infromation in Create Store option in Magento 2
  2. Click Save Store.

Step 4: Create a New Store View

Store views allow customers to switch between different language versions of a store.

  1. Go to Stores > Settings > All Stores.
  2. Click Create Store View.
Create Store View option in Magento 2
  1. Configure the store view settings:
    • Store: Select the store created in Step 3.
    • Name: Enter a unique name for this store view (e.g., “English Store”).
    • Code: Provide a unique identifier.
    • Status: Set to Enabled.
    • Sort Order: (Optional) Define the display order.
  2. Click Save Store View.
Store View Informatio in Stores in magento 2 Admin panel

Step 5: Configure Base URLs for Each Store

Each store should have its own domain or subdomain.

  1. Go to Stores > Settings > Configuration.
  2. In the upper-left corner, change the scope to the newly created website.
  3. Expand General > Web.
  4. Open the Base URLs section.
    • Uncheck Use Default and enter the store’s URL (e.g., http://store2.example.com/).
  5. Expand Base URLs (Secure).
    • Uncheck Use Default and enter the store’s secure URL (https://store2.example.com/).
Web -> based URLs and Base URLs (secure) in Magento 2 Admin panel
  1. Click Save Config.

Step 6: Add index.php and .htaccess Files

To make sure each store runs correctly, you need to copy some files and adjust their settings.

  1. Go to your Magento 2 root directory on your server.
  2. Create a new folder inside it (e.g., file1).
  3. Copy the index.php and .htaccess files from the Magento root directory into this new folder.
  1.  Open the copied index.php file in a text editor and replace this part of the code:
<?php
require realpath(__DIR__) . '/../app/bootstrap.php'; // Path to the Magento bootstrap file
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'your_store_code'; // Replace with your store's code from Step 4
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; // Use 'store' if setting up a store view, otherwise use 'website'
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

5. Save the changes and close the file.

    This step ensures that Magento knows which store to load when a customer visits the corresponding URL.

    Step 7: Add Store Codes to URLs (Optional)

    Magento can include store codes in URLs to distinguish stores.

    1. Go to Stores > Configuration > General > Web.
    2. Expand the URL Options section.
    3. Set Add Store Code to URL to Yes.
    Url Options section in Magento 2 admin panel
    1. Click Save Config.

    Step 8: Configure the Web Server (Apache/Nginx)

    If you are using Apache, update the Virtual Host Configuration:

    <VirtualHost *:80>
        ServerName store2.example.com
        DocumentRoot /var/www/html/magento2/
        <Directory /var/www/html/magento2/>
            AllowOverride All
        </Directory>
    </VirtualHost>
    

      For Nginx, update the configuration:

    server {
        listen 80;
        server_name store2.example.com;
        set $MAGE_RUN_TYPE website;
        set $MAGE_RUN_CODE store2;
        root /var/www/html/magento2;
        index index.php;
    }
    

      Restart the web server after making changes:

    sudo systemctl restart apache2   # For Apache
    sudo systemctl restart nginx     # For Nginx

    Step 9: Set Up DNS for New Domains

    Ensure that each store’s domain (e.g., store2.example.com) points to your server’s IP address.

    1. Log in to your domain registrar.
    2. Add an A Record pointing store2.example.com to your server’s IP.
    3. Save changes and wait for DNS propagation (may take a few hours).

    Step 10: Flush Cache and Reindex

    1. Go to System > Cache Management.
    2. Click Flush Magento Cache.
    3. Run the following CLI commands:
    php bin/magento cache:flush
    php bin/magento indexer:reindex
    php bin/magento setup:upgrade
    php bin/magento deploy:mode:set production

    Step 11: Test Your Multi-Store Setup

    • Open a browser and visit each store URL (store2.example.com).
    • Ensure that each store displays the correct catalog and settings.
    • Check if switching between store views works correctly.

    Common Issues and Fixes in Multi-Store Setup

    1. Forbidden Error (403)

    If you get a 403 Forbidden error, it may be due to incorrect file permissions or a missing .htaccess file.

    Fixes:

    Check file permissions: Ensure files have 644 permissions and directories have 755 permissions. Run the following commands:

    find /path/to/magento2/ -type f -exec chmod 644 {} \;
    find /path/to/magento2/ -type d -exec chmod 755 {} \;
    • Ensure .htaccess exists: Make sure it’s in the Magento root directory and configured correctly.
    • Check store configuration: Verify the store code and base URLs in the Magento Admin panel.
    • Enable URL rewrites: Go to Stores > Configuration > General > Web > Search Engine Optimization and set Use Web Server Rewrites to Yes.
    Search Engine Optimization option in Magento 2 Admin panel

    2. Subdomain Not Showing Content

    If subdomains don’t display content, ensure the new folders are correctly created and domains point to the right directories. Try adding a test.php file and accessing it to verify the setup.

    3. Extension Conflicts

    Some Magento extensions may not work properly in a multi-store setup. You can disable them for specific stores if needed. Ensure all extensions are compatible with your Magento version.

    4. Customer Login Issues Across Stores

    By default, Magento does not share login sessions across different stores. To allow users to log in with the same credentials across all stores:

    1. Go to Stores > Settings > Configuration > Customers > Customer Configuration > Account Sharing Options.
    Customer Conifguration option in Magento 2 Admin panel
    1. Set Share Customer Accounts to Global.
    2. Save the configuration.

    5. Issues with Multi-Store Session Handling

    Sometimes, customers may face login issues when switching between stores due to session handling problems. This can be resolved by setting a proper cookie lifetime.

    Fix:

    1. Navigate to Stores > Configuration > General > Web > Default Cookie Settings.
    2. Adjust the Cookie Lifetime value to a longer duration.
    Default Cookie Settings in Magento 2
    1. Save the configuration

    Conclusion

    By following these steps, you can successfully set up multiple stores in Magento 2, each with its own domain and configuration, all managed from a single admin panel. You can now customize themes, apply store-specific settings, and track sales for each store separately when having multistore functionality correctly configured.

    If you need custom checkout options, different shipping/payment methods per store, or SEO optimization, consider using Magento extensions for additional functionality.

    Author

    Leave a Reply

    Your email address will not be published. Required fields are marked *