Configure IP based Apache Virtual Host on RHEL 6.x or CentOS 6.x

Applies to: RHEL 6.x, CentOS 6.x, Apache HTTP Server 2.2

Legacy guide: This article was originally written for RHEL 6 and CentOS 6. CentOS Linux 6 reached end of life on November 30, 2020 and no longer receives security updates. Use a currently supported Linux distribution for new deployments.

Configure Apache IP based virtual hosting

IP-based virtual hosting allows Apache HTTP Server to select a virtual host according to the IP address and port on which a request is received. Each virtual host therefore uses a different IP address, port, or IP/port combination.

For most modern websites, name-based virtual hosting is more convenient because multiple sites can share the same IP address. IP-based virtual hosting is still useful when separate addresses or interfaces are specifically required.

Example Environment

  • Server: RHEL 6.x or CentOS 6.x
  • Apache service: httpd
  • Site 1 IP: 192.0.2.10
  • Site 2 IP: 192.0.2.20
  • HTTP port: 80

The server must already have both IP addresses configured and reachable before Apache can use them for separate virtual hosts.

Configure the Apache Virtual Hosts

On RHEL/CentOS 6, the main Apache configuration is normally stored in /etc/httpd/conf/httpd.conf. Add virtual host definitions similar to the following:

Listen 80

<VirtualHost 192.0.2.10:80>
    ServerName site1.example.com
    DocumentRoot /var/www/site1
    ErrorLog logs/site1_error_log
    CustomLog logs/site1_access_log combined
</VirtualHost>

<VirtualHost 192.0.2.20:80>
    ServerName site2.example.com
    DocumentRoot /var/www/site2
    ErrorLog logs/site2_error_log
    CustomLog logs/site2_access_log combined
</VirtualHost>

Create the document-root directories and add test pages:

mkdir -p /var/www/site1 /var/www/site2
echo "Site 1" > /var/www/site1/index.html
echo "Site 2" > /var/www/site2/index.html

Validate the Configuration

Before restarting Apache, validate the configuration syntax:

service httpd configtest

If the result is Syntax OK, restart the service:

service httpd restart

You can then test each address separately:

curl http://192.0.2.10/
curl http://192.0.2.20/

SELinux and Firewall Notes

If you use document roots outside the standard /var/www hierarchy, SELinux contexts may also need to be configured. The host firewall must allow TCP port 80, or any other port used by the virtual host.

Current Apache Reference

The IP-based virtual hosting concept is still supported by Apache HTTP Server 2.4. Apache notes that name-based virtual hosts are generally simpler unless the design specifically requires different IP addresses.

This article is retained as a historical reference for RHEL/CentOS 6 and Apache 2.2 environments.