Sling Academy
Home/DevOps/How to set up multiple domains and subdomains in Apache

How to set up multiple domains and subdomains in Apache

Last updated: January 20, 2024

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.

Next Article: Apache Error: PHP files downloaded instead of executed

Previous Article: Apache Error: PHP code not working but plain text is displayed

Series: Apache Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide