🇯🇵 日本語 | 🇺🇸 English | 🇪🇸 Español | 🇵🇹 Português | 🇹🇭 ไทย | 🇨🇳 中文

Introduction to Apache Virtual Hosts: Easily Manage Multiple Domains

In the last article, we learned the basics of configuring Apache's core configuration file, `httpd.conf`. You should now be able to confidently publish a single website. But what if you want to run other sites, like `example.org` or `blog.example.com`, on the same server?

The answer is a feature we'll introduce today called Virtual Hosts. This feature allows you to operate multiple different websites, each with its own domain, on a single server (and a single IP address), as if you had multiple servers.

In this article, we'll explain how virtual hosts work using a simple apartment analogy, and we'll walk you through the steps to get two different domains running on your local environment with copy-and-paste-ready code. By the time you finish this article, managing multiple sites should no longer seem intimidating!


Understanding How Virtual Hosts Work

The easiest way to understand virtual hosts is to think of your server as a single apartment building.

When a visitor (a web browser) arrives at the apartment building (the server's IP address) and says, "I'm here to see Mr. Smith (`example.com`)," the building manager (Apache) will direct the visitor to Mr. Smith's apartment (the `example.com` website). If they say, "I'm here for Ms. Jones (`example.org`)," Apache will direct them to the Ms. Jones's apartment.

This mechanism of checking the "nameplate" (the domain name) to switch where to direct visitors is called Name-based Virtual Hosting. It's the most common method used today, and it's the approach we'll be using in this article.


Step 1: Preparing for Virtual Host Setup

Before we start writing configuration files, there are a few things we need to prepare.

1-1. Enable the Virtual Host Configuration File

In Apache, it's recommended to keep your virtual host settings in a separate file to keep the main `httpd.conf` file clean. First, open your `httpd.conf` file and look for the following line. If it has a `#` at the beginning (commenting it out), remove the `#` to enable it.

# Uncomment (remove the #) this line in your httpd.conf file
Include conf/extra/httpd-vhosts.conf

This single line tells Apache to read the `conf/extra/httpd-vhosts.conf` file and apply the virtual host configurations written there.


1-2. Create Directories and Files for Each Site

Next, let's create folders for our two sites, `example.com` and `example.org`, and an HTML file for each of their homepages. In this example, we'll create dedicated folders in a separate location from `htdocs` to make managing website data easier.

  1. In a location of your choice on your server (e.g., /Users/YourName/Sites or C:\Users\YourName\Sites), create a folder for each site.
    • sites/example.com/public_html
    • sites/example.org/public_html
  2. Inside each `public_html` folder, create a simple `index.html` file so you can tell which site is being displayed.

Contents of `sites/example.com/public_html/index.html`:

<!DOCTYPE html>
<html>
<head><title>Welcome!</title></head>
<body><h1>This is the page for example.com</h1></body>
</html>

Contents of `sites/example.org/public_html/index.html`:

<!DOCTYPE html>
<html>
<head><title>Welcome!</title></head>
<body><h1>This is the page for example.org</h1></body>
</html>

Step 2: Configuring the Virtual Hosts (in httpd-vhosts.conf)

With our preparations complete, it's time to edit the `conf/extra/httpd-vhosts.conf` file. In this file, we'll write the "apartment info" for each of our sites.

First, the configuration for `example.com`. Add the following block to `httpd-vhosts.conf`. (You can delete or comment out any existing examples in the file.)

<VirtualHost *:80>
    # The main domain name for this virtual host
    ServerName example.com
    # You can also specify aliases, like the www version
    ServerAlias www.example.com
    # The location of this site's files (the path to the folder we just created)
    DocumentRoot "/Users/YourName/Sites/example.com/public_html"
    # A dedicated error log for this site
    ErrorLog "/Users/YourName/Sites/example.com/error_log"
    # A dedicated access log for this site
    CustomLog "/Users/YourName/Sites/example.com/access_log" common
    # Access permission settings for the directory
    <Directory "/Users/YourName/Sites/example.com/public_html">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

※ Please change paths like `DocumentRoot` to match your own environment.


Next, let's add the configuration for `example.org`. Add this block right below the previous one.

<VirtualHost *:80>
    ServerName example.org
    DocumentRoot "/Users/YourName/Sites/example.org/public_html"
    ErrorLog "/Users/YourName/Sites/example.org/error_log"
    CustomLog "/Users/YourName/Sites/example.org/access_log" common
    <Directory "/Users/YourName/Sites/example.org/public_html">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 3: Testing Locally (Using the hosts file)

Okay, the Apache configuration is done, but if you try to access `example.com` right now, it won't connect to your computer. To test this in a local environment, we need to edit your computer's `hosts` file to forcibly map the domain `example.com` to your own computer (`127.0.0.1`).

Location of the `hosts` file:

Open this file with administrator privileges and add the following lines to the end.

127.0.0.1   example.com
127.0.0.1   www.example.com
127.0.0.1   example.org

Step 4: Restarting and Verifying Apache

All the settings are complete! Finally, let's run a syntax check to make sure there are no mistakes in the Apache configuration, and if everything is okay, restart the server.

# Syntax check (if no errors appear, you're good)
httpd -t

# Restart Apache (on Windows)
httpd.exe -k restart

# Restart Apache (on macOS with Homebrew)
brew services restart httpd

# Restart Apache (on Linux)
sudo systemctl restart apache2 # or httpd

After restarting, try accessing `http://example.com` and `http://example.org` in your browser. If you see "This is the page for example.com" and "This is the page for example.org" respectively, then congratulations, it's a success!


Advanced Example: Running a Subdomain

Virtual hosts can also be used to run subdomains (e.g., `blog.example.com`). The setup method is exactly the same as before. You just need to add a new `<VirtualHost>` block to your `httpd-vhosts.conf` file.

<VirtualHost *:80>
    ServerName blog.example.com
    DocumentRoot "/Users/YourName/Sites/blog.example.com/public_html"
    # ... and write the log and Directory settings in the same way ...
</VirtualHost>

Of course, don't forget to add `127.0.0.1 blog.example.com` to your `hosts` file and restart Apache afterward!


Conclusion

Great work! Today, we learned about the virtual host feature, which allows you to manage multiple domains on a single Apache server, from how it works to the specific setup steps. Hopefully, the apartment analogy made the relationship between the server and domains easier to visualize.

By mastering virtual hosts, you can efficiently manage multiple different projects—like a hobby site, a portfolio site, or a client project in development—all on one machine. Your server administration skills have just leveled up!

Now that you know how to "assign rooms" on your server, you're probably getting curious about how to set more detailed "interior decorations" and "rules" for each room. Our next topic is a complete guide to the magic file that allows for all kinds of control on a per-directory basis: `.htaccess`. Stay tuned!

A Complete Guide to .htaccess: How to Use Redirects, URL Rewriting, and Access Control