Install Apache Virtual Hosts on CentOS 8

Install Apache Virtual Hosts on CentOS 8

Apache Virtual Hosts allows you to run more than one website on a single machine. With Virtual Hosts, you can specify the site document root (the directory containing the website files), create a separate security policy for each site, use different SSL certificates, and much more.

This article explains how to set up Apache Virtual Hosts on a CentOS 8 server.


Ensure that you have met the following requirements before continuing with this tutorial:

  • Domain name pointing to your public server IP.
  • Apache installed on your CentOS system.
  • You are logged in as root or user with sudo privileges.

Creating Directory Structure

The document root is the directory in which the website files for a domain name are stored and served in response to requests. The document root can be set to any location you want.

We will use the following directory structure:

Apache codeFor each domain that will be hosted on the server, we’ll create a separate directory inside /var/www. Within the domain directory, we’ll create a public_html directory that will be the domain document root directory and will store the domain website files.

Let’s start by creating the root directory for the domain example.com:

sudo mkdir -p /var/www/example.com/public_html

For testing purposes, create an index.html file inside the domain’s document root directory:

sudo nano /var/www/example.com/public_html/index.html

Copy and paste the following code into the file:/var/www/example.com/public_html/index.html

<!DOCTYPE html>

<html lang=”en” dir=”ltr”>


<meta charset=”utf-8″>

<title>Welcome to example.com</title>



<h1>Success! example.com home page!</h1>



The above code should look like the image below when you  input it on your system.



To avoid any permission issues change the ownership of the domain document root directory to user apache:

sudo chown -R apache: /var/www/example.com

Creating Virtual Host File

There are a few ways to set up a virtual host. You can either add all Virtual Host Directives in a single file or create a new configuration file for each Virtual Host Directive. Generally, you should prefer the second approach, which is more maintainable.

By default, Apache is configured to load all configuration files that ends with .conf from the /etc/httpd/conf.d/ directory.

To create a virtual host for a specific website open your editor of choice and create the following basic Virtual Host configuration file:/etc/httpd/conf.d/example.com.conf

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com/public_html

<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All

ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined

The above code should look like the image below when you  input it on your system.

html code

  • ServerName: Name of the domain for which the virtual host configuration will be used. This is your domain name.
  • ServerAlias: All other domains for which the virtual host configuration will be used as well, such as the www subdomain.
  • DocumentRoot: The directory from which Apache serves the domain files.
  • OptionsThis directive controls the per-directory server features.
    • -Indexes: Prevents directory listings.
    • FollowSymLinks: This option tells the webserver to follow the symbolic links.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLogCustomLog: The location od the log files.

Edit the file according to your needs and save it.

The configuration file name must end with .conf. You can name the configuration file as you like. The best practice is to use the domain name as the name of the virtual host configuration file.

Test the configuration file syntax with:

sudo apachectl configtest

If there are no errors, the output should look like this:

Syntax OK

To activate a newly created virtual host, restart the Apache service with:

sudo systemctl restart httpd

Open http://example.com to verify that everything is working as expected.

In Conclusion

In this guide, we have outlined how to create an Apache virtual host configuration on CentOS 8. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

If you have any feedback or think there’s something we should add here, please leave you comment below and we will carefully review and consider it.

About the Author
Web Guy
Web Guy has a burning desire to assist as many people as possible obtain a digital presence. We have and will keep striving to provide our clients with enterprise-level performance at an unbeatable cost. Web Guy is proud to offer a wide range of customized, hybrid, virtual private server hosting and web development services. All Web Guy clients enjoy a 99.9% uptime SLA and 24/7 rapid response support team. Web Guy has a core directive to always provide our clients with the best services and infrastructure possible, whether you’re hosting a game server, a high-intensity database, a development environment, or anything in-between.

Leave a Reply