بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم

A Virtual Host is a way to run more than one website on a single machine. For example, company1.example.com and company2.example.com. There are two kinds of Virtual Host. The first kind is IP-based. That means each website has its own IP address. An IP is the unique address of a computer on a network. The second kind is name-based. That means several website names share one IP address. Visitors cannot see that all the websites run on one physical server. Apache was one of the first servers to support IP-based Virtual Hosts. Apache version 1.1 and later support both kinds of Virtual Host. The name-based kind is also sometimes called host-based or non-IP Virtual Hosts.

Configuration

First, open the /etc/httpd/conf/httpd.conf file:

sudo nano /etc/httpd/conf/httpd.conf

Then remove the hash mark at the start of this line:

Include conf/extra/httpd-vhosts.conf

Next, set up the hosts file. Open it with this command:

sudo nano /etc/hosts

Add this line at the end of the file:

127.0.0.1 contoh.dom

This line makes the address contoh.dom point to your own computer.

img1

Create a vhosts folder:

sudo mkdir /etc/httpd/conf/vhosts

Create a folder for the website files. This folder is called the DocumentRoot. The DocumentRoot is the folder where the website files are stored:

mkdir -p ~/public_html/tugasweb

The ~ sign means your home folder. Also create a simple test file in that folder:

echo "Hello from contoh.dom" > ~/public_html/tugasweb/index.html

Create a file in the vhosts folder. The file name must match the name in hosts:

sudo nano /etc/httpd/conf/vhosts/contoh.dom

Fill the file with this:

<VirtualHost *:80>
    ServerAdmin webmaster@domainname1.dom
    DocumentRoot "/home/febrian/public_html/tugasweb/"
    ServerName contoh.dom
    ServerAlias contoh.dom
    ErrorLog "/var/log/httpd/contoh.dom-error_log"
    CustomLog "/var/log/httpd/contoh.dom-access_log" common

    <Directory "/home/febrian/public_html/tugasweb/">
        Require all granted
    </Directory>
</VirtualHost>

Replace /home/febrian with your own username on your computer if it is different.

img2

Enable the v-host. Add this line to the /etc/httpd/conf/httpd.conf file:

Include conf/vhosts/contoh.dom

img3

Before restarting, check the configuration first. This command makes sure there are no typos:

sudo apachectl configtest

The result should be Syntax OK. If there is an error, read the message and fix the line it mentions.

Once it is OK, restart Apache:

sudo systemctl restart httpd

Open this address in your browser: http://contoh.dom

img4

Hope this helps.

References

  1. https://wiki.archlinux.org/title/Apache_HTTP_Server#Virtual_hosts
  2. https://httpd.apache.org/docs/2.4/vhosts/