How To Set Up Apache2 Virtual Hosts on Ubuntu

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

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.04 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: Setting Up Document Root

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 and our directory structure will be

│   └── public_html
│   └── public_html
│   └── public_html

We will create a directory here for the virtual hosts we plan on making. Within each of these directories, we will create a public_html 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/
sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

The -p flag tells mkdir to create any necessary parent directories along the way:

Step 2: Grant Permissions

Since the commands above are executed as a sudo user, the newly created files and directories are owned by the root user.

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

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

Step 3: Creating Virtual Hosts

On Ubuntu, Apache2 Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache2 reads during the startup.

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/>
    Options -Indexes +FollowSymLinks
    AllowOverride All
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/

    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 should now have the ability 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.

If you are facing any problems, feel free to leave a comment.

Related Post

How to Set Up Nginx Server Block on Ubuntu

Here is how you can set up Nginx Server Block on Ubuntu. In particular on Vultr‘s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 22.04 LTS.

How To Create a Sudo User on Ubuntu

Here is how you can create a sudo user on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 18.10 / 22.04 LTS.

Leave a Reply