How to Configure Multiple Domains Using Virtual Hosts on Apache Ubuntu

Introduction

Apache is a widely used web server that can host multiple websites and domains on a single server. Apache Virtual Hosts allow each domain to use its own document root and configuration while sharing the same server.

This guide explains how to configure multiple domains on an Ubuntu server using Apache Virtual Hosts.

For this example, we will configure:

  • domain1.tld
  • domain2.tld

Each domain will have its own document root and VirtualHost configuration.

Prerequisites

Before configuring the Virtual Hosts, make sure you have:

  1. An Ubuntu server.
  2. Apache2 installed and running.
  3. Root or sudo access to the server.
  4. Two domain names.
  5. Both domains pointed to the server’s IP address.

In this example:

  • domain1.tld → /var/www/domain1.tld
  • domain2.tld → /var/www/domain2.tld

If Apache is not installed, install and configure Apache before proceeding.

Implementation

Step 1: Create Document Root Directories

Create separate document root directories for both domains:

mkdir /var/www/domain1.tld
mkdir /var/www/domain2.tld

These directories will contain the website files for each domain.

Step 2: Set Directory Ownership

Set the ownership of the directories to the Apache web server user:

chown www-data. /var/www/domain1.tld
chown www-data. /var/www/domain2.tld

On Ubuntu, Apache commonly runs as the www-data user.

Step 3: Create VirtualHost Configuration Files

Apache stores site configurations under:

/etc/apache2/sites-available/

Create a separate configuration file for each domain by copying the default VirtualHost configuration:

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.tld.conf
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain2.tld.conf

Step 4: Configure the VirtualHost for domain1.tld

Open the configuration file:

vi /etc/apache2/sites-available/domain1.tld.conf

Update the VirtualHost configuration with the domain-specific values:

<VirtualHost *:80>

    ServerAdmin admin@domain1.tld
    ServerName domain1.tld
    ServerAlias www.domain1.tld
    DocumentRoot /var/www/domain1.tld

</VirtualHost>

Here:

  • ServerName defines the primary domain.
  • ServerAlias allows the www version of the domain.
  • DocumentRoot specifies where the website files are stored.
  • ServerAdmin specifies the administrator email address.

Step 5: Configure the VirtualHost for domain2.tld

Open the second configuration file:

vi /etc/apache2/sites-available/domain2.tld.conf

Update it with the corresponding domain details:

<VirtualHost *:80>

    ServerAdmin admin@domain2.tld
    ServerName domain2.tld
    ServerAlias www.domain2.tld
    DocumentRoot /var/www/domain2.tld

</VirtualHost>

Each VirtualHost points to a different document root, allowing Apache to serve different website content for each domain.

Step 6: Enable the Websites

Enable both VirtualHost configurations using a2ensite:

a2ensite domain1.tld
a2ensite domain2.tld

Apache will create the required links under the enabled-sites configuration directory.

Step 7: Check Apache Configuration

Before restarting Apache, check the configuration for syntax errors:

apachectl -t

A successful configuration should return:

Syntax OK

If Apache reports an error, correct the configuration before continuing.

Step 8: Restart Apache

Restart Apache to apply the changes:

service apache2 restart

Alternatively, on modern Ubuntu systems:

systemctl restart apache2

Step 9: Upload Website Content

Upload the website files into their respective document root directories:

/var/www/domain1.tld
/var/www/domain2.tld

For example:

/var/www/domain1.tld/index.html
/var/www/domain2.tld/index.html

Each domain can now contain completely different website content.

Verify the Virtual Hosts

If both domains are already pointing to the server’s IP address, open them in a browser:

http://domain1.tld

and:

http://domain2.tld

Apache will use the requested domain name to determine which VirtualHost configuration and document root should handle the request.

For example:

domain1.tld → /var/www/domain1.tld
domain2.tld → /var/www/domain2.tld

Testing Before DNS Is Updated

If the domains are not yet pointed to the server, you can test the VirtualHost configuration locally by modifying the /etc/hosts file on a Linux or macOS system.

Open the hosts file:

vi /etc/hosts

Add entries similar to:

xx.xx.xx.xx domain1.tld
xx.xx.xx.xx domain2.tld

Replace xx.xx.xx.xx with the IP address of the Apache server.

After saving the file, access the domains from your browser:

http://domain1.tld
http://domain2.tld

This allows you to test the Apache VirtualHost configuration before making the DNS changes publicly.

Conclusion

Apache Virtual Hosts make it possible to host multiple domains on a single Ubuntu server. By creating a separate document root and VirtualHost configuration for each domain, Apache can serve different website content based on the requested domain name.

The basic process is:

  1. Create document root directories.
  2. Set the appropriate ownership.
  3. Create VirtualHost configuration files.
  4. Configure ServerName, ServerAlias, and DocumentRoot.
  5. Enable the sites using a2ensite.
  6. Validate the Apache configuration.
  7. Restart or reload Apache.
  8. Point the domains to the server and verify access.

Frequently Asked Questions

1. Can multiple domains use the same Apache server?

Yes. Apache Virtual Hosts allow multiple domains to be hosted on a single server, with each domain using its own configuration and document root.

2. What is the purpose of ServerAlias?

ServerAlias specifies additional hostnames that should use the same VirtualHost. For example:

ServerName domain1.tld
ServerAlias www.domain1.tld

Both domain1.tld and www.domain1.tld will use the same VirtualHost configuration.

3. How can I check whether my Apache configuration has errors?

Run:

apachectl -t

If the configuration is valid, Apache will return:

Syntax OK

Installing ModSecurity with OWASP CRS on Apache (Ubuntu)

How to Deploy WordPress Behind a Reverse Proxy on cPanel/Apache

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top