How to set up Apache Virtual Hosts on Ubuntu?

How To Set Up Apache Virtual Hosts On Ubuntu

The purpose of Apache Virtual Hosts is to run multiple websites on a single machine. It could be IP-based or Name-based.

  • IP-based virtual hosting sets different directives based on IP addresses. Therefore, each host needs a separate IP address, and commonly it is used to host websites on distinct ports and interfaces.
  • Name-based virtual hosting allows you to host multiple websites with the same IP address, as the server relies on the client to report the hostname in the form of HTTP headers.


Create directory structure for your websites

In this tutorial, we are using the domains and to demonstrate Apache Virtual Hosts.

We’ll start by creating directories for both the websites in /var/www , execute the following mkdir command with sudo:

sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

Within these directories, there is a public_html directory, where we will keep our website files.

Since we created these directories with the help of the sudo command, they are currently owned by the root user. To grant them permissions, execute the following chown command with sudo:

sudo chown -R $USER:$USER /var/www/
sudo chown -R $USER:$USER /var/www/

Now we’ll add an index.html file with the following script in our public_html directory of both the websites which, will help us later to test out our Apache Virtual Hosts. Use your favorite editor for this purpose.

nano /var/www/

and then copy-paste the following HTML (this is basic HTML code that sets a page title, and adds two headers)

		<title>Welcome to!</title>
			<h3>Congratulations! You've have successfully reached </h3> <h2></h2>

Similarly, do it for the other domain name.

Creating Apache Virtual Host files

We need to create the Virtual Host file for each domain name. Use the cp command to copy the default configuration for each domain name.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

and for our second domain name,

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

Now use your favorite text editor to edit these configuration files. For the first domain name,

sudo nano /etc/apache2/sites-available/
Edit Virtual Host File 1
Edit Virtual Host File

Enter administrator email for ServerAdmin, and the website root directory location for that domain name in DocumentRoot.

Just below DocumentRoot, add lines for ServerName that holds your domain name, and ServerAlias hold aliases (like www) for your domain name, as shown in the picture above.

Similarly, do it for the other domain.

sudo nano /etc/apache2/sites-available/
Virtual Host File For The Other Domain Name
Virtual Host file for the other domain name

Update configuration to enable virtual hosts

Now to enable our virtual hosts, we will use a2ensite utility. Execute the following commands to enable the websites:

sudo a2ensite

and similarly, for the other domain name:

sudo a2ensite

We all set to restart the Apache HTTP server and to activate the new configuration, execute the following systemctl command with sudo:

sudo systemctl reload apache2

Now you can verify by visiting and on your browser.

Congratulations! We have successfully set up Apache Virtual Hosts on Ubuntu.


In this article, we set up Apache Virtual Hosts on Ubuntu, which is very useful in serving multiple websites on the same server.

Thank you for reading! 😀