Configure Apache name-based virtual hosts

To host multiple websites of different domains on a single Apache HTTP Server, you can configure name-based virtual hosts. With name-based virtual hosts, the Apache HTTP Server can distribute different websites to different domains that resolve to the server IP address.

Before you begin

  • Clients and the web server resolve the example.com and example.net domain to the IP address of the web server.

    Note that you must manually add the example.com and example.net domain entries to your DNS server.

About this task

You can configure a name-based virtual host for both the example.com and example.net domains with separate document root directories. Both virtual hosts serve static HTML content.

Procedure

  1. Install the httpd package:
    # dnf install httpd
  2. Edit the /etc/httpd/conf/httpd.conf file:
    1. Append the following virtual host configuration for the example.com domain:
      <VirtualHost *:80>
          DocumentRoot "/var/www/example.com/"
          ServerName example.com
          CustomLog /var/log/httpd/example.com_access.log combined
          ErrorLog /var/log/httpd/example.com_error.log
      </VirtualHost>

      These settings configure the following:

      • All settings in the <VirtualHost *:80> directive are specific for this virtual host.
      • DocumentRoot sets the path to the web content of the virtual host.
      • ServerName sets the domains for which this virtual host serves content.

        To set multiple domains, add the ServerAlias parameter to the configuration and specify the additional domains separated with a space in this parameter.

      • CustomLog sets the path to the access log of the virtual host.
      • ErrorLog sets the path to the error log of the virtual host. Note

        The Apache HTTP Server uses the first virtual host found in the configuration also for requests that do not match any domain set in the ServerName and ServerAlias parameters. This also includes requests sent to the IP address of the server.

  3. Append a similar virtual host configuration for the example.net domain:
    <VirtualHost *:80>
        DocumentRoot "/var/www/example.net/"
        ServerName example.net
        CustomLog /var/log/httpd/example.net_access.log combined
        ErrorLog /var/log/httpd/example.net_error.log
    </VirtualHost>
  4. Create the document roots for both virtual hosts:
    # mkdir /var/www/example.com/
    # mkdir /var/www/example.net/
  5. Install the policycoreutils-python-utils package to run the restorecon command:
    # dnf install policycoreutils-python-utils
  6. If you set paths in the DocumentRoot parameters that are not within /var/www/, set the httpd_sys_content_t context on both document roots:
    # semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?"
    # restorecon -Rv /srv/example.com/
    # semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?"
    # restorecon -Rv /srv/example.net/

    These commands set the httpd_sys_content_t context on the /srv/example.com/ and /srv/example.net/ directory.

  7. If you use firewalld, open port 80 in the local firewall:
    # firewall-cmd --permanent --add-port=80/tcp
    # firewall-cmd --reload
  8. Enable and start the httpd service:
    # systemctl enable --now httpd

Results

  1. Create a different example file in each virtual host’s document root:
    # echo "vHost example.com" > /var/www/example.com/index.html
    # echo "vHost example.net" > /var/www/example.net/index.html
  2. Use a browser and connect to http://example.com. The web server shows the example file from the example.com virtual host.

For details, see httpd(8) and httpd.conf(5) man pages on your system.