Book Image

Drupal 7 Multi Sites Configuration

Book Image

Drupal 7 Multi Sites Configuration

Overview of this book

Drupal is one of the most powerful PHP Content Management Systems there is. However, why would you install a CMS for every site you build? With just one copy of Drupal you can host several sites. Drupal has long had multi-site support, and Drupal 7's support is even better than previous versions. Drupal 7 Multi-Sites Configuration will teach you how to host several websites on a single copy of Drupal. You will learn how to create different sites, each with its own domain, content, and theme. This valuable information will help you to save time by managing modules and sharing them across several sites as well as administering your sites all in one place.This book will show you how to configure a system for multi-site, and then install several sites on one copy of Drupal, all with their own domain name and database. Learn how to install and share modules and themes, configure Apache, PHP, and MySQL for multi-site, and then manage the site. Once your site system is successfully set up, discover some of the advanced configurations possible with Drupal multi-site, as well as how to upgrade and maintain your sites.
Table of Contents (11 chapters)

Web servers


Once networking is configured, we can move to the next layer and work with the web server. In a nutshell, a web server's responsibility is to listen for network requests that use the HTTP or HTTPS protocols, and then respond to these requests. Sometimes responding is as simple as delivering a static response or sending the contents of a file on the file system. Images are often served like this. Other times the web server may need to coordinate with other software on the system. Other software may generate the response and feed that back to the web server, which then sends the data back to the client. The original Common Gateway Interface (CGI) worked in this way, and many contemporary web technologies still do. On some servers, like Nginx, PHP is executed this way. Finally, sometimes the web server itself processes and executes code which generates the content. Apache's mod_php module executes PHP code in this w ay. Most web servers, including Apache, Nginx, IIS, and Lighttpd ("lighty") can support the situation where one single computer handles requests for multiple domain names. That is, most web servers can handle multi-site configurations. The exact configuration differs from server to server, so you will need to consult your web server's documentation to learn more. Here we will focus mainly on Apache.

Apache

The Apache server is one of the most popular web servers used on the Internet. Web hosting providers use it. It comes installed on Mac OS X and is readily available on most versions of Unix, as well as on Linux. A special version of it runs on Windows, as well.

Note

If you are looking to set up a local server environment on Windows, you may wish to look at Acquia's pre-configured Windows/MySQL/PHP/Drupal stack available at http://acquia.com.

Apache supports running multiple sites on a single web server through a method called virtual hosting. In this system, a single Apache server can act as if it were multiple servers. And it can do this even if there is only one IP address on the system. For example, to handle two sites, we might configure Apache to have two virtual hosts—one for each site. Each virtual host can then have its own configuration file (or its own section in a larger configuration file) tailored to that site's needs. When Apache receives a request, it looks at the HTTP headers to figure out which virtual host should handle the request, and it hands it processes accordingly.

With its long history and devoted development community, Apache has been built to be remarkably flexible. Multi-site configurations in Apache can be quite sophisticated, and many books have been written about building complex services with Apache. But with domain-aware systems like Drupal, Apache's virtual hosting tools aren't even necessary. A much simpler Apache configuration can handle the necessary HTTP details, while Drupal itself can handle the domain mapping.

For a sophisticated site, you may find it most beneficial to configure Apache to take advantage of virtual hosting, but for this book we will err on the side of simplicity and use only a bare-bones Apache configuration.

Configuring Apache for a Drupal Multi-site

In the last section we looked at setting up a local Vagrant-based virtual machine. Now we will turn our attention back to manual configuration. If you are already running Vagrant, this part of the configuration has been done for you (and you can see it in /etc/apache2/sites-available/books.local.conf on your virtual machine).

We are not going to walk through the entire process of configuring Apache. Instead, I assume that Apache is already working. We will focus on fine-tuning the existing configuration. We will assume the following about Apache:

  • It is installed and configured to listen on port 80.

  • It is correctly serving web pages.

  • The Apache mod_rewrite module is enabled.

  • It is correctly executing PHP using mod_php.

The first thing we need to do is locate the Apache configuration for the default host. By default, Apache comes with a single host configured. On Ubuntu, which is configured with virtual hosting in mind, the file /etc/apache2/sites-available/default.conf contains the default host's configuration. Sometimes this information is in a file called apache-vhost.conf or just httpd.conf.

Note

Apache is usually configured to support virtual hosting out of the box. Even though we are creating three sites, we will create only one virtual host (or re-use the default one).

You do not have to edit the default virtual host. If you are comfortable making a new (single) virtual host for our three sites, feel free to do so. The configuration below applies equally to a single default virtual host or a new virtual host.

The default host configuration typically begins like this:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/
# Lots more below.

Most of our configuration will happen between the first and third lines above. Take note of the location of the Document Root. (You can change it if you want.) We will need to make sure that Drupal is installed at the document root. That is, in the example above, if /var/www is the document root, /var/www/index.php should point to Drupal's index.php file.

Next, after ServerAdmin, insert these two lines:

ServerName books.local
ServerAlias books.local cooks.local looks.local

These tell Apache that this server is named books.local and that it will answer to the names books.local, cooks.local, and looks.local. These are the domains for the three sites we are going to build in this book. Of course, if you were creating the sites example.com and anotherexample.com, you would enter those hostnames here.

Once this configuration is done, you can restart Apache.

A note on using virtual hosting for each domain

In the previous section we configured Apache without using its built-in virtual hosting to handle each site in the multi-site configuration. We let Apache treat all domains as if they were the same, and in the next chapter we will use Drupal to distinguish sites based on domain names.

If you are running a virtual host for each site, which is a good idea in many circumstances, you will need to do something that may seem counterintuitive. You must point all of your sites to the same document root—the one and only place where Drupal lives. This is done because all sites will use the same installation of Drupal.

In many other respects, you will be able to take advantage of Apache's host-specific configuration. You can still, for example, create separate access logs for each virtual host.

Next we will configure MySQL.