How to Set Up Apache Virtual Hosts on CentOS 8

How to Set Up Apache Virtual Hosts on CentOS 8

Introduction

In this article, you will learn about how to set up Apache virtual hosts on a Centos 8.

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:

/var/www/
├── example.com
│   └── index.html

For each domain that will be hosted on the server, we’ll create a separate directory inside /var/www.

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

mkdir /var/www/example.com

2021-09-26_16h00_21.png

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

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

Copy and paste the following code into the file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to your First Website</title>
  </head>
  <body>
    <h1>Success! First Website created!</h1>
  </body>
</html>

Using the cat command to view the content. image.png

Create Demo Page for the Virtual Host

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:

Create your conf file using the following command and add the configuration given.

vim  /etc/httpd/conf.d/example.conf
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com

</VirtualHost>
  • 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.

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.

Testing your configuration

apachectl configtest

If there are no errors, the output should say Syntax ok:

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

systemctl restart httpd

To test your configuration on your termination, use the command curl.

curl your_server_ip

image.png

If you try to curl using example.com or example.com, you will get an error. As these site/domains are not the public so no entry will be there to easy the steps we needed to edit the /etc/hosts file so that if we browse with domain names example.com and example.com we can see the web pages

vim /etc/hosts

image.png

Now that you have two hosts configured, you can test your setup easily by opening the favorite browser and open the domains or in this tutorial we will be using the curl command.

curl http://example.com
curl http://www.example.com

2021-09-26_16h25_41.png

Conclusion

In this tutorial, we have shown you 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.

Thank you.