This DevOps tutorial will teach you how to configure your Apache web server to handle multiple domain names and deliver the correct site to your visitors. You’ll create Apache virtual hosts (vhosts), test our domain names to make sure they’re pointing to the correct web server, and then configure the Apache vhost files to make those domain names all point to a designated folder.
This tutorial was originally posted on the blog of Drupalize.Me.
Introduction
As web developers there are pieces of tech that we need to understand and use on a regular basis. One example is Apache web server. This is the most popular web server on the web and likely the one your site uses. While you don’t need to be a system administrator to use Drupal, you do need to understand some basics of Apache when working with domain names, especially if you’re managing multiple domain names on one web server.
This tutorial is based on the Configure Apache for Multiple Domains video at Drupalize.Me, which is part of the Working with Drupal Multisite series, as well as the upcoming Domain Access series. Those series explore two different methods for running multiple sites using different domain names. For both methods, you need to configure your Apache web server to handle multiple domain names and deliver the correct site to your visitors. So in this tutorial, we’ll create Apache virtual hosts (vhosts) to get three different domain names all pointing to the same Drupal installation. We’ll test our domain names to make sure they’re pointing to our web server, and then configure the Apache vhost files to make those domain names all point to the Drupal folder. When we’re done, we’ll have three domain names which all point to the same web server directory.
Prerequisites
This tutorial assumes you have access to an Apache web server, either a server accessible via the internet or a local development server on your computer. (To learn how to install a local web server, see our Installing a Local Development Server guide.) We’re using an Ubuntu server here, and some things will vary for different operating systems. We’ll highlight Ubuntu-specifics as we go, as well as suggestions for different operating systems.
Before we dive in to Apache server configuration, you need to make sure your domain names are actually pointed to the web server you’re working on. If you’re using a domain service, then you need to make sure the domain name is pointing to your correct server IP address. If you’re working locally, you can update your local hosts file to point a domain name to your web server’s localhost.
We’re also accessing and manipulating the server through a command-line interface (using SSH). But you can accomplish the same tasks using a GUI, if you have one available and your web server allows it. If you need to brush up on your command line skills, you can review our Command Line Basics series.
Apache vhosts
The general idea with Apache vhosts is that Apache needs a way to map your incoming domain name with some files it’s supposed to serve. You’re simply creating a record in the Apache configuration that describes this mapping. You’ll identify the incoming domain name(s) and the location of the site files, and Apache takes care of the rest.
In this tutorial, we’re setting up three domain names: udrupal.com, news.udrupal.com, and udrupalalumni.com. All three will point to the same Drupal code base so that we can later create a multisite installation.
Hands-on
Let’s walk through the steps to set up our three domain names on an Ubuntu Apache web server.
Preparation
Before we start, open a browser and go to the domain names you’ll be working with. Verify the domain names are showing the correct server page. If you’ve freshly installed Apache on your server, you should see a page that says something like “It works!” If you’re working locally, you should see your localhost root page. If you see a page not found or the wrong site/server, go back and fix your domain names first.
You also need to know which directory on your server is the home for your websites. If you want to install Drupal in this directory, make sure Drupal core is in there before you get started. And make note of the full path to this directory. (You can use the command pwd
in the command line, or some GUIs will let you right click on the folder name to display or copy the path.)
Configure the Apache vhost
With the domains working properly, we need to configure Apache to route the domain names to our site directory.
- Locate and navigate to your Apache configuration files directory. On Ubuntu this is located at /etc/apache2. (Another common location on other servers is /etc/http.)
- Locate your vhost configuration. On Ubuntu this is in the sites-available directory. (For some systems you’ll directly edit the httpd.conf file. But most systems have another process in place, and you’ll need to create vhosts in separate files or in a specific vhosts configuration file.)
- Create or open the vhost configuration. On Ubuntu, inside of the sites-available directory create a new file. It’s common practice to name the file the same as your domain name (e.g. udrupal.com) But you can name it whatever you want as long as you know what’s what. In this example, I’m going to create one file for all three of my domains since they all point to the same directory. I’ll name this file udrupal. On newer versions of Ubuntu, as of 14.04, you should add a .conf to the end of the file name (e.g. udrupal.conf).
- Add a new vhost record. Now we just need to add the Apache directives to the file. You’ll want to have the ServerName, ServerAlias, and DocumentRoot directives for any vhost. The
directive is added here for Drupal sites to allow the use of .htaccess files. You can find detailed documentation and other examples in the official Apache documentation.
# Begin vhost record on the default HTTP port 80 <VirtualHost *:80> # This is the name of the vhost. ServerName udrupal.com # These are alternative names for this same vhost. # We put the other domains here. They will all go to the same place. ServerAlias news.udrupal.com ServerAlias udrupalalumni.com # Directory where the website code lives. DocumentRoot /home/udrupal/www <Directory /> Options FollowSymLinks AllowOverride All </Directory> </VirtualHost>
- Save the file with your changes.
- Enable the site. On Ubuntu you must now enable the site in order for Apache to pick up your new configuration. From the command line, run the command
sudo a2ensite udrupal
. It should report back that it’s enabling the site and then give you a reload command you need to run. - Restart or reload Apache. Apache will not know about your changes immediately. You need to reload the configuration files. This command will vary based on your system. On Ubuntu, you’ve been helpfully provided the correct command, which will be the apache reload command. You’ll need to use sudo in front of it. For my system in the video, this command was
sudo /etc/init.d/apache2 reload
. Alternatively, you can simply restart Apache.
Test It
Everything should be in place at this point. Go back to your browser, and go to one of the domain names. You should see your site directory loading. And if you have Drupal in that directory, you’ll see the Drupal installation screen.
Summary
In this tutorial, we took three domains and made them all resolve to one Drupal directory on our web server. We confirmed the domains were pointed to the correct server, then we configured our Apache vhosts. We walked through the necessary steps for doing this on Ubuntu, which included creating a sites-available file, adding the minimal vhost directives we needed, and then enabling the site. With the vhost configuration in place, we reloaded Apache to make sure it was properly reading the new configuration. After Apache reloaded, we went to our domain names in a browser and found a Drupal installation screen waiting for us.
While the Apache vhost configuration specifics will vary depending on the operating system you’re using, the basic steps and Apache directives stay the same: add a new vhost directive to the correct configuration file, make sure it’s enabled, if necessary, and then reload or restart Apache.
About the Author:
Addison Berry (add1sun) has been involved with Drupal since 2006. She spent many years with Lullabot as a developer, consultant, and trainer prior to becoming Lullabot’s Director of Education and the Product Manager for Drupalize.Me.
She has been working with technical documentation and training since 2000, and was the Drupal community Documentation Team Lead from 2008 to 2010. She is one of the co-authors for both editions of the O’Reilly book Using Drupal, published in 2008 and 2012. In March 2009 she was awarded a Knight Foundation grant to improve Drupal documentation.
In 2010 Addison was recognized as one of the Most Influential Women in Tech by Fast Company Magazine.