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:
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
Open Configuration Manager
Locate the Apache server in the environment and click Config.

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

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>

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>

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.
