Set Up Multiple Websites on a Single Drupal Installation

Leave your reply

Introduction

Learn how to set up multiple websites on a single installation of Drupal. This will allow you to point multiple domain names to their own directories in your document root. Each one will function as a stand-alone Drupal website, independent of any other domain on the server.

Requirements

  • A web server up and running, with at least two domain names pointed to it.
  • Drupal installed and running.

Note: For any server with Plesk, applications like Drupal should always be installed and managed through the Plesk interface. See our article Use Drupal on a server with Plesk for step-by-step instructions.

For more information on installing Drupal, see our articles:

Overview

Each website will be served out of the main Drupal directory (in our example, /var/www/html). We will have Apache point the sites to this directory, and Drupal will be able to direct traffic from there.

There are three steps to the process of setting up a website using this method:

  • Set up the Drupal files: Copy the Drupal file sites/default/default.settings.php to sites/default/settings.php, then create a copy of the sites/default directory for each website.
  • Add the domain names to Apache, specifying the main Drupal directory (NOT the sites directory) as the website's document root.
  • Create a database for each website and complete the installation in a browser, as for a standard Drupal installation.

Set Up the Drupal Files

The key directory for this project is the sites/default directory which was created when you installed Drupal.

By default on many installations, this will be in /var/www/html but your server's installation may vary.

First, go to the sites directory:

cd /var/www/html/sites

Make sure the permissions on the default directory are correct by using the command:

sudo chmod -R 777 default

Next, create a copy of the default.settings.php file and rename it to settings.php:

sudo cp default/default.settings.php default/settings.php

Finally, create copies of the default directory for each domain:

sudo cp -a default first-example.com
sudo cp -a default second-example.com

Add the Domain Names to Apache

Next, we need to tell Apache that the domains are being served out of the main Drupal directory. In our example, this directory is /var/www/html.

There are several ways to do this. For this example, we will be creating one file for each domain in Apache's sites directory.

Note: The location and filename of a site's Apache configuration file can vary based on how you or your server administrator has set up hosting. If you already have a system and naming convention in place, it is best to stick with it.

Start by creating the file for the first domain:

CentOS 7:

sudo nano /etc/httpd/conf.d/first-example.com.conf

Ubuntu 14.04:

sudo nano /etc/apache2/sites-available/first-example.com.conf

Put the following into this file:

<VirtualHost *:80>
    DocumentRoot /var/www/html 
    ServerName first-example.com
    ServerAlias www.first-example.com
</VirtualHost>

Save and exit the file, then do the same for the second domain:

CentOS 7:

sudo nano /etc/httpd/conf.d/second-example.com.conf

Ubuntu 14.04:

sudo nano /etc/apache2/sites-available/second-example.com.conf

Put the following into this file:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName second-example.com
    ServerAlias www.second-example.com
</VirtualHost>

Save and exit the file, then restart Apache for the changes to take effect:

CentOS 7:

sudo systemctl restart httpd

Ubuntu 14.04:

sudo services apache2 restart
sudo mysql –u root –p

After you enter the MySQL/MariaDB root user password, you will be logged in to the MySQL/MariaDB client.

Create a database with the command:

CREATE DATABASE [database name];

Replace [database name] with the name you want to use for your new database. For example, to create a database for the first site called first_example the command would be:

CREATE DATABASE first_example;

Create a user for this database and grant them privileges with the command:

GRANT all ON [database name].* TO [database username]@localhost IDENTIFIED BY '[database user password]';
GRANT ALL ON first_example.* TO first_user@localhost IDENTIFIED BY '8cqFZmI95xIK';

Repeat these steps to create a database and user for the second site:

CREATE DATABASE second_example;
GRANT ALL ON second_example.* TO second_user@localhost IDENTIFIED BY 'M7QG4t9CCn';

Once you have finished, exit the database with the command:

quit;

Complete the Installation

You can now complete the Drupal installation by visiting each site in a browser. You will see the Drupal installation page. Select Standard then click Save and Continue.

Install Drupal: Installation Page

On the next page, click Save and Continue.

Install Drupal: Installation Page

On the next page, select MySQL, MariaDB, or equivalent, then fill out the details of the database name, database user, and password which you created in the previous step. Then click Save and Continue.

Install Drupal: Installation Page

On the next page, you will be asked to fill in some website details and create a Drupal Site Maintenance (admin) account. Fill out all the required fields, then scroll down and click Save and Continue.

Install Drupal: Installation Page

Now that the installation is complete, you can go to your main website and begin using Drupal 7.

Install Drupal: Installation Page