This post shows students and new users the steps to configure an Apache Virtual Host file on Ubuntu Linux. A virtual host is an Apache feature that allows users to run more than one website on a single server.

A virtual host file contains configuration directives for the website, including the site document root, security policies, SSL certificate settings, and much more. Every website. Websites that are configured with Apache Virtual Host work independently of each other with separate and unique settings.

The Apaches Virtual Host feature enables the webmaster to maximize the resources of a server by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures in Ubuntu Linux

When you run multiple websites on a single host, each website will have its own document root. The root of a document is the directory where website files for a domain name are stored and served in response to requests.

Here is an example of a directory structure for multiple websites with unique content and domains.

│   └── public_html
│   └── public_html

As you can see above, each domain will have its own folder with a document root included.

Example: / var / www /domain/ public_html

Run the following commands to create a directory for the domain with your document root.

sudo mkdir -p /var/www/

Each document root will need an index.html file that will be displayed to clients. Run the following commands to create an index.html file.

sudo nano /var/www/

Then copy and paste the content below into the file and save it.

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

Once you save the file, we are ready to configure Apache Virtual Host to reference this content. To avoid permission issues, change ownership of the domain document root directory and all files within the directory to the apache user (www-data):

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

How to create an Apache virtual host file on Ubuntu Linux

Now that you have created a domain content in the above directory, go ahead and configure the Apache Virtual Host configuration file for the above domain content.

On Ubuntu Linux, the Apache Virtual Hosts configuration files are located in the / etc / apache2 / sites-available directory.

To create a virtual host file in the sites directory available for our content above, run the following commands to create a site-specific virtual host configuration file

sudo nano /etc/apache2/sites-available/

Here is an example configuration that should work with most environments. Copy and paste the content below into the file above and save it.

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

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

    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined

Once the file is saved, you can go ahead and enable it to become a virtual host.

To enable the new virtual host file, use the a2ensite helper script that creates a symbolic link from the virtual host file to the enabled sites directory.

Run the following commands to enable the configuration file for our domain.

sudo a2ensite

After that, run the following commands to restart the Apache service.

sudo systemctl restart apache2

Once Apache restarts it now navigates to the server hostname or IP address and it should display the content file we created earlier.

Repeat this for other domains and virtual host files that you want to create. You can create as many virtual host files as you can as long as the server can run all of them.

You should do that!


In this tutorial we have seen how to create Apache virtual hosts on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Write A Comment