CentOS

How to Set up Apache Virtual Host on CentOS 8

What is a Virtual Host?

A Virtual Host or Vhost is a term of running multiple websites (such as domain1.com, domain2.com, and domain3.com) on a single machine server. It allows you to create multiple websites with a different domain name on a single server.

The Apache webserver provides two ways for virtual hosting, the IP-based, and the name-based virtual host.

  • The IP-based virtual host allows you to have a different IP address for every website on the server.
  • The Name-based virtual host allows you to run multiple websites with the different domain names on the single IP address and a single server.

In this tutorial, we will show you setup Virtual Host for the Apache webserver on CentOS 8 server.

Prerequisites

For this tutorial, make sure you have got an CentOS 8 server with the LAMP Stack installed on it.

Below is the tutorial about the CentOS 8 server installation.

Install CentOS 8 Server

And below is the guide LAMP Stack installation on CentOS 8 server.

Install LAMP Stack on CentOS 8 Server

Before going any further, log in to your Ubuntu server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Define Virtual Host Directory

First, we will define the virtual host directory for the httpd webserver. We will create a new directory that will be used for storing the virtual host configuration file.

Go to the ‘/etc/httpd’ directory and create a new directory called ‘vhosts’.

cd /etc/httpd/
mkdir -p vhosts/

Next, edit the ‘httpd.conf’ configuration file using vim editor.

vim /etc/httpd/conf/httpd.conf

Add the following configuration into it.

IncludeOptional vhosts/*.conf

Save and close.

Next, restart the httpd service and makes sure there is no error.

systemctl restart httpd

And you will get the result as below.

Create Virtual Host Directory for Apache CentOS 8

As a result, you’ve defined the virtual host directory and you’re ready to create a virtual host for your domain.

Step 2 – Create Document Root Directory

In this step, we will create a new document root directory for the Virtual Host domain.

The document-root is a directory for storing your website. The source codes, images, and static files of your website must be stored at the document root directory.

Now go to the Apache/httpd document-root directory ‘/var/www/’.

cd /var/www/

Create a new document-root directory for each domain name.

mkdir -p domain1.com/public_html/
mkdir -p domain2.com/public_html/

After that, change the ownership of the document root directory to ‘apache’ user and group.

chown -R apache:apache domain1.com
chown -R apache:apache domain2.com

And you’ve created a new document root directory.Create Document Root Directory for Virtua Host

As a result, the ‘/var/www/domain1.com/public_html/’ directory will be used as a document-root directory for the ‘domain1.com’, and the ‘/var/www/domain2.com/public_html/’ directory will be used for the ‘domain2.com’.

Step 3 – Create Index.html Page

After setting up the document root directory, we will create a custom index.html file on each document root directory.

Create a new ‘index.html’ file on the ‘domain1.com’ document root directory using the vim editor.

vim /var/www/domain1.com/public_html/index.html

Paste the following HTML script into it.

<html>
  <head>
    <title>Welcome domain1.com!</title>
  </head>
  <body>
    <h1><center>Congratulations - The domain1.com virtual host is working!</center></h1>
  </body>
</html>

Save and close.

Next, create a new ‘index.html’ file on the ‘domain2.com’ document root directory.

vim /var/www/domain2.com/public_html/index.html

Paste the following HTML script into it.

<html>
  <head>
    <title>Welcome domain2.com!</title>
  </head>
  <body>
    <h1><center>Congratulations - The domain2.com virtual host is working!</center></h1>
  </body>
</html>

Save and close.

As a result, you’ve created the ‘index.html’ file for the ‘domain1.com’ and ‘domain2.com’.

Step 4 – Create Apache/httpd Virtual Host Configuration

At this stage, we’ve already created the document root directory and index.html file for our domain name. In this step, we will create a new virtual host configuration for the domian1.com and domain2.com.

We will create a new virtual host configuration named as ‘domain1.conf’ and ‘domain2.conf’ on the ‘/etc/httpd/vhosts’ directory.

Go to the ‘/etc/httpd/vhosts’ directory and create a new virtual host configuration for the ‘domain1.com’.

cd /etc/httpd/vhosts/
vim domain1.conf

Paste the following configuration into it.

<VirtualHost *:80>

    ServerName domain1.com
    ServerAlias www.domain1.com

    ServerAdmin [email protected]
    DocumentRoot /var/www/domain1.com/public_html

    ErrorLog /var/log/httpd/domain1-error.log
    CustomLog /var/log/httpd/domain1-access.log combined

</VirtualHost>

Save and close.

Next, create a new virtual host configuration ‘domain2.conf’ using the vim editor.

vim domain2.conf

Paste the following configuration into it.

<VirtualHost *:80>

    ServerName domain2.com
    ServerAlias www.domain2.com

    ServerAdmin [email protected]
    DocumentRoot /var/www/domain2.com/public_html

    ErrorLog /var/log/httpd/domain2-error.log
    CustomLog /var/log/httpd/domain2-access.log combined

</VirtualHost>

Save and close.

Next, test the Apache/httpd configuration and make sure there is no error. Then restart the httpd service.

apachectl configtest
systemctl restart httpd

And you will get the result as below.

Create Apache Virtual Host on CentOS 8

As can be seen, you’ve created the Apache virtual host configuration for the ‘domain1.com’ and ‘domain2.com’.

Step 5 – Setup Hosts File (Optionally)

If you’re doing this setup on the local computer, you can use the ‘/etc/hosts’ file to redirect the domain name to your local IP address.

On the other hand, you must set up the DNS of your domain name with your self and point your domain name to your server IP address.

Edit the ‘/etc/hosts’ configuration using vim editor.

vim /etc/hosts

Change the IP address with your server IP address and paste into it.

10.5.5.65 domain1.com
10.5.5.65 www.domain1.com

10.5.5.65 domain2.com
10.5.5.65 www.domain2.com

Save and close.

As a result, the domian1.com and domain2.com has  the same ip address 10.5.5.65.

In effect, the ‘domain1.com’ and ‘domain2.com’ are accessible on your local environment.

Step 6 – Testing

Open your web browser and type the ‘domain1.com’ on the address bar.

http://domain1.com/

As a result, you will get the index.html page as below.

Apache Virtual Host for domain1.com

Next, open a new tab and type the ‘domain2.com’ on the address bar.

http://domain2.com/

As a result, you will get the index.html page of the ‘domain2.com’ as below.

Apache Virtual Host for domain2.com

And finally, you’ve successfully setup the Apache virtual host on CentOS 8 server.

Write A Comment