Apache Name-Based Virtual Host

Name-Based Virtual Host in Apache

A virtual host allows several websites or domains to run on one server. With name-based virtual hosting, multiple hostnames share one IP address, and Apache selects the correct website from the hostname supplied by the client.

Name-Based Virtual Hosting Overview

Virtual hosting is the practice of running more than one website on one server. Virtual hosts can be configured in two ways:

IP-based virtual hostEach website uses a different IP address.
Name-based virtual hostSeveral website names share the same IP address.

The physical server arrangement is not visible to website visitors. Apache uses the hostname in the HTTP request to select the matching virtual-host configuration.

Open Apache Configuration

1

Open Configuration Manager

Locate the Apache server in the environment and click Config.

Open Apache server configuration
Open Configuration Manager for the Apache application server.
2

Open httpd.conf

Navigate to the following Apache configuration file:

/etc/httpd/conf/httpd.conf

Enable NameVirtualHost

Before configuring the virtual hosts, ensure that the NameVirtualHost line is uncommented:

NameVirtualHost *:80
Enable NameVirtualHost in Apache
Uncomment the NameVirtualHost directive for port 80.
i

Version-specific directive

The source workflow uses NameVirtualHost *:80. In newer Apache versions, name-based virtual hosting is enabled automatically when several VirtualHost blocks use the same address and port.

Create VirtualHost Blocks

Locate the <VirtualHost> section and create one block for every hostname that the server must handle.

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/webroot/example
</VirtualHost>

<VirtualHost *:80>
    ServerName another-example.com
    DocumentRoot /var/www/webroot/another-example
</VirtualHost>
Create Apache VirtualHost blocks
Create a separate VirtualHost block for every website.
!

VirtualHost address must match

The argument used in each <VirtualHost> directive must match the defined NameVirtualHost address. In this example, both use *:80.

Each VirtualHost block requires at least these directives:

  • ServerName: Uniquely identifies the virtual host. Use the website’s primary hostname.
  • DocumentRoot: Defines the filesystem directory containing that website’s files.

Configure ServerAlias

Use the ServerAlias directive when one website should be available through several names. Wildcards such as * and ? can also be used.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com *.example.com
    DocumentRoot /var/www/webroot/example
</VirtualHost>
Configure ServerAlias in Apache
Add alternate hostnames inside the relevant VirtualHost block.

Additional Apache directives can be placed inside a VirtualHost container to customize only that website. Settings defined in the main server context are used unless they are overridden inside the matching VirtualHost block.

Check DNS first

Each hostname configured in ServerName or ServerAlias must resolve to the Apache environment through the corresponding DNS record.

Expected Result

Apache serves several websites from the same IP address. Each request is directed to the matching DocumentRoot according to the requested hostname.

Important Notes

  • Create one VirtualHost block for each primary hostname.
  • Use ServerAlias for additional names that should open the same website.
  • Ensure every domain points to the environment through DNS.
  • The first matching VirtualHost can become the default when no ServerName or ServerAlias matches.
  • Validate the Apache configuration before restarting the server.
  • Restart or reload Apache after saving httpd.conf.

Common Issues and Solutions

Every domain opens the same websiteCheck the ServerName, ServerAlias, VirtualHost order, DNS records, and DocumentRoot paths.
Domain does not openConfirm that the domain resolves to the environment and that Apache is listening on the configured port.
Apache fails to restartCheck the VirtualHost syntax, closing tags, duplicate directives, and filesystem paths.
www hostname does not workAdd the www hostname through ServerAlias and create the corresponding DNS record.
Wrong site is used as defaultReview the order of the VirtualHost blocks because the first applicable host is used when no hostname matches.