How To Set Up Apache2 Virtual Hosts on Ubuntu 16.04 / 18.04 / 18.10

Here is how you can set up Apache2 Virtual Hosts on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 / 18.10 and 18.04 LTS.

Apache2 Virtual Hosts can run more than one website on a single server machine. With Virtual Hosts, you can specify the multiple site document roots, define a separate security policy for each site, use different SSL certificates and much more.


  • Have an Ubuntu 16.04 x64 / 18.01 x64 / 18.10 x64 instance.
  • Logged in as a root with sudo privileges.
  • You will also need to have Apache2 installed by following these instructions.

To get started with the set up of Apache2 Virtual Hosts, follow the steps below:

Step 1: Create the Directory Structure

The first step that we are going to take is to create a directory structure that will hold the website data and will be serving to visitors. The document root is the directory where the website data for a domain name is stored. You can define the document root to anywhere you want but in this article, we will use the default document root which is /var/www/html and our directory structure will be

|   |__ public
|   |__ public
|   |__ public

We will create a directory here for the virtual hosts we plan on making. Within each of these directories, we will create a public folder that will hold our actual website data. This gives us some flexibility in our hosting.

You can create directories by using the following command:

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

Step 2: Grant Permissions

As we are running the commands as a sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we can change the ownership of the domain document root directory to the apache user (www-data):

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/html/

Step 3: Create a Virtual Hosts

By default, Apache2 Virtual Hosts on Ubuntu configuration files are stored in /etc/apache2/sites-available directory and can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory.

Open your editor of choice and create the basic Virtual Host configuration file in /etc/apache2/sites-available/ with the following code.

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
<Directory /var/www/html/>
    Options -Indexes +FollowSymLinks
    AllowOverride All
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/

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

To enable the new virtual host file we need to create a symbolic link from the virtual host file to the sites-enabled directory, which is read by apache2 during startup. To enable the virtual host is by using the a2ensite helper i.e.

sudo a2ensite
sudo systemctl restart apache2.service

Once done, test the configuration for any syntax errors with:

sudo apachectl configtest

If there are no errors you will see the following output:

Syntax OK

Now that you have your virtual hosts configured, you can test your set up by opening any browser and browse to the server domain name. You should see that everything is working correctly.


You have learned how to set up an apache2 virtual host configuration to host multiple domains on Ubuntu server. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

Related Post

How to Install Apache2 on Ubuntu 16.04 / 18.04 / 18.10

Here is how you can install Apache2 on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 / 18.04 / 18.10.

How to Switch WordPress from PHP 7.2-FPM to PHP 7.3-FPM on Ubuntu 19.10 with Nginx

Here is you will learn how to switch WordPress from PHP 7.2-FPM to PHP 7.3-FPM on Ubuntu 19.10. In particular on Vultr‘s instance i.e. Ubuntu 19.10.

Leave a Reply