How To Configure Apache for Multiple Domains

While a lot of our days are focused on Drupal, as web developers there are other 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, which is part of the Working with Drupal Multisite series, as well as the 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.

  1. 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.)
  2. 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.)
  3. 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).
  4. 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>
    
  5. Save the file with your changes.
  6. 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.
  7. 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.

Comments

hello, with this method i tried to add .... in my /etc/httpd/conf/httpd.conf file but after this change i am unable to restart apache service. Kindly help my website is not working since then.

Regards

Hi zain,

I'd recommend taking a look at your log files to see what exactly the error is. It's likely a small syntax error, but your error log (or the command line if that's what you're using to restart apache) should be able to provide you some solid clues.

Good luck!

It really helps me, because it is new in the linux world or the like.

All the domains point to the same folder. What happens on having different domains to different folders/websites?

For those behind NATed routers what changes need to be made to the router ?

What DNS configurations are needed?

Hi Dom,

In general as long as your router sends traffic to the server with this apache configuration, it will sort out serving the right host.

How to go about doing that will depend on your environment details, and is beyond the scope of this article.

Cheers,

Blake

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <code class> <ul type> <ol start type> <li> <dl> <dt> <dd><h3 id> <p>
  • Lines and paragraphs break automatically.

About us

Drupalize.Me is the best resource for learning Drupal online. We have an extensive library covering multiple versions of Drupal and we are the most accurate and up-to-date Drupal resource. Learn more