46.4. Virtual Hosts

The term virtual host refers to Apache's ability to serve multiple URIs (universal resource identifiers) from the same physical machine. This means that several domains, such as www.example.com and www.example.net, are run by a single Web server on one physical machine.

It is common practice to use virtual hosts to save administrative effort (only a single Web server needs to be maintained) and hardware expenses (each domain does not require a dedicated server). Virtual hosts can be name based, IP based, or port based.

Virtual hosts can be configured via YaST (see Default Host) or by manually editing the Virtual Host section of httpd.conf (see Section 46.3.2, “Configuring Apache Manually”).

By default, Apache in SUSE Linux is prepared for one configuration file per virtual host in /etc/apache2/vhosts.d/. A basic template for a virtual host is provided in this directory (vhost.template). Virtual host configuration can also be added elsewhere, for example, in one file that is then included in the configuration.

[Important]Important

It is very helpful to check the virtual host setup with the command httpd2 -S. It outputs the virtual host settings as understood by Apache and can help you make sure that you get the expected results. If you use Apache with flags like -DSSL, it is necessary to use the same flags when testing, for example, use httpd2 -S -DSSL.

46.4.1. Name-Based Virtual Hosts

With name-based virtual hosts, more than one Web site is served per IP address. Apache uses the host field in the HTTP header sent by the client to connect the request to a matching ServerName entry of one of the virtual host declarations. If no matching ServerName is found, the first specified VirtualHost is used as a default.

NameVirtualHost starts the Virtual Host section in an Apache configuration.

46.4.1.1. NameVirtualHost

NameVirtualHost tells the Apache Web server on which IP address and, optionally, which port to listen for requests by clients containing the domain name in the HTTP header.

The first argument can be a fully qualified domain name, but it is recommended to use the IP address. The second argument is the port and is optional. By default, port 80 is used and is configured via the Listen directive (Network Device Selection).

The wild card * can be used for both the IP address and the port number to receive requests on all interfaces. IPv6 addresses must be enclosed in square brackets.

Example 46.8. Variations of Name-Based VirtualHost Entries

# NameVirtualHost IP-address[:Port]
NameVirtualHost 192.168.1.100:80
NameVirtualHost 192.168.1.100
NameVirtualHost *:80
NameVirtualHost *
NameVirtualHost [2002:c0a8:164::]:80
                

46.4.1.2. <VirtualHost></VirtualHost> in the Name-Based Context

The <VirtualHost></VirtualHost> blocks holds the information that applies to a particular domain. When Apache receives a client request for a defined VirtualHost, it uses the directives enclosed in this section. Any Apache directive that is allowed in the VirtualHost context can be used here. The opening VirtualHost tag takes the following arguments in a name-based virtual host configuration:

  • IP address (or fully qualified domain name) previously declared with the NameVirtualHost directive.

  • Optional port number previously declared with the NameVirtualHost directive.

The wild card * is also allowed as a substitute for the IP address. This syntax is only valid in combination with the wild card usage in NameVirtualHost * . When using IPv6 addresses, the address must be included in square brackets.

Example 46.9. Name-Based VirtualHost Directives

<VirtualHost 192.168.1.100:80>
    ServerName www.example.com
    DocumentRoot /srv/www/htdocs/example.com
    ServerAdmin webmaster@example.com
    ErrorLog /var/log/apache2/www.example.com-error_log
    CustomLog /var/log/apache2/www.example.com-access_log common
</VirtualHost>

<VirtualHost 192.168.1.100:80>
    ServerName www.example.net
    DocumentRoot /srv/www/htdocs/example.net
    ServerAdmin webmaster@example.net
    ErrorLog /var/log/apache2/www.example.net-error_log
    CustomLog /var/log/apache2/www.example.net-access_log common
</VirtualHost>

<VirtualHost [2002:c0a8:164::]>
    # 2002:c0a8:164:: is the IPv6 equivalent to 192.168.1.100
    ServerName www.example.org
    DocumentRoot /srv/www/htdocs/example.org
    ServerAdmin webmaster@example.org
    ErrorLog /var/log/apache2/www.example.org-error_log
    CustomLog /var/log/apache2/www.example.org-access_log common
</VirtualHost>
                

In this example, both the domain www.example.com and www.example.net are hosted on the machine with the IP address 192.168.1.100. The first VirtualHost is the default for all incoming requests to the Web server.

The directives ErrorLog (described in Section 46.3.2.3.4, “ErrorLog file | "|command") and CustomLog (see http://httpd.apache.org/docs-2.0/mod/mod_log_config.html#customlog) do not need to contain the domain name. Here, use a name of your choice.

46.4.2. IP-Based Virtual Hosts

This alternative virtual host configuration requires the setup of multiple IPs for a machine. One instance of Apache hosts several domains, each of which is assigned a different IP.

[Important]IP Addresses and IP-Based Virtual Hosts

The physical server must have one IP address for each IP-based virtual host. If the machine does not have multiple network cards, virtual network interfaces (IP aliasing) can also be used.

46.4.2.1. Configuring IP Aliasing

For Apache to host multiple IPs, the physical machine must accept requests for multiple IPs. This is called multi-IP hosting. Additionally, IP aliasing must be activated in the kernel. This is the default setting in SUSE Linux.

Once the kernel has been configured for IP aliasing, the commands ifconfig and route can be used to set up additional IPs on the host. These commands must be executed as root.

For the following example, it is assumed that the host already has the IP 192.168.0.10 assigned for the network device eth0. Enter the command ifconfig to view the IP of the host. Further IP addresses can be added with the following commands:

ip addr add 192.168.0.20/24 dev eth0
ip addr add 192.168.0.30/24 dev eth0
            

All these IP addresses are assigned to the same physical network device (eth0).

46.4.2.2. <VirtualHost></VirtualHost> in the IP-Based Context

Once IP aliasing has been set up on the system (or the host has been equipped with several network cards), Apache can be configured. A separate VirtualHost block is needed for every virtual server.

The following example shows Apache running on a machine with the IP 192.168.1.10, hosting two domains on the additional IPs 192.168.0.20 and 192.168.0.30. This particular example only works on a private network, because IPs ranging from 192.168.0.0 to 192.168.0.255 are not routed to the public Internet.

Example 46.10. IP-Based VirtualHost Directives

<VirtualHost 192.168.0.20>
    ServerName www.example.com
    DocumentRoot /srv/www/htdocs/example.com
    ServerAdmin webmaster@example.com
    ErrorLog /var/log/apache2/www.example.com-error_log
    CustomLog /var/log/apache2/www.example.com-access_log common
</VirtualHost>

<VirtualHost 192.168.0.30>
    ServerName www.example.net
    DocumentRoot /srv/www/htdocs/example.net
    ServerAdmin tux@example.net
    ErrorLog /var/log/apache2/www.example.net-error_log
    CustomLog /var/log/apache2/www.example.net-access_log common
</VirtualHost>
                

Here, VirtualHost directives are only specified for interfaces other than 192.168.0.10. When a Listen directive (described in Network Device Selection) is also configured for 192.168.0.10, a separate IP-based virtual host must be created to answer HTTP requests to that interface or the directives found in the Main Server section of /etc/apache2/httpd.conf (see Section 46.3.2.3, “Apache Directives in /etc/apache2/httpd.conf: Main Server ”) are applied.