YouTube Icon

Code Playground.

How To Set Up Apache Virtual Hosts on Ubuntu 20.04


How To Set Up Apache Virtual Hosts on Ubuntu 20.04

A Virtual Host is an Apache arrangement mandate that permits you to run more than one site on a solitary worker. With Virtual Hosts, you can indicate the webpage report root (the registry containing the site records), make a different security strategy for each website, utilize diverse SSL declarations, and substantially more. 

This article portrays how to set up Apache Virtual Hosts on Ubuntu 20.04. 


Guarantee that you have met the accompanying necessities before proceeding with the guide: 

  • At least one space names highlighting your public worker IP. 
  • Apache introduced on your Ubuntu framework. 
  • You are signed in as root or client with sudo benefits . 

Creating the Directory Structure

The archive root is where the site records for an area name are put away and served in light of solicitations. You can set the record root to any area you need, in this model, we will utilize the accompanying index structure: 

?   ??? public_html
?   ??? public_html

Every space facilitated on the worker will have its archive root set to/var/www/<domain_name>/public_html. 

Start by making the root catalog for the area: 

sudo mkdir -p /var/www/

We'll likewise make an index.html record inside the area report root catalog that will be indicated when you visit the space in your program: 

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

Since the orders above are executed as a sudo client, the recently made records and registries are claimed by root. To evade any consent issues change the responsibility for space record root catalog and all documents inside the registry to the apache client (www-information) : 

sudo chown -R www-data: /var/www/

Creating a Virtual Hosts

On Ubuntu frameworks, Apache Virtual Hosts setup documents are situated in/and so on/apache2/destinations accessible catalog. They can be empowered by making emblematic connects to the/and so on/apache2/locales empowered index, which Apache read during the startup. 

Open your word processor of decision and make the accompanying fundamental Virtual Host design record: 

<VirtualHost *:80>
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All

    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined
  • ServerName: The space that should coordinate for this virtual host setup. This ought to be your area name. 
  • ServerAlias: All different areas or subdomains that should coordinate for this virtual host, for example, the www subdomain. 
  • DocumentRoot: The index from which Apache will serve the space records. 
  • Alternatives: This mandate controls which worker highlights are accessible in a particular index. 
    • - Indexes: Prevents registry postings. 
    • FollowSymLinks: When this choice is empowered, Apache will follow the emblematic connections. 
    • AllowOverride: Specifies which orders pronounced in the .htaccess document can abrogate the setup orders. 
  • ErrorLog, CustomLog: Specifies the area for log records. 

You can name the setup record as you like, yet the best practice is to utilize the space name as the name of the virtual host design document. 

To empower the new virtual host document, utilize the a2ensite aide content which makes a representative connection from the virtual host record to the locales empowered catalog: 

sudo a2ensite

The other choice is to physically make a symlink as demonstrated as follows: 

sudo ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

When done, test the setup for any punctuation blunders with: 

sudo apachectl configtest

In the event that there are no mistakes, you will see the accompanying yield: 

Language structure OK 

Restart the Apache administration for the progressions to produce results: 

sudo systemctl restart apache2

At long last to confirm that everything is functioning true to form, open in your program, and you will see the substance of the index.html page: 


You have figured out how to make an apache virtual host design to have various areas on a solitary Ubuntu worker. 

Rehash the means we sketched out above to make extra virtual hosts for every one of your spaces. 

In the event that you are confronting any issue, don't hesitate to leave a remark.