Unlock cybersecurity expertise, protect digital frontiers, secure your future today! Join Now

Apache2 Virtual Hosts Configuration on Linux

Apache2 is a versatile, open-source web server supporting virtual hosting, SSL/TLS, and modular features, ideal for hosting multiple secure websites.

When you're managing multiple websites on a single Linux server, Apache2’s Virtual Hosts (vHOSTS) system is what allows you to serve different websites based on the domain name a user enters. Whether you're hosting multiple projects, staging environments, or client websites, virtual hosting is essential.

In this article, we’ll go step-by-step through setting up virtual hosts on Apache2 with real examples, explanations, and command breakdowns.

Apache2 Virtual Hosts Configuration on Linux

What is a Virtual Host?

A virtual host in Apache is like a separate compartment where each website has its own configuration, domain name, root folder, and settings, all while being served from the same web server.

There are two types of vHOSTS:

  • IP-based: Each site uses a unique IP.
  • Name-based (most common): All sites share the same IP but are distinguished by the domain name in the request.

We’ll be using name-based virtual hosting here.

Step-by-Step Setup: Hosting Multiple Sites

We’re going to set up two websites:

  • site1.com
  • site2.com

Each site will live in its own directory and have its own configuration file.

1. Create Document Root Directories

Let’s start by creating the directory structure for each site.

sudo mkdir -p /var/www/site1.com/public_html
sudo mkdir -p /var/www/site2.com/public_html
  • The -p flag ensures that mkdir creates parent directories if they don’t exist.
  • /var/www/site1.com/public_html is where the HTML files for site1 will reside.

Tip: Keep permissions in mind. You’ll usually want to make sure the Apache user (www-data) has read access.

2. Create and Copy Virtual Host Config Files

Now we create individual configuration files for each website by copying Apache’s default config file as a template.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site1.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site2.com.conf

Why do we do this?

Apache comes with a default configuration file named 000-default.conf. It’s a good starting point because it contains a basic structure. We copy it and modify it for each website.

3. Edit the Virtual Host Files

Open each configuration file in your favorite text editor. For example:

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

Update the content like this:

<VirtualHost *:80>
    ServerAdmin abc@abc.net
    ServerName site1.com
    ServerAlias www.site1.com
    DocumentRoot /var/www/site1.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/site1.com_error.log
    CustomLog ${APACHE_LOG_DIR}/site1.com_access.log combined
</VirtualHost>

Let’s break it down:

  • ServerAdmin: Contact email for server issues.
  • ServerName: The main domain name.
  • ServerAlias: Other domains (like www.site1.com).
  • DocumentRoot: Folder containing the website files.
  • ErrorLog and CustomLog: Separate logs for the site, helpful in debugging.

Do the same for site2.com.conf:

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

With contents:

<VirtualHost *:80>
    ServerAdmin abc@abc.net
    ServerName site2.com
    ServerAlias www.site2.com
    DocumentRoot /var/www/site2.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/site2.com_error.log
    CustomLog ${APACHE_LOG_DIR}/site2.com_access.log combined
</VirtualHost>

4. Enable the Virtual Hosts

Now that the configuration files are ready, we need to enable them using Apache’s built-in tools.

sudo a2ensite site1.com.conf
sudo a2ensite site2.com.conf

a2ensite stands for “Apache 2 enable site”. It creates symbolic links in the sites-enabled directory pointing to the sites-available files.

At the same time, disable the default configuration to avoid conflicts:

sudo a2dissite 000-default.conf

a2dissite means “Apache 2 disable site”.

5. Test Your Configuration

Before restarting Apache, always check the syntax:

sudo apache2ctl configtest

You should see:

Syntax OK

If there are any typos or misconfigurations, they’ll show up here.

6. Restart Apache to Apply Changes

Finally, restart the Apache service:

sudo systemctl restart apache2

Or:

sudo service apache2 restart

Apache will now load the two new virtual host configurations and serve your websites accordingly.

Optional: Add Content to Your Sites

You can add simple HTML files for testing:

echo "<h1>Welcome to Site 1</h1>" | sudo tee /var/www/site1.com/public_html/index.html
echo "<h1>Welcome to Site 2</h1>" | sudo tee /var/www/site2.com/public_html/index.html

Visit http://site1.com or http://site2.com in your browser. If you're working locally and not using real domain names, update your /etc/hosts file:

sudo nano /etc/hosts

Add:

127.0.0.1 site1.com
127.0.0.1 site2.com

Final Thoughts

Apache2’s virtual host system is powerful and flexible. It allows you to:

  • Host multiple domains on the same server
  • Maintain organized logs
  • Customize configurations per site
  • Scale projects efficiently