How to Set up an Apache Virtual Hosts on Ubuntu Linux

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

A Virtual Host file contains configuration directives for website, including a site’s document root, security policies, SSL certificate configuration, and much more. Each website that is configured within an Apache Virtual Host operates independently of each other with separate and unique settings.

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

How to create website directory structures on Ubuntu Linux

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

Below is an example of a directory structure for multiple website 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 in included.

Example: /var/www/domain/public_html

Run the commands below to create directory for domain with its document root.

sudo mkdir -p /var/www/

Each document root will need an index.html file that will be shown to clients. Run the commands below to create an index.html file for the domain.

sudo nano /var/www/

Then copy and paste the content below into the file and save. Below is just a basic HTML file for testing purposes.

<!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’re ready to configure Apache Virtual Host to reference this content. To avoid any permission issues, change the 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’ve created a domain content at the directory above, go and configure Apache Virtual Host configuration file for the domain content above.

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

To create a virtual host file in the sites-available directory for our content above, run the commands below to create a site specifics Virtual Host configuration file

sudo nano /etc/apache2/sites-available/

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

<VirtualHost *:80>
    ServerAdmin [email protected]
    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 and enable it to become a virtual host.

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

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

sudo a2ensite

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

sudo systemctl restart apache2

Once Apache is restarted, you now browse to the server hostname or IP address and it should display the content file we created above.

Repeat this for other domains and Virtual Host files you want to create. You can create as many virtual host file as you can as long as the server is able to run them all.

That should do it!


This post showed you how to create Apache Virtual Hosts on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.