How to implement multiple virtual hosts on Ubuntu 21.04

To Implement Multiple Virtual Hosts on Ubuntu 21.04

Introduction

In Apache, virtual hosting is the concept of hosting multiple sites within a single server, there are two types of virtual hosting, IP address hosting and domain hosting, so we can host either IP addresses or domain names.

Installation Procedure:

Check the OS version

linuxhelp@linuxhelp:~$ lsb_release -a

Changing as root user

linuxhelp@linuxhelp:~$ sudo -s

Changing the directory to root

root@linuxhelp:/home/linuxhelp# cd / 

Installing apache web server

root@linuxhelp:/# apt install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
0 upgraded, 8 newly installed, 0 to remove and 254 not upgraded.
Need to get 1,739 kB of archives.
After this operation, 7,550 kB of additional disk space will be used.

Copying the default virtual host .conf file example1.com virtual host

root@linuxhelp:/# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example1.com.conf

Copying the default virtual host .conf file example2.com virtual host

root@linuxhelp:/# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example2.com.conf

Configure the virtual host for example1.com

root@linuxhelp:/# vi /etc/apache2/sites-available/example1.com.conf 

Configure the virtual host for example2.com

root@linuxhelp:/# vi /etc/apache2/sites-available/example2.com.conf 

Making document Root directory as example1

root@linuxhelp:/# mkdir /var/www/html/example1

Making document Root directory as example2

root@linuxhelp:/# mkdir /var/www/html/example2

Making the index.html file for example1.com

root@linuxhelp:/# vi /var/www/html/example1/index.html

Making the index.html file for example1.com

root@linuxhelp:/# vi /var/www/html/example2/index.html

Enabling virtual host site example1.com.

root@linuxhelp:/# a2ensite example1.com.conf
Enabling site example1.com.
To activate the new configuration, you need to run:
  systemctl reload apache2

Enabling virtual host site example2.com.

root@linuxhelp:/# a2ensite example2.com.conf
Enabling site example2.com.
To activate the new configuration, you need to run:
  systemctl reload apache2

Restart the apache server

root@linuxhelp:/# systemctl restart apache2

Put host entries for virtual hosts

root@linuxhelp:/# vi /etc/hosts

Ping the www.example1.com

Ping the www.example1.com

With this the implement of multiple virtual hosts comes to an end.

FAQ
Q
What is Mod_vhost_alias?
A
mod_vhost_alias. The VirtualDocumentRoot directive allows you to determine where Apache HTTP Server will find your documents based on the value of the server name.
Q
How do I get Apache to listen on multiple ports?
A
Open configure file /etc/httpd/conf/httpd. conf and modify the Listen directive tells the server to accept incoming requests on the specified port. Multiple Listen directives may be used to specify the number of ports to listen.
Q
Where is the Apache virtual host file?
A
By default on Ubuntu systems, Apache Virtual Hosts configuration files are stored in /etc/apache2/sites-available directory and can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory.25-Mar-2019
Q
How many virtual hosts can Apache handle?
A
If each virtual host has its own log, the limit is likely 64 due to file descriptor limits. However, you can configure Apache to run more using this guide
Q
What is a virtual host example?
A
Name-based virtual hosts use multiple hostnames for the same IP address.. For instance, a blog server may host both blog1.example.com and blog2.example.com. The biggest issue with name-based virtual hosting is that it is difficult to host multiple secure websites running SSL/TLS.