What is Virtual Hosting?

Hosting several domains within a single web server is known as virtual hosting. We can save costs and administrative workload by using virtual hosting. There are several options for virtual hosting like IP-based virtual hosting, name-based virtual host, running multiple Apache instances on one machine, etc. But in this tutorial, we will walk through name-based virtual hosting. So let's begin.

Prerequisites

For this tutorial, I have assumed that you have already installed apache2 in Debian; if not, set up your environment first.

Step 1: Setting up Pages

I am currently on a Debian box, and I will show you how to set up a name-based virtual host.

First of all, I will create three different directories for three different sites with domains. We can do this by creating host1.local, host2.local  & host3.local in the following locations.

💡
You can use your own domains for the purposes of this tutorial.
Directories for the .local files

Now I will create index pages for the three sites in the following locations:

Host 1

Host 1 Index Location
<html>
<p> Message from host1.com</p>
</html>
Host 1 Index Contents

Host 2

Host 2 Index Location
<html>
<p> Message from host2.com</p>
</html>
Host 2 Index Contents

Host 3

Host 3 Index Location
<html>
<p> Message from host3.com </p>
</html>
Host 3 Index Contents
💡
You can add your own website content inside these host*.local files.

Step 2: Apache2 Server Config

Now, I will add virtual host configurations inside the apache2 server configuration.

Apache2 Config Location

To confirm which directory the website is in:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Apache Config Contents

Step 3: Domain Configuration Files

Now we will create configuration files for three domains in the following locations:

Domain Config File Locations
ServerName host1.local
ServerAdmin webmaster@host1.local
ServerAlias www.host1.local
DocumentRoot /var/www/host1.local
#Include conf-available/serve-cgi-bin.conf
<Directory /var/www/host1.local>
Domain Config Content

Make new configuration files accordingly for other domains as well. For example, for Host 2, you would repeat the step above but change host1 into host2.

Step 4: Enabling Sites

Now its time to enable the site. Create new configuration files in the following locations for each domain:

Site Enable Config
Enabling site host1.local
Enabling site host2.local
Enabling site host3.local

To activate the new configuration, you need to run:systemctl reload apache2

Don't forget to disable the default configuration by running: a2dissite 000-default.conf as well.

Step 5: Finalizing

We can reload the apache server with this command: systemctl reload apache2

Finally, we have to add our entries in the /etc/hosts file:

/etc/hosts Location

And we will add our entries as,

[Server_IP] host1.local host2.local host3.local

Result

All done, we can now visit our site:

Host 1

Host 2

Host 3

Thank you for reading till the end. Please do not hesitate to leave comments if you have any queries or feedback.