How to build multiple Drupal 10 sites directly on the web server using Composer, on a shared web hosting plan, without using git or localhost

Note: If you need to install only one Drupal site, please refer to "How to install Drupal 10 directly on the web server and on a shared web hosting plan using Composer, without needing localhost".

Installing multiple instances of Drupal 10 on a shared web hosting, which requires the Composer, can often be on onerous task for many web developers. This is different from Drupal multisite, fyi, since the Drupal multisite deals with building / managing multiple websites using a single, shared Drupal codebase and multiple MySQL databases. The official documentation available on Drupal.org, gives an incorrect impression that you need to use a localhost or git to build the local or test sites, and then push them to the live web server for the production sites, and you should not use Composer directly on the web server to build Drupal 10 sites.

In this tutorial, we tell you how to install multiple Drupal 10 websites directly on the web server using Composer, and on a shared web hosting plan, without needing to build the test site on git or localhost and push the same to the web server later. We are talking of building / managing multiple websites using Composer on a shared web hosting plan, using multiple Drupal 10 installations and multiple MySQL databases. 

Prerequisite: Composer - If you find installing the Composer on the shared web hosting intimidating, you may avail a shared web hosting plan with Composer pre-installed. Please refer to the best shared web hosting providers with Composer installed by default.

Steps:

  1. Your main domain is "maindomain", whose files are at the web root, i.e., under home/web or home/www or home/public_html. You need to addon domains "adom1", "adom2", etc, for each additional website, so that their respective web roots will be at home/public_html/adom1, home/public_html/adom2, etc (not parked).
  2. Enable the SSH on your hosting plan, by following the procedure mentioned in the concerned knowledgebase, and get it activated by contacting the technical support.
  3. Create multiple MySQL databases like db, db1, db2 ..., using database wizard or phpMyAdmin on the cpanel, allow the necessary permissions for each database, and note down the credentials, like database user name and database password for each database.
  4. Connect to the server using SSH, while using a client like PuTTY.
  5. At home (not on folder www or web or public_html) on the command prompt of SSH, execute the commands "composer create-project drupal/recommended-project site --no-interaction --no-install", "composer create-project drupal/recommended-project site1 --no-interaction --no-install", "composer create-project drupal/recommended-project site2 --no-interaction --no-install" ... . These commands create folders by name "site", "site1", "site2" etc in home , and two files composer.lock and composer.json in each such folder created. These folders will hold Drupal files and folders for websites linked to domains "maindomain", "adom1", "adom2" etc respectively, or for "site", "site1", "site2" ...

        The folder structure looks like:

        | -home

        | -site

        | |-vendor (will be created later, after installation is complete for main domain "maindomain")

        | |-composer.json

        | |-composer.lock

        | -site1

        | |-vendor (will be created later, after installation is complete for addon domain "adom1")

        | |-composer.json

        | |-composer.lock

        | -site2

        | |-vendor (will be created later, after installation is complete for addon domain "adom2")

        | |-composer.json

        | |-composer.lock

        | -public_html

        | | -all main domain "maindomain" Drupal folders and files, for "site"

        | -site1

        | | -all addon domain "adom1" Drupal folders and files for "site1"

        | -site2

        | | -all addon domain "adom2" Drupal folders and files for "site2"

  1. Using the File Manager in the cpanel, edit all files named composer.json, under each folder "site", "site1", "site2" etc, to change their respective web roots, exactly at 9 (nine) occurrences in each file. Replace "web" with "public_html" at 9 locations under "site" for "maindomain"; replace "web" with "../public_html/site1" under "site1" for "adom1" at 9 locations, replace "web" with "../public_html/site2" under "site2" for "adom2" at 9 locations, etc. all in their respective 'composer.json files.
  2. Visit SSH again. Use command "cd site", and over there run the command "composer install" to install Drupal 10 for the primary site "maindomin" under public_html. Then run the command "cd .." to come back to home. To install second site for "adom1", use command "cd site1", and over there run the command "composer install" to install Drupal 10 for the "adom1" under public_html/site1. Then run the command "cd .." to come back to home. To install third site for "adom2", use command "cd site2", and over there run the command "composer install" to install Drupal 10 for the "adom2" under public_html/site2. Then run the command "cd .." to come back to home ...
  3. Visit the browser, type the url of each domain, and install Drupal 10 using the interactive installer as usual, which is to be repeated for each domain and for each site.

Once installed, the "vendor" directory will be created at respective folders, as shown under #5 above, not under "public_html" or web root as recommended by Drupal documentation.

Caution: While installing a new module / installing new theme / updating / upgrading the Drupal core or module or theme using Composer, one has to be in the respective directory by using commands like "cd site", "cd site1", "cd site2" etc for the respective site, or else you end up in meddling the wrong site!

About

About

This web portal, Learnt.In intends you to learn something new, every day, life-long.

List of Authorities for higher education