How To Enable Multiple Sites on a Drupal Installation On CentOS server
Introduction
Drupal is a popular content management system (CMS) that is used to quickly deploy websites on sites in an easy to use framework.
This guide will cover how to deploy multiple sites from within a single Drupal installation. We will be installing the latest version of Drupal as of this writing (7.23), on an Ubuntu 12.04 VPS. We will create two separate sites served from a single installation.
Install the Prerequisites
Drupal needs a LAMP (Linux, Apache, MySQL, and PHP) stack installed in order to function properly. We will install the necessary components by typing:
sudo apt-get update
sudo apt-get install apache2 mysql-server libapache2-mod-auth-mysql php5-mysql php5 libapache2-mod-php5 php5-mcrypt php5-gd
You will be asked to create and confirm an administrative password for the MySQL portion of the installation. Remember this for later, as you will have to use it in a moment.
Configure apache to preference .php files over .html files by typing:
sudo nano /etc/apache2/mods-enabled/dir.conf
Add a parameter reading “index.php” as the first item after “DirectoryIndex”:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Save and close the file.
Implement these changes in Apache by restarting the server:
sudo service apache2 restart
Configure MySQL
You can perform a preliminary database initialization and lock down some insecure settings by typing:
sudo mysql_install_db && sudo mysql_secure_installation
You will be prompted to enter the administrative password that you configured during the MySQL installation.
You will then be asked some questions to secure your install. Press “Enter” to answer yes to all of the questions besides the first (changing the admin password again).
Log into MySQL by typing the following:
mysql -u root -p
You will be given a MySQL prompt to input further commands. We need to create one database for each site we will be creating:
CREATE DATABASE firstsite; CREATE DATABASE secondsite;
Next, create a user and password combination for each site and grant it privileges on the databases you just created:
GRANT ALL ON firstsite.* TO firstuser@localhost IDENTIFIED BY 'firstpassword'; GRANT ALL ON secondsite.* TO seconduser@localhost IDENTIFIED BY 'secondpassword';
Apply the changes and exit the MySQL environment by typing the following commands:
FLUSH PRIVILEGES;
exit
Download and Configure Drupal
We will download the Drupal sources into our home directory from the project’s repositories:
cd ~
wget http://ftp.drupal.org/files/projects/drupal-7.23.tar.gz
Extract the files and then more them into the webroot of the server. We will store all of the drupal files inside a separate “drupal” directory:
tar xzvf drupal-7.23.tar.gz
sudo mv drupal-7.23 /var/www/drupal
Next, we will copy the default settings file into a valid settings file:
cd /var/www/drupal/sites
cp default/default.settings.php default/settings.php
Drupal needs write access to the directory and the files within during installation. We can assign write permissions like this:
sudo chmod -R a+rw default
Next, we will have to figure out how we are going to serve the sites from subdirectories or sub domains. Choose the appropriate section to follow below.
Serving Multiple Sites Using Subdirectories
Drupal can be configured to serve separate sites out of subdirectories from a centralized domain. For example, you could have “example.com/site1” and “example.com/site2”, each having separate content, themes, etc.
How Drupal Associates Requests with Directories
First, you need to understand that Drupal resolves requests for domains that get passed to it by looking at the directory structure within the “sites” directory.
The “default” directory describes its function perfectly. It is contains the default content served if no other matches are found.
We want to create other directories that will match our site subdirectories, however. We do this by simply specifying the main domain name for the site (without the “www”), followed by a dot instead of a slash, and the subdirectory name.
For instance, a site that is accessed by going to “http://www.example.com/firstsite” should be given a directory called “example.com.firstsite”.
Create the Site Directories
We will copy the default directory we configured earlier to two separate directories within the “/var/www/drupal/sites” directory.
We will use the “-a” flag to preserve permissions, since we will also need those same permissions for each of the Drupal sites we are creating:
cp -a default example.com.firstsite cp -a default example.com.secondsite
We now have the directory structure that will handle the requests for those sites, but we haven’t made any changes that will direct the traffic Apache is sorting through into Drupal.
We will do this by creating symbolic links to the drupal root directory from within the web root directory, each describing our sites:
sudo ln -s /var/www/drupal /var/www/firstsite sudo ln -s /var/www/drupal /var/www/secondsite
Now, you can set up your separate Drupal installations by navigating to:
http://www.example.com/firstsite
http://www.example.com/secondsite
Be sure to enter the separate database information you configured for each site.
Serving Multiple Sites With Independent Domains
If you have bought two completely separate domains, you will preform most of the same procedures that are used in the subdirectories method above, but you will have to name the directories differently, and you must modify the Virtual Hosts file to configure the domains.
We will assume in this section that your domains have both been pointed at your VPS. We will use the names “firstsite.com” and “secondsite.com” to distinguish.
Go to the Drupal sites directory if you are not already there and copy the default again to appropriately named sub-directories. Remember, do not include the “www” in your naming convention.
cd /var/www/drupal/sites cp -a default firstsite.com cp -a default secondsite.com
Configure Apache Virtual Hosts
Again, we need to be able to inform Apache how to pass the correct domains to Drupal. We will do this with a Virtual Hosts file.
Go to Apache’s sites available directory and create a new Virtual Hosts file for Drupal:
cd /etc/apache2/sites-available
sudo nano drupal
You need to point both of your names to the drupal folder in the webroot in this file. Use this format:
<VirtualHost *:80> DocumentRoot /var/www/drupal ServerName www.firstsite.com ServerAlias firstsite.com *.firstsite.com </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/drupal ServerName www.secondsite.com ServerAlias secondsite.com *.secondsite.com </VirtualHost>
Save and close the file.
Enable the new site configuration by typing:
sudo a2ensite drupal
Reload the server configuration:
sudo service apache2 restart
Your sites should be available to set up in your browser (assuming you set up your domain names correctly) by visiting:
www.firstsite.com
www.secondsite.com
Serving Multiple Sites Using Subdomains
A third way that you can configure this to serve separate sites is through the use of subdomains within a single parent domain.
For instance, we could have one site located at “first.example.com” and a second located at “second.example.com”.
To complete this, we start again with creating the appropriate site directories:
cd /var/www/drupal cp -a default first.example.com cp -a default second.example.com
Create an Apache Virtual Host file to point your base domain to the “drupal” subdirectory within the document root:
cd /etc/apache2/sites-available
sudo nano drupal
Put the following basic configuration in the file:
<VirtualHost *:80> DocumentRoot /var/www/drupal ServerName www.example.com ServerAlias example.com *.example.com </VirtualHost>
Save and close the file.
Enable the new site by typing:
sudo a2ensite drupal
Reload the server configuration:
sudo service apache2 restart
Your sites will be available for set up in your browser by visiting:
first.example.com
second.example.com