How to set up multiple domains and subdomains in Apache

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Hosting multiple domains and subdomains on a single server is an efficient way to manage web projects, and the Apache web server provides a robust platform to accomplish this task. This tutorial will guide you through the process of configuring Apache to serve multiple domains and subdomains on a single server, leveraging the power of virtual hosts.

Prerequisites

  • A server running Apache web server on Ubuntu 20.04, 21.04, 22.04, or 23.04
  • Access to the server with root privileges
  • Domain names pointed to your server’s IP address
  • Text editor to edit Apache’s configuration files

Step-by-Step Instructions

Step 1: Create Directory Structure

We will begin by creating a directory structure within /var/www/ for each domain and subdomain to hold the web documents.

sudo mkdir -p /var/www/domain.com/public_html

sudo mkdir -p /var/www/sub.domain.com/public_html

Set the correct permissions for each directory:

sudo chown -R $USER:$USER /var/www/domain.com/public_html

sudo chown -R $USER:$USER /var/www/sub.domain.com/public_html

Replace domain.com and sub.domain.com with your actual domain and subdomain names.

Step 2: Create Test Pages

To test our virtual hosts, let’s create a simple HTML file in each directory:

echo "<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to domain.com!</title>\n</head>\n<body>\n<h1>Success: domain.com is working!</h1>\n</body>\n</html>" > /var/www/domain.com/public_html/index.html

echo "<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to sub.domain.com!</title>\n</head>\n<body>\n<h1>Success: sub.domain.com is working!</h1>\n</body>\n</html>" > /var/www/sub.domain.com/public_html/index.html

Step 3: Create Apache Virtual Host Files

Each domain and subdomain will have its own virtual host file. To create these files:

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

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sub.domain.com.conf

Edit the virtual host file for domain.com using your preferred text editor:

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

In the domain.com.conf file, modify the <VirtualHost *:80> block to look like this:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Ensure you also edit sub.domain.com.conf with the subdomain’s specific information:

sudo nano /etc/apache2/sites-available/sub.domain.com.conf

In the sub.domain.com.conf file, adjust the virtual host settings accordingly:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName sub.domain.com
DocumentRoot /var/www/sub.domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable the Virtual Host Files

After configuring the virtual hosts, enable them:

sudo a2ensite domain.com.conf

sudo a2ensite sub.domain.com.conf

Then disable the default site:

sudo a2dissite 000-default.conf

Reload Apache to apply the changes:

sudo systemctl reload apache2

Step 5: Edit Hosts File (Optional)

If you haven’t pointed your domains to your server’s IP address, you can modify your local machine’s /etc/hosts file to test the virtual hosts locally:

sudo nano /etc/hosts

# Add the following lines:
server_ip_address domain.com
server_ip_address sub.domain.com

Replace server_ip_address with your server’s actual IP.

Step 6: Testing the Results

Open a web browser and navigate to http://domain.com and http://sub.domain.com. You should see the test pages created earlier, indicating that Apache is correctly serving the content for your domains and subdomains.

Conclusion

Setting up multiple domains and subdomains on Apache allows scalability and organization for your web services. Always remember to secure your domains with SSL certificates and configure backups to protect your data.