How to Setup Apache Virtual Hosts on Ubuntu 20.04 (Step by Step). Apache is one of the best web servers that accounts for more than 50% of all active websites and applications on the internet. It is a tool that aims at delivering content on the web without hassle.
Apache web server breaks its components and functionality into units that can be configured and customized independently. One of these units is the Apache Virtual Host; a component used to describe a domain or a website.
Apache Virtual Hosting
Virtual Hosting is an Apache feature that allows users to host more than one website on a single server (physical server or cloud based server). The best thing about this virtual host is that it enables the users to use any virtual host within a single server. It maps out requests of every domain into a document root respectively, where all the files of the sites are located. Generally, Virtual Hosting features are used in Shared web hosting and Reseller web hosting.
There are two main types of virtual hosting, name based hosting and IP based. Each virtual host has its own unique IP address and name based virtual hosts.
1. Name Based Virtual Hosting
Name based virtual hosting uses different domain names controller to host multiple websites on a single server with a single IP address. In name based virtual hosting, all domains will share a single IP. So you will need to map all domains with a single IP via the DNS control panel. Mostly, users rely on the name based virtual host as they find it a lot more convenient than an IP based Virtual Host.
IP based virtual hosting uses multiple IP addresses to host multiple websites on a single server. You will require multiple NICs (Network Interface Card) and assign different IPs to each NIC. This way you can define different directives based on the IP address a request is received on.
Lab Environment
Operating System – Ubuntu 20.04 Application – Apache Web Server IP Address – 192.168.0.10 IP Address – 192.168.0.11 Domain – web1.example.com Domain – web2.example.com
Note: Both domains must be pointed with your server’s IP address.
Follow this post to learn how to setup Apache Virtual Hosts on Ubuntu 20.04.
Here we will need to create an index.html web page inside the web root directory of each website that will be shown when you visit the domain in your browser.
First, create an index.html page for web1.example.com domain:
nano /var/www/html/web1.example.com/index.html
Add the following code:
Welcome to web1.example.com
Success! web1.example.com home page!
Save and close the file then create an index.html page for web2.example.com domain:
nano /var/www/html/web2.example.com/index.html
Add the following code:
Welcome to web2.example.com
Success! web2.example.com home page!
Save and close the file then create an index.html page for ip1:
nano /var/www/html/ip1/index.html
Add the following code:
Welcome to 192.168.0.10
Success! 192.168.0.10 home page!
Save and close the file then create an index.html page for ip2:
nano /var/www/html/ip2/index.html
Add the following code:
Welcome to 192.168.0.11
Success! 192.168.0.11 home page!
Save and close the file then change the ownership of the Apache web root:
By default, Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available directory on Ubuntu 20.04. In this section, we will create an Apache virtual host configuration file for both name based and ip based virtual hosting.
IP based virtual hosting allows you to host multiple websites on a same server by using assigning a separate IP for each website. First, create an Apache virtual host configuration file for IP 192.168.0.10:
At this point, name based and IP based virtual hosting are configured on Ubuntu 20.04 server. Now, it’s time to verify that everything is working as expected.
Open your web browser and access both domain by their name using the URL:
http://web1.example.com
You should see the content of the index.html hosted on the web1.example.com domain in the following page:
http://web2.example.com
You should see the content of the index.html hosted on the web2.example.com domain in the following page:
You can also access both website by their IPs using the URL:
http://192.168.0.10
You should see the content of the index.html hosted on the IP 192.168.0.10 in the following page:
http://192.168.0.11
You should see the content of the index.html hosted on the IP 192.168.0.11 in the following page:
How to Setup Apache Virtual Hosts on Ubuntu 20.04 Conclusion
In this guide, we explained how to setup apache virtual hosts on Ubuntu 20.04. We also setup both name based and IP based virtual hosting by real life examples. I hope you can now easily host multiple websites on a single server using the Apache virtual hosting feature.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.