Apache Virtual Hosts allows multiple websites to run on one Web server. With virtual hosts, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.

In this tutorial, we’ll provide a step by step instructions about how to set up Apache Virtual Hosts on a CentOS 7 server.

Prerequisites

Make sure you met the following prerequisites before continuing with this tutorial:

Creating Directory Structure

DocumentRoot is the directory where the website files for a domain name are stored and served in response to requests. You can set the document root to any location you want, this example we will use the following directory structure:

/var/www/
├── example.com
│   └── public_html
├── example-1.com
│   └── public_html
├── example-2.com
│   └── public_html

We’re creating a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, we will create a public_html directory that will store the domain website files.

Create the root directory for the domain example.com using the mkdir command :

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

For testing purposes we will create an index.html file inside the domain document root directory. Open your editor and create a HTML file with the following contents:

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

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Copy

All commands are executed as sudo user, so the newly created files and directories are owned by the root user. To make sure there are no permission issues, change the ownership of the domain document root directory to the apache user :

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

#apache

How to Set Up Apache Virtual Hosts on CentOS 7
1.30 GEEK