Configuring Virtual Hosts
1. Understanding Name-Based Virtual Hosts
| Directive | Description | Example |
|---|---|---|
| ServerName | Defines the primary name for the server | ServerName example.com:80 |
| ServerAlias | Defines alternative names for the server | ServerAlias www.example.com |
| VirtualHost | Defines a virtual host block for a specific IP and port | <VirtualHost 192.168.1.1:80> |
Example: Basic Name-Based Virtual Host Configuration
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
</VirtualHost>
Note: Name-based virtual hosts rely on the Host header sent by the client's browser to determine which virtual host to serve.
2. Understanding IP-Based Virtual Hosts
| Directive | Description | Default | Notes |
|---|---|---|---|
| NameVirtualHost | Enables name-based virtual hosting | None | Deprecated in Apache 2.4+ |
| VirtualHost | Defines a virtual host block | None | Must specify IP and port |
Example: Basic IP-based Virtual Host
<VirtualHost 192.168.1.10:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>
Note: IP-based virtual hosts require a unique IP address per virtual host.
Warning: Using NameVirtualHost is deprecated in Apache 2.4 and later versions.
3. Understanding Port-Based Virtual Hosts
| Directive | Description | Default | Notes |
|---|---|---|---|
| Listen | Specifies the IP address and port on which the server listens for requests. | 80 (HTTP) | Can be specified multiple times for different ports. |
| VirtualHost | Defines a virtual host for a specific IP address and port combination. | None | Must be enclosed in <VirtualHost> tags. |
Example: Basic Port-Based Virtual Host Configuration
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>
<VirtualHost *:8080>
ServerName example.org
DocumentRoot /var/www/example-org
</VirtualHost>
Note: The
* in <VirtualHost *:80> means the server will listen on all available IP addresses for that port.
Key Points About Port-Based Virtual Hosts
- Each virtual host must specify a unique port or IP address combination.
- Port-based virtual hosts are matched by the requested port number.
- They do not require a specific domain name to be specified in the request.
4. Creating Virtual Host Block
| Directive | Description | Example |
|---|---|---|
| ServerName | Defines the primary domain name for the server | ServerName example.com |
| DocumentRoot | Specifies the directory from which documents are served | DocumentRoot /var/www/example.com |
| ErrorLog | Specifies the file where error logs are written | ErrorLog /var/log/apache2/example.com_error.log |
| CustomLog | Defines the log file for access logs | CustomLog /var/log/apache2/example.com_access.log combined |
Example: Basic Virtual Host Configuration
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
5. Setting Virtual Host ServerName
| Directive | Values | Description |
|---|---|---|
| ServerName | example.com |
Defines the primary hostname for the server |
| ServerName | www.example.com:80 |
Includes port number for specific virtual host |
Example: Setting ServerName in Virtual Host
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>
Note:
ServerName should be unique per virtual host to avoid conflicts in server configuration.
6. Configuring ServerAlias Directive
| Directive | Values | Description |
|---|---|---|
| ServerAlias | alias1.example.com, alias2.example.com |
Defines alternative names for the server. Can include wildcards like *.example.com. |
Example: Basic ServerAlias Usage
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com alias.example.com
DocumentRoot /var/www/example
</VirtualHost>
Example: Wildcard ServerAlias
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /var/www/example
</VirtualHost>
Note:
ServerAlias is case-insensitive and can be used in combination with ServerName to handle multiple domains or subdomains pointing to the same virtual host.
7. Setting Virtual Host DocumentRoot
| Directive | Values | Description |
|---|---|---|
| DocumentRoot | /var/www/html |
Specifies the directory from which the web server serves files |
Example: Setting DocumentRoot in Virtual Host
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory "/var/www/example.com">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Note: The
DocumentRoot must point to a valid directory that exists and is readable by the web server process.
8. Configuring Per-VHost Log Files
| Directive | Description | Default | Notes |
|---|---|---|---|
| ErrorLog | Specifies the path to the error log file for a virtual host. | logs/error_log |
Can be set per-vhost to isolate errors. |
| CustomLog | Defines the format and destination of the access log for a virtual host. | logs/access_log |
Can be set per-vhost to capture specific access logs. |
Example: Setting Per-VHost Log Files
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
Note: Ensure that the log directory has appropriate permissions for the Apache process to write logs.
9. Setting Default Virtual Host
| Directive | Description | Example |
|---|---|---|
| ServerName | Defines the primary name for the server | ServerName example.com:80 |
| DocumentRoot | Specifies the directory from which documents are served | DocumentRoot "/var/www/html" |
| DirectoryIndex | Lists the files to look for when a directory is requested | DirectoryIndex index.html index.php |
Example: Default Virtual Host Configuration
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Note: The
<VirtualHost> directive binds the configuration to a specific IP address and port. If no IP is specified, it defaults to all available IPs.
10. Enabling Virtual Hosts
Example: Basic Virtual Host Configuration
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory "/var/www/example.com">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
| Directive | Description | Default |
|---|---|---|
| VirtualHost | Defines a virtual host block | None |
| ServerName | Primary domain name for the virtual host | None |
| DocumentRoot | Root directory for the virtual host's content | None |
Note: Ensure that the
VirtualHost directive is properly configured in your main server configuration or included files.
Warning: Make sure to enable the
mod_rewrite module if you plan to use URL rewriting rules within virtual hosts.
Enabling Virtual Hosts Summary
- Use
<VirtualHost>blocks to define individual virtual hosts - Set
ServerNameto specify the primary domain name - Define
DocumentRootto point to the directory containing website files - Configure
<Directory>blocks to control access permissions
11. Disabling Virtual Hosts
To disable a virtual host configuration in Apache HTTP Server, you can either comment out the <VirtualHost> directive or remove it entirely from your configuration files.
| Action | Description | Example |
|---|---|---|
| Commenting out | Wrap the virtual host block with # to disable it. |
|
| Removing | Delete the entire <VirtualHost> block from the config file. |
# Removed entirely from config |
Note: After making changes, restart or reload the Apache server using
systemctl reload apache2 or apachectl graceful to apply the changes.12. Testing Virtual Host Configuration
| Command | Description | Notes |
|---|---|---|
| apache2ctl configtest | Checks syntax of configuration files | Returns OK if valid |
| apache2ctl -t | Alias for configtest | Same behavior as configtest |
Note: Ensure all virtual host blocks are syntactically correct before restarting the server.
| Action | Effect | Notes |
|---|---|---|
| reload | Re-reads config files without stopping server | Does not drop existing connections |
| graceful | Same as reload |
Used for graceful restarts |
Warning: Incorrect virtual host settings can cause all sites to become unreachable.
| Log File | Purpose | Relevance |
|---|---|---|
| error.log | Contains errors and warnings during config load | Check for virtual host errors here |
| access.log | Tracks client requests to virtual hosts | Verify that traffic is routed correctly |