Apache | VirtualHost Configuration | IP-based Virtual Hosts

IP-based Virtual Hosts

IP-based virtual hosting assigns a virtual host to each IP address when a single server has multiple LAN cards and can use multiple IP addresses.

To assign IP-based virtual hosts, use VirtualHost.

<VirtualHost IP address 1:port number>
  ...
  ...
</VirtualHost>

<VirtualHost IP address 2:port number>
  ...
  ...
</VirtualHost>

For example, if the two IP addresses 192.168.1.1 and 192.168.1.2 are assigned, register ww1.devkuma.com as 192.168.1.1 in DNS and ww2.devkuma.com as 192.168.1.2.

To configure two hosts in one Apache instance, write the following in httpd.conf or http-vhosts.conf. From a management perspective, it is better to write this in http-vhosts.conf rather than httpd.conf. For details, see Name-based Virtual Hosts.

<VirtualHost 192.168.1.1:80>
    ServerName     ww1.devkuma.com
    ServerAdmin    devkuma@devkuma.com
    DocumentRoot   "${SRVROOT}/htdocs-ww1"
    CustomLog      logs/ww1.access.log common
    ErrorLog       logs/ww1.error.log
</VirtualHost>

<VirtualHost 192.168.1.2:80>
    ServerName     ww2.devkuma.com
    ServerAdmin    devkuma@devkuma.com
    DocumentRoot   "${SRVROOT}/htdocs-ww2"
    CustomLog      logs/ww2.access.log common
    ErrorLog       logs/ww2.error.log
</VirtualHost>

You need to configure the required settings for each IP address. For example, when ww1.devkuma.com is accessed, the content under the document root ${SRVROOT}/htdocs-ww1, which is configured for 192.168.1.1, is returned.

Because each VirtualHost can have its own individual settings, configure the settings as needed. As shown above, it is recommended to configure ServerName, DocumentRoot, and log-related settings separately. They are not required, however, so common settings may also be used.

You also need to configure access permissions for each document root. In the httpd.conf file, set appropriate permissions for the two document roots as follows.

<Directory "${SRVROOT}/htdocs-ww1">
    Require all granted
</Directory>

<Directory "${SRVROOT}/htdocs-ww2">
    Require all granted
</Directory>

Practice

Note: This was written without actually performing or verifying the exercise because there was no environment available for testing IP-based virtual hosts. Please use it as a reference.