Support Online
Skip to main content

Apache Virtual Host Setup

In this guide, you will learn how to configure Virtual Host on the Apache web server running on Ubuntu 16.04.

Thanks to Virtual Host:

  • A single IP address
  • A single server

You can host more than one domain name on it.

Apache provides the correct site directory by looking at the domain name of the incoming request.**

What is Virtual Host?

Virtual Host is the configuration that allows Apache to run multiple websites on the same server.

Example:

Domain NameIndex
example.com/var/www/example.com
testsite.net/var/www/testsite.net

Apache serves content from the correct directory upon request.


Prerequisites

Before starting this guide, you need to have the following ready:

  • Ubuntu 16.04 server
  • User with sudo privilege
  • Apache web server

If Apache is not installed:

sudo apt-get update
sudo apt-get install apache2

Creating the Web Directory Structure

A separate directory should be created for each site.

sudo mkdir -p /var/www/ornek.com/public_html
sudo mkdir -p /var/www/testsite.net/public_html

These directories are used by Apache as DocumentRoot.

Setting Directory Privileges

By default, the directory owner is root. Let's delegate file management to the current user.

sudo chown -R $USER:$USER /var/www/ornek.com/public_html
sudo chown -R $USER:$USER /var/www/testsite.net/public_html

Set permissions so Apache can read files:

sudo chmod -R 755 /var/www

Creating HTML Pages for Testing Purposes

Test page for the first site:

nano /var/www/ornek.com/public_html/index.html
<html>
<head>
<title>GenixNode</title>
</head>

<body>
<h1>Başarılı! ornek.com sitesi çalışıyor.</h1>
</body>

</html>

Copy for Second Site

cp /var/www/ornek.com/public_html/index.html /var/www/testsite.net/public_html/index.html

Edit the file:

nano /var/www/testsite.net/public_html/index.html

Change the domain name to testsite.net.

Apache Virtual Host Configuration

First, we will create a new site configuration file.

sudo nano /etc/apache2/sites-available/ornek.com.conf

Initial site configuration:

<VirtualHost *:80>

ServerAdmin destek@ornek.com
ServerName ornek.com
ServerAlias www.ornek.com

DocumentRoot /var/www/ornek.com/public_html

ErrorLog ${APACHE_LOG_DIR}/genixnode_error.log
CustomLog ${APACHE_LOG_DIR}/genixnode_access.log combined

</VirtualHost>
İkinci site yapılandırması
sudo nano /etc/apache2/sites-available/testsite.net.conf
<VirtualHost *:80>

ServerAdmin admin@testsite.net
ServerName testsite.net
ServerAlias www.testsite.net

DocumentRoot /var/www/testsite.net/public_html

ErrorLog ${APACHE_LOG_DIR}/testsite_error.log
CustomLog ${APACHE_LOG_DIR}/testsite_access.log combined

</VirtualHost>

Enabling Virtual Host Configuration

Activate sites:

sudo a2ensite ornek.com.conf
sudo a2ensite testsite.net.conf

You can turn off the default site:

sudo a2dissite 000-default.conf

Restart Apache:

sudo systemctl restart apache2

Testing from Browser

If your domains are pointed to the server IP address, go to:

http://ornek.com http://testsite.net

Each domain name should point to the page in its own directory.

Frequently Asked Questions

What is Virtual Host?

It is an Apache configuration that allows multiple websites to run on a single server.

What is the difference between ServerName and ServerAlias? ServerName → Main domain ServerAlias → Additional domains (e.g. www)

Why is 000-default.conf turned off?

The default site may cause incorrect matches in some cases.

Does it work on Ubuntu 20.04 and 22.04?

Yes. Virtual Host logic is the same in all modern Apache versions.

What does a2ensite do?

Activates the site in sites-available.

Result

With this guide:

You learned how to configure Apache Virtual Host You published multiple sites on a single server You configured domain-based content serving

This structure is especially ideal for hosting infrastructures and multi-site management.

You can use the GenixNode infrastructure to quickly set up your servers.