Setup Apache Virtual Hosts on Ubuntu Server

We can host more than one website on Apache with Virtual hosts, in this guide we are going to learn how to set it.

Before we begin

You need to have domain name, already preinstalled Apache.

Also, this tutorial assumes you are sudo user on your system.

I gave my Ubuntu web server which will host files fixed ip

On my Ubuntu client on which we will preview our websites I mapped to following names

On your Ubuntu client (not server which is hosting files) enter

sudo nano /etc/hosts

Reboot client machine after modifying hosts file.

Creating web structure

We will first create directory structure for our webistes on our Ubuntu Web Server. It will look like this:

Main directory will be /var/www, inside it it will be subdirectories for each domain /var/www/, /var/www/ and /var/www/

Inside, and directories we will create public_html directories that will host our website files for each website.

So, our directory structure will look like this




I will show you by example.

Lets create /var/www/ and other directories

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

For visual types here is our directory structure in bash

Inside each website (primjer, primjer1, we will create index.html

sudo nano /var/www/
sudo nano /var/www/
sudo nano /var/www/

Into index.html on each website, enter following (change to primjer1,

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

Save file and repeat procedure for other two websites. Make sure you edit text so it reflects website you are accessing (add and into text).

Before we proceed to creating virtual hosts we will also make sure we have right permissions on our folders.

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

Creating Virtual Hosts

irtual host files can be done in a couple of ways, I will show one, which should be practical to maintain.

Apache on Ubuntu loads .conf files from /etc/apache2/sites-available directory. We will put our virtual hosts files in this folder.

Repeat following procedure for,

sudo nano /etc/apach2/sites-available/

Enter following into .conf file

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

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

    ErrorLog /var/log/
    CustomLog /var/log/ combined

s2ensite will create symbolic link so that will be our next step

sudo a2ensite
sudo a2ensite
sudo a2ensite

We will test our config by entering

sudo apachectl configtest

Syntax OK means that we have a pass. You can ignore AH00558 since we are working only in local network.

As a last step before test, we need to reboot web server

sudo systemctl restart apache2


Into my Ubuntu client machines browser I’m now going to enter

and repeat the same for other two sites…

Everything is getting loaded – success!!