Home UbuntuHow to Host Multiple Websites with Apache Virtual Hosts on Ubuntu

How to Host Multiple Websites with Apache Virtual Hosts on Ubuntu

by Knowledge Base Linux
How to Host Multiple Websites with — How to Host Multiple Websites with Apache Virtual Hosts on Ubuntu

This article covers How to Host Multiple Websites with in detail. With Apache virtual hosts on Ubuntu, a single server can host many independent websites, each with its own domain, document root and log files. Instead of buying a separate VPS for every small site, you run them side by side on one machine and Apache decides which site to serve based on the domain name in the request. This is called name-based virtual hosting, and it is how most shared and multi-site setups work. In this guide you will create two Apache virtual hosts on Ubuntu, enable them properly and fix the mistakes that usually make the wrong site appear.

How Apache Virtual Hosts on Ubuntu Work – How to Host Multiple Websites with

When a browser connects to your server it sends a Host header with the domain it wants. Apache compares that header against the ServerName of every enabled virtual host and serves the matching site. If no name matches, Apache falls back to the default virtual host, which is simply the first one loaded. Ubuntu stores available site definitions in /etc/apache2/sites-available/ and activates them by symlinking into /etc/apache2/sites-enabled/ with the a2ensite tool, so you never edit files in sites-enabled directly.

Prerequisites – How to Host Multiple Websites with

  • An Ubuntu server with Apache installed and running. If you have not installed it yet, follow our LAMP stack on Ubuntu 24.04 guide first.
  • Two domain names or subdomains, for example site1.example.com and site2.example.com, with DNS A records pointing to the server.
  • A sudo user account.

Step 1: Create the Document Roots

Each site gets its own directory under /var/www/. Create both roots and a simple identifying page in each:

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

Give the web server ownership of the files so Apache can read them and your applications can write uploads:

sudo chown -R www-data:www-data /var/www/site1.example.com /var/www/site2.example.com
sudo chmod -R 755 /var/www
Domains connected to Apache virtual hosts on Ubuntu

Step 2: Write the Virtual Host Files

Create one configuration file per site in sites-available. The first file, /etc/apache2/sites-available/site1.example.com.conf:

<VirtualHost *:80>
    ServerName site1.example.com
    ServerAlias www.site1.example.com
    DocumentRoot /var/www/site1.example.com
    ErrorLog /var/log/apache2/site1_error.log
    CustomLog /var/log/apache2/site1_access.log combined
    <Directory /var/www/site1.example.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Create the second file the same way, replacing every site1 with site2. Separate log files per site make troubleshooting far easier later, and AllowOverride All lets applications such as WordPress use their own .htaccess rules. Disabling Indexes prevents visitors from browsing directory listings.

Step 3: Enable the Sites and Reload Apache

sudo a2ensite site1.example.com.conf
sudo a2ensite site2.example.com.conf
sudo a2dissite 000-default.conf
sudo apachectl configtest
sudo systemctl reload apache2

configtest must answer Syntax OK before you reload. Disabling the default site is optional, but recommended: unmatched requests then fall through to your first real site instead of the Ubuntu welcome page, which makes misconfigurations obvious rather than confusing.

Step 4: Test Both Virtual Hosts

If DNS has not propagated yet, test locally from the server itself by telling curl which name to request:

curl --resolve site1.example.com:80:127.0.0.1 http://site1.example.com
curl --resolve site2.example.com:80:127.0.0.1 http://site2.example.com

Each command must return its own welcome heading. From your workstation you can achieve the same by adding both names to your local hosts file temporarily. Once DNS is live, test in a real browser, and when you are ready to add HTTPS, the same per-site workflow applies — our tutorial on Let’s Encrypt SSL explains the certificate concepts, and Certbot’s Apache plugin automates them for virtual hosts.

Adding a Third, Fourth or Tenth Site

The process scales linearly: every additional website is just another directory, another configuration file and another a2ensite. There is no hard limit in Apache itself; in practice, small VPS plans run out of RAM and CPU long before they run out of virtual hosts, because idle sites consume almost nothing. Busy sites are the ones to watch, and per-site logs plus a monitoring tool will tell you which domain is eating the resources.

Troubleshooting

The wrong website is served. Almost always a ServerName typo, a forgotten a2ensite, or DNS pointing at a different server. Run sudo apachectl -S to list every virtual host Apache loaded and the order it loaded them in.

403 Forbidden on a new site. The <Directory> block is missing Require all granted, or the document root permissions block www-data. Check that every directory from /var/www down to the site root is executable by others.

Changes have no effect. You edited a file in sites-available but never reloaded, or you edited the symlink target of a different site. Always finish with configtest plus systemctl reload apache2, and read the site’s own error log under /var/log/apache2/.

Both names show the first site. The second configuration file was never enabled, or both files declare the same ServerName. Compare the outputs of ls /etc/apache2/sites-enabled/ and apachectl -S.

Conclusion

Apache virtual hosts on Ubuntu let you consolidate many small websites onto one server without them interfering with each other: one directory, one config file and one set of logs per site. Create the roots, write careful ServerName definitions, enable with a2ensite, and verify with apachectl -S and curl --resolve before blaming DNS. The official reference for every directive used here is the Apache virtual host documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.