How to create multiple web sites using shared code base in Drupal 8 and Drupal 9
In this article, we are going to discuss different ways of creating multiple websites with a single Drupal codebase. This article is applicable to Drupal 8 and Drupal 9. By default, Drupal allows creating multiple websites with separate databases for each website. Themes, modules, and libraries will be shared among the sites.
For easier maintenance and code reusability, the multi-site approach mostly recommended . one of the draw back of multi-site approach is, during maintenance, all sites need to be in maintenance mode.
we can create multi-sites with different approaches as provided below. Each one have its own advantages and disadvantages.
- Multi-site with separate dedicated database for each website.
- Multi-site within the same database with the table prefix
- Multi-site with one common shared database using Domain Access module
Before creating subsites from a main site, we need to configure the virtual host and add host entry in order to access the main site using a domain name. Here I am using XAMPP server in windows.
Create virtual host for main site in below file.
C:\xampp\apache\conf\extra\httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/dnajaxmodal" ServerName digitalnadeemlocal.com ErrorLog "logs/digitalnadeem.com-error.log" CustomLog "logs/digitalnadeem.com.log" common </VirtualHost>
Open below host file.
Add host entry
SO our main website can be accessed using url http:// digitalnadeemlocal.com
1.Multi-site with separate dedicated database for each website
Here we are going to create another website that will have a separate database but the code base will the same as digitalnadeemlocal.com say this as the parent site or main website.
Create a new database. Here we are creating a database called android_digitalnadeem
Add virtual host and mapping in host file
Add below virtual host entries in configuration file httpd-vhost file.
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/dnajaxmodal" ServerName android.digitalnadeemlocal.com ErrorLog "logs/digitalnadeem.com-error.log" CustomLog "logs/digitalnadeem.com.log" common </VirtualHost>
DocumentRoot will be mapped to main website directory which is same as path of digitalnadeemlocal.com
Here we are giving subdomain as android.digitalnadeemlocal.com
Add this in host file. Provide below mapping in your host file.
Create a folder for site1 in your multisite: /d8multisite/sites/android.digitalnadeemlocal.com
Make a copy of /d8multisite/sites/example.sites.php called /d8multisite/sites/sites.php
Edit sites.php and add below line at the end of the file
$sites[‘android.digitalnadeemlocal.com’] = ‘android.digitalnadeemlocal.com’;
Copy /d8multisite/sites/default/default.settings.php to the new site’s directory as settings.php:
cp sites/default/default.settings.php / sites/ android.digitalnadeemlocal.com /settings.php
access newly created subdomain and complete installation. During installation step provide new database for this website.
Clear all cache before accessing main and sub sites.
You can see all tables available in newly created website database.
If we create users in particular site, users will be available only to that website.
All modules, libraries, theme and files are shared between websites.
2.Multi-site within the same database with the table prefix
Follow above steps from step 2 to step 5
Access your new web site and proceed with the installation.
Provide database name as an existing main site database name and also provide table prefix. Here I Have provided values as below.
new tables with the prefix are created for each subsite.
See some of the tables in an existing database. All tables have a corresponding table with the prefix web.
3.Multi-site with one common shared database using Domain Access module
The domain access module is a contributed module that helps to create multiple sites in same Drupal instance with users, contents and configurations are shared across the sites. Here a single database is shared between subsites and main site.
By default Drupal 8 and Drupal 9 does not allow sharing content and configuration in a multi site environment. This draw back can be resolved by using Domain Access module.
Since code base is shared between multiple sites, you have to handle contents to specific regions based on domains.
If each sites have design with difference in layout and positioning of regions, then it will be difficult to have such a multisite environment using Domain Access module.
While creating content , users and other entities , there will be an option to select to which domain this entity is belongs to. Based on that content will be displayed for in specific sub sites.