18

Can you please provide the source/procedures to create a multi websites in magento 2. I have a google it regarding this,but none of the source/procedure didn't provide the path to create multi website/store.

2

5 Answers 5

33

Created multi-website in Magento, Steps to create a multistore in admin panel is same as like in magento1.x. Don't forget to change the base url and secure url for the new website/store. Once made changes in admin panel follow the below steps,

  1. Create a new folder in magento root and copy the index.php and .htaccess files from magento root to new folder.

  2. Edit the index.php which is in new folder

Replace:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

/** @var \Magento\Framework\App\Http $app */

$app = $bootstrap->createApplication('Magento\Framework\App\Http');

$bootstrap->run($app);

With:

 $params = $_SERVER;
    
 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'newstore'; //Webite code as same in admin panel
    
 $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
    
 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
    
 /** @var \Magento\Framework\App\Http $app */

 $app = $bootstrap->createApplication('Magento\Framework\App\Http');

 $bootstrap->run($app);

And also update bootstrap.php include path as below,

Replace:

require __DIR__ . '/app/bootstrap.php';

With:

require __DIR__ . '/../app/bootstrap.php';
  1. Create simlinks inside the new folder

    ln -s /home/example/example.com/html/app/ app

    ln -s /home/example/example.com/html/lib/ lib

    ln -s /home/example/example.com/html/pub/ pub

    ln -s /home/example/example.com/html/var/ var

Refer this

Please clear the var/generation, var/cache, and pub/static files and do the static content deployment.

5
  • I think we have to copy .htaccess also (together with index.php) from the root to the new folder.
    – Andrea
    Commented Aug 8, 2016 at 20:53
  • 1
    can you setup multistores without hardcoding values in index.php or htaccess using add store codes to urls? seems to not work on magento2 Commented Feb 3, 2017 at 11:25
  • Have you tried doing this with the suggested configuration for production environments and pointing the webroot to [Magento root]/pub? Commented Jan 24, 2018 at 13:17
  • Do you guys have had any issue applying the same custom theme to both website? Mine the second website is loading assets from ../en_US/... instead of ../en_GB/.. any idea what could be ? Commented Feb 26, 2018 at 20:10
  • Try deploy on specific theme with language code Commented Feb 27, 2018 at 4:28
4

thanks to this resource

in Magento backend, go to Stores > All Stores Create here your architecture with different websites/store/storeview Note carefully the website codes, for instance,

  • USA store has code : us, and will be accessed by www.store.com
  • French store has code : fr, and will be accessed by www.store.fr
  • Spanish store has code : es, and will be accessed by www.store.es

in your Nginx config file (most likely in /etc/nginx/sites-enabled folder) add at the top of the config file :

map $HTTP_HOST $mage_run_code {
www.store.com us;
www.store.fr fr;
www.store.es es;
}

then, in the server block, add the declaration to listen to the 3 domains :

server {
 listen 80;
 server_name www.store.com www.store.fr www.store.es;

// whatever other config you get...
}

last, in the php config (the block starting with location ~ \. php $ {), add

fastcgi_param MAGE_RUN_TYPE website;
fastcgi_param MAGE_RUN_CODE $mage_run_code;

before the line (you will normally see other lines starting with fastcgi_param )

 include        fastcgi_params;

save your config file, restart your Nginx server et voila.

0
2

RTFM
https://devdocs.magento.com/guides/v2.3/config-guide/multi-site/ms_over.html

Contents

  • Introduction to multiple Magento stores and websites
  • Configure Magento
  • Set values for MAGE_RUN_TYPE and MAGE_RUN_CODE
0
1

Simple way tested on magento 2.2.5

Create a domain alias on your server example domain2.com

Edit index.php file in magento root

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

$params = $_SERVER;
$customstore = array(
    'www.main.com'=>'main_website', // Website, Store or Storeview Code
    'www.domian2.com'=>'domain2_website'     // Website, Store or Storeview Code
    );
if(isset($customstore[$_SERVER['HTTP_HOST']]))
    $websitecode = $customstore[$_SERVER['HTTP_HOST']];
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = isset($websitecode) ? $websitecode : '';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';//use website or store or view 
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
2
  • I am trying to create multi-website locally (Centos - Nginx) But getting error in Nginx configuration. Any help thanks magento.stackexchange.com/q/315027/57334 @Altravista thanks
    – zus
    Commented Jun 25, 2020 at 13:34
  • Thank you it is working for me in 2.4.2 Commented Jul 21, 2021 at 9:54
0

There are 6 steps to implement multiple website or store on the Magento2 site.

Step 1: Create a Root Category

Step 2: Create a new website

Step 3: Create a new store

Step 4: Create store views

Step 5: Configure the Store URL

Step 6: Map the vhost to work with Magento 2

2
  • I have read this : thecoachsmb.com/… Commented May 16, 2022 at 10:12
  • I'm using apache server in ubuntu system, so how can i set host and domain for second website? can you please guide me. Commented May 16, 2022 at 10:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.