Home → Articles → How to Host Multiple Websites with Apache on Ubuntu 24.04

How to Host Multiple Websites with Apache on Ubuntu 24.04

Introduction

Apache virtual hosts allow hosting multiple websites on a single server by creating individual configuration files for each site and pointing them to specific directories. This setup saves costs by eliminating the need for separate servers, efficiently utilizes server resources, and simplifies management tasks. By centralizing updates, file management, and performance monitoring, Apache Virtual Hosts provide a cost-effective, efficient, and manageable solution for running multiple websites.

This guide shows you how to host multiple websites with Apache on Ubuntu 24.04.

Prerequisites

Set Up Directory Structure for Websites

For each website you intend to host on your Apache web server, you should create the correct directories and set up the appropriate permissions. This guide intends to run the following websites:

Follow the steps below:

  1. Create three directories in the root directory of your webserver.

    console
    $ sudo mkdir -p /var/www/example.com/public_html
    $ sudo mkdir -p /var/www/example.info/public_html
    $ sudo mkdir -p /var/www/example.net/public_html
    
  2. Take ownership of the new directories.

    console
    $ sudo chown -R $USER:$USER /var/www/example.com/public_html
    $ sudo chown -R $USER:$USER /var/www/example.info/public_html
    $ sudo chown -R $USER:$USER /var/www/example.net/public_html
    
  3. Set the appropriate permissions for the directories.

    console
    $ sudo chmod -R 755 /var/www/example.com/public_html
    $ sudo chmod -R 755 /var/www/example.info/public_html
    $ sudo chmod -R 755 /var/www/example.net/public_html
    
  4. Ensure any files or new directories that you create inside the above base directories inherit the permissions.

    console
    $ sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \;
    $ sudo find /var/www/example.info/public_html -type d -exec chmod g+s {} \;
    $ sudo find /var/www/example.net/public_html -type d -exec chmod g+s {} \;
    

Set Up Virtual Host Files for Websites

For each website that you intend to run, you must set up a virtual host file.

Create Sample Home Pages for Websites

Create a sample home page for each website you intend to run with the Apache server.

Edit your Local Computer Hosts File

On your local computer, add the example.com, example.net, and example.info sample domain names to your hosts file and point the domain names to your server's public IP address.

If you are running Windows, follow the steps below.

  1. Edit the c:\Windows\System32\Drivers\etc\hosts file and remember to replace 192.88.99.1 with the public IP address associated with your Ubuntu server.

    INI
    # Copyright (c) 1993-2009 Microsoft Corp.
    ...
    
    # localhost name resolution is handled within DNS itself.
    #             127.0.0.1       localhost
    #             ::1             localhost
    192.88.99.1 example.com
    192.88.99.1 example.info
    192.88.99.1 example.net
    ...
    
  2. Save the file and visit each domain name on your browsers, you should see different web content for each domain.

    • example.com:

      http://example.com
      
    • example.info:

      http://example.info
      
    • example.net:

      http://example.net
      

Conclusion

In this guide, you've configured multiple websites using Apache Virtual Hosts. You have set up different domain names, created separate configuration files for each site, and pointed them to their respective directories. To further enhance these websites, consider buying a domain name from a domain registrar and point the domain names A records to your server's public IP address.