• Digital accessories
  • Server
  • Digital life
  • Privacy policy
  • Contact us
  1. Home
  2. Article
  3. Configure Apache Virtual Host on Ubuntu 20.04 LTS - Tech Blog

Configure Apache Virtual Host on Ubuntu 20.04 LTS - Tech Blog

Rsdaa 24/01/2022 1847

In this tutorial, we are going to configure Apache virtual host on Ubuntu 20.04 LTS.

What is a virtual host? And its uses.

Well, the term Virtual Host refers to the practice of running more than one web site such as company1.example.com and company2.example.com and so on within a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “name-based”, meaning that you have multiple names running on the same IP address.

So, the fact is that they are running on the same physical server but it is not apparent to the end-user. Means Company1 does not have access to Company 2’s files and whatever inside going on. Thus, it secure and reliable.

In the real world, we often called this method shared web hosting. The price for shared web hosting is much lesser than a dedicated web server because many customers can be hosted on a single server.

VIDEO

Prerequisite:

Here we used ubuntu 20.04 LTS for this experiment, It also applicable for 16.04 and higher versions.

Steps to configure Apache Virtual Host:

Open up your terminal and update the package listInstall Apache ServerCreating Virtual Host UsersCreating Index fileConfigure Virtual Host DirectivesAdd Local DNS ResolverTest Virtual Host

Step-1) Update the package list

First thing is first, do update your package list. Go ahead and type

sudo apt update

Step-2) Install Apache HTTP Server

After updating the package list, you need to be installed apache into your system.

sudo apt install apache2

After installing Apache, you could check it perfectly running or not by using the service command

service apache2 status

Step-3) Creating User for Virtual Host

Now we are going to create two users for this experiment one is userA and other is userB. Let’s go and type

sudo useradd -m userAsudo useradd -m userB

Here the flag -m will Create the user’s home directory if it does not exist. Now create a directory named public_html inside each user’s directory. Go ahead and type

sudo mkdir /home/userA/public_htmlsudo mkdir /home/userB/public_html

Now we have a public_html directory under each user’s directory.

Step-4) Creating Index File

Now you need to put some markup text inside public_html to identify your virtual host. So, we are gonna create an index.html file under public_html directory. Go ahead and type

sudo nano /home/userA/public_html/index.html

And put this markup text and save the file.

<html> <head><title>Welcome to abc.com</title> </head> <body><h1>Hello! There welcome to abc.com page</h1> </body></html>

And similarly, do the same for user B.

sudo nano /home/userA/public_html/index.html<html> <head> <title>Welcome to xyz.com</title> </head> <body> <h1>Hello! There welcome to xyz.com page</h1> </body></html>

Now each user had their index file ready to serve over apache.

Step-5) Creating Apache Virtual Host Config

Now it’s time to create vhost config file for each user, go ahead and type

sudo nano /etc/apache2/site-available/abc.com.conf.

So, now you need to tell apache about your vhost and its configuration. Actually, these are called tags and directives.

<VirtualHost *:80> ServerAdmin [email protected]com ServerName abc.com ServerAlias www.abc.com DocumentRoot /home/userA/public_html/<Directory /home/userA/public_html/> Options Indexes FollowSymLinks AllowOverride None Require all granted</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

So, roughly this is a standard Virtual Host structure which I made here, and my recommendation is first to understand your needs and then place the configuration. Do not blindly copy-paste from any tutorial in your production server. This can bring disaster at any time.

Or you could learn more about directives and Virtual host examples at apache documentation just open up the documentation.

Now we are going to create another Virtual host configuration for xyz.com

sudo nano /etc/apache2/site-available/xyz.com.conf.<VirtualHost *:80> ServerAdmin [email protected]com ServerName abc.com ServerAlias www.abc.com DocumentRoot /home/userA/public_html/<Directory /home/userA/public_html/> Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

So, after all this stuff you need to enable your sites. Go ahead and type

sudo a2ensite abc.comsudo a2ensite xyz.com

And finally restart the apache, type

sudo systemctl restart apache2.

Step-6) Add Local DNS Resolver

Now, one last thing you needed is that a local DNS resolver as we are in a local environment, so, edit your hosts file using nano editor and add a local DNS record for each host. And if you are in a production environment then point your public IP to your DNS manager.

sudo nano /etc/hosts

And add the following lines at the end

127.0.0.1 abc.com127.0.0.1 xyz.com

Step-7) Test your Virtual Host

Finally, test your settings, open up the web browser and open xyz.com and you could see the web page serving from xyz vhost.

Also, open another tab aside and type abc.com and this page serving form abc vhost.

You could follow 7 best practices for Apache security:

Keep your Apache updated with the latest releases and patchesDisable Directory ListingDisable unnecessary modulesTurn off unnecessary servicesEnsure that Apache server-info is disabledDisable Trace HTTP RequestDistribute the ownership and don’t run Apache as ‘root’

Hope this tutorial helpful to you. Leave a comment if you have any questions. Also, click on subscribe button to encourage us and get the latest update. Thank you.

The following two tabs change content below.

I like Programming and New Technologies. And work with Linux.

Related


PREV: VNC Connect Error Messages

NEXT: Connect to a Report Server in Management Studio - SQL Server ...

Popular Articles

Hot Articles

Navigation Lists

Back to Top