Apache virtual hosts allow a single Apache HTTP Server instance to respond to multiple domain names, each with its own document root, logs, and settings. On RHEL 8, virtual host configuration files are placed in /etc/httpd/conf.d/ and are automatically included by the main httpd.conf. This tutorial walks you through creating and enabling an Apache name-based virtual host on RHEL 8.

Prerequisites

  • Apache HTTP Server installed and running on RHEL 8 (see How to Install Apache HTTP Server on RHEL 8)
  • A domain name resolving to your server’s IP, or a local /etc/hosts entry for testing
  • Root or sudo access on the server
  • HTTP traffic permitted through firewalld

Step 1 — Create the Document Root and Test Page

Create a directory structure for your site and set ownership so the current user can write files there:

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Create a simple HTML test page:

echo "

Virtual host for example.com is working

" > /var/www/example.com/html/index.html

Step 2 — Create the Virtual Host Configuration File

Create a new .conf file in /etc/httpd/conf.d/ for your domain:

sudo nano /etc/httpd/conf.d/example.com.conf

Add the following virtual host block:


    ServerName   example.com
    ServerAlias  www.example.com

    DocumentRoot /var/www/example.com/html

    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    

    ErrorLog  /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined

Key directives explained:

  • VirtualHost *:80 — matches all incoming requests on port 80; Apache selects the correct virtual host based on the HTTP Host header
  • ServerName — the primary domain for this virtual host
  • ServerAlias — additional domain names this virtual host accepts (e.g., the www subdomain)
  • DocumentRoot — the directory from which Apache serves files for this site
  • AllowOverride All — permits .htaccess files to override server settings (required for WordPress and similar applications)
  • ErrorLog / CustomLog — site-specific log files for easier debugging

Step 3 — Set the Correct SELinux Context

Because the document root is in /var/www/, Apache can usually read it without changes. However, if you use a non-standard path, you must apply the httpd_sys_content_t SELinux label:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com

If your application writes to the document root (e.g., uploads), use the writable type instead:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.com/html/uploads(/.*)?"
sudo restorecon -Rv /var/www/example.com/html/uploads

Step 4 — Test the Configuration and Reload Apache

Before reloading, validate the configuration syntax with apachectl:

sudo apachectl configtest

A successful result shows Syntax OK. Reload Apache to apply the new virtual host without dropping existing connections:

sudo systemctl reload httpd.service

Step 5 — Test the Virtual Host

If DNS is not yet configured, add a temporary entry to /etc/hosts:

echo "127.0.0.1   example.com www.example.com" | sudo tee -a /etc/hosts

Use curl to confirm the virtual host is responding with the correct content:

curl http://example.com

You should see the test page content. Check the site-specific access log to confirm the request was recorded:

sudo tail /var/log/httpd/example.com_access.log

Conclusion

You have successfully configured a name-based Apache virtual host on RHEL 8, created a dedicated document root, applied appropriate SELinux contexts, and verified that Apache is serving the site correctly. You can host as many virtual hosts as needed by repeating this process for each domain.

Next steps: How to Secure Apache with Let’s Encrypt on RHEL 8, How to Enable mod_rewrite for Apache on RHEL 8, and How to Configure Apache as a Reverse Proxy on RHEL 8.