Tutorial: Configuring Multiple Apache Subdomains

Avatar

By squashlabs, Last Updated: July 30, 2023

Tutorial: Configuring Multiple Apache Subdomains

Virtual Hosts in Apache

Virtual hosts in Apache allow you to host multiple websites on a single server. Each website can have its own domain or subdomain. Configuring virtual hosts involves creating separate configuration files for each website and specifying the document root, server name, and other settings.

To create a virtual host in Apache, follow these steps:

Step 1: Create a new configuration file for the virtual host in the Apache configuration directory. For example, create a file named example.com.conf in the directory /etc/httpd/conf.d/.

Step 2: Open the configuration file and add the following content:

    ServerName example.com
    DocumentRoot /var/www/example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

In this example, the virtual host is configured with the server name example.com and the document root /var/www/example.com. The block sets the options for accessing the files in the document root.

Step 3: Save the configuration file and restart Apache for the changes to take effect. On most Linux distributions, you can use the following command:

sudo systemctl restart apache2

Now, when you visit http://example.com in your web browser, Apache will serve the files from the document root specified in the virtual host configuration.

You can create multiple virtual hosts by following the same steps and using different server names and document roots for each virtual host.

Related Article: The Path to Speed: How to Release Software to Production All Day, Every Day (Intro)

Configuring DNS Records for Subdomains

To configure DNS records for subdomains, you need to have control over the DNS settings for your domain. This typically involves accessing the DNS management interface provided by your domain registrar or DNS hosting provider.

To configure DNS records for a subdomain, follow these steps:

Step 1: Log in to your DNS management interface.

Step 2: Create a new DNS record for the subdomain. The exact steps may vary depending on your DNS management interface, but the general process involves selecting the record type (e.g., A record or CNAME record), entering the subdomain name, and specifying the IP address or domain name of the server hosting the subdomain.

For example, to create an A record for the subdomain sub.example.com that points to the IP address 192.168.1.10, you would enter the following information:

Record Type: A

Name: sub

IP Address: 192.168.1.10

Step 3: Save the DNS record and wait for the changes to propagate. DNS changes can take some time to propagate across the internet, so it may take a few minutes or hours for the subdomain to become accessible.

Once the DNS records are configured, you can set up a virtual host in Apache to serve the subdomain.

Domain Mapping for Multiple Subdomains

Domain mapping allows you to associate multiple subdomains with different websites on a single server. This can be useful when you want to create separate websites for different departments or services within an organization.

To configure domain mapping for multiple subdomains in Apache, follow these steps:

Step 1: Create virtual host configuration files for each subdomain following the steps outlined in the "Virtual Hosts in Apache" section. For example, create sub1.example.com.conf and sub2.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 2: Open each configuration file and update the ServerName and DocumentRoot directives to match the subdomain and document root for each website.

For example, in sub1.example.com.conf, you would have:

    ServerName sub1.example.com
    DocumentRoot /var/www/sub1.example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

And in sub2.example.com.conf, you would have:

    ServerName sub2.example.com
    DocumentRoot /var/www/sub2.example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

Step 3: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit http://sub1.example.com and http://sub2.example.com, Apache will serve the files from the respective document roots specified in the virtual host configurations.

You can add as many subdomains as needed by creating additional virtual host configuration files and updating the DNS records accordingly.

Exploring Wildcard Subdomains in Apache

Wildcard subdomains allow you to dynamically map any subdomain of a domain to a specific directory or website. This can be useful when you want to create a flexible system for managing subdomains without having to configure each subdomain individually.

To enable wildcard subdomains in Apache, follow these steps:

Step 1: Create a virtual host configuration file for the wildcard subdomain. For example, create a file named wildcard.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 2: Open the configuration file and add the following content:

    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/%1

In this example, the ServerAlias directive is set to *.example.com, which matches any subdomain of example.com. The VirtualDocumentRoot directive specifies that the document root should be based on the subdomain name, with %1 representing the first part of the subdomain.

Step 3: Save the configuration file and restart Apache for the changes to take effect.

Now, any subdomain of example.com will be dynamically mapped to a directory based on the subdomain name. For example, if you visit http://sub1.example.com, Apache will serve the files from the directory /var/www/sub1.

Wildcard subdomains can be particularly useful when creating dynamic or multi-tenant applications where each user or account has its own subdomain.

Related Article: Intro to Security as Code

Securing Multiple Subdomains with SSL Certificates

Securing multiple subdomains with SSL certificates involves obtaining a wildcard SSL certificate that covers all subdomains of a domain. This allows you to secure all subdomains with a single certificate, simplifying the management and deployment of SSL certificates.

To secure multiple subdomains with an SSL certificate, follow these steps:

Step 1: Obtain a wildcard SSL certificate from a trusted certificate authority (CA). This typically involves generating a certificate signing request (CSR), submitting it to the CA, and completing any verification steps required by the CA.

Step 2: Once you have obtained the wildcard SSL certificate, install it on your web server. The exact process for installing SSL certificates can vary depending on your server and the software you are using. Consult the documentation for your server or SSL certificate provider for detailed instructions.

Step 3: Update the virtual host configuration for each subdomain to enable SSL/TLS connections. Add the following lines to each virtual host configuration file:

    ServerName sub.example.com
    DocumentRoot /var/www/sub.example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key

In this example, the SSLEngine directive is set to on to enable SSL/TLS connections. The SSLCertificateFile and SSLCertificateKeyFile directives specify the paths to the wildcard SSL certificate and the private key.

Step 4: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit https://sub.example.com, Apache will serve the files over an encrypted SSL/TLS connection using the wildcard SSL certificate.

Understanding Reverse Proxy with Multiple Subdomains

A reverse proxy is a server that sits between client devices and web servers, forwarding client requests to the appropriate backend server based on various criteria. When used with multiple subdomains, a reverse proxy can route requests to different subdomains to different backend servers or applications.

To set up a reverse proxy with multiple subdomains in Apache, follow these steps:

Step 1: Enable the necessary Apache modules for reverse proxy and proxying over SSL. Open a terminal and run the following commands:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod ssl
sudo systemctl restart apache2

Step 2: Create virtual host configuration files for each subdomain that you want to reverse proxy. For example, create sub1.example.com.conf and sub2.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 3: Open each configuration file and add the following content:

    ServerName sub1.example.com

    ProxyPass / http://backend1.example.com/
    ProxyPassReverse / http://backend1.example.com/



    ServerName sub2.example.com

    ProxyPass / http://backend2.example.com/
    ProxyPassReverse / http://backend2.example.com/

In this example, the ProxyPass and ProxyPassReverse directives are used to define the backend servers for each subdomain. Requests to sub1.example.com will be forwarded to http://backend1.example.com/, and requests to sub2.example.com will be forwarded to http://backend2.example.com/.

Step 4: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit http://sub1.example.com, Apache will reverse proxy the request to http://backend1.example.com/, and when you visit http://sub2.example.com, Apache will reverse proxy the request to http://backend2.example.com/.

Reverse proxying can be useful for load balancing, caching, or routing requests to different backend servers based on various criteria.

Load Balancing Traffic Across Subdomains

Load balancing traffic across subdomains involves distributing incoming requests to different backend servers or applications to ensure optimal performance and availability. This can be achieved using various load balancing techniques, such as round-robin, least connections, or session persistence.

To load balance traffic across subdomains in Apache, you can use the mod_proxy_balancer module. Follow these steps:

Step 1: Enable the necessary Apache modules for load balancing. Open a terminal and run the following commands:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo systemctl restart apache2

Step 2: Create a virtual host configuration file for the load balancer. For example, create a file named lb.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 3: Open the configuration file and add the following content:

    ServerName lb.example.com

    
        BalancerMember http://backend1.example.com/
        BalancerMember http://backend2.example.com/
        BalancerMember http://backend3.example.com/
    

    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/

In this example, the block defines the backend servers for the load balancer. Requests will be distributed across the backend servers specified in the BalancerMember directives.

Step 4: Save the configuration file and restart Apache for the changes to take effect.

Now, when you visit http://lb.example.com, Apache will load balance the requests across the backend servers http://backend1.example.com/, http://backend2.example.com/, and http://backend3.example.com/.

You can add or remove backend servers and adjust the load balancing algorithm as needed to achieve optimal performance for your specific use case.

Subdomain Routing in Apache

Subdomain routing involves directing incoming requests to different parts of a web application based on the subdomain. This can be useful when you want to serve different content or functionality for different subdomains.

To set up subdomain routing in Apache, follow these steps:

Step 1: Create virtual host configuration files for each subdomain that you want to route. For example, create sub1.example.com.conf and sub2.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 2: Open each configuration file and add the following content:

    ServerName sub1.example.com

    DocumentRoot /var/www/sub1.example.com
    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

    RewriteEngine On
    RewriteRule ^/(.*)$ /sub1/$1 [L]



    ServerName sub2.example.com

    DocumentRoot /var/www/sub2.example.com
    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

    RewriteEngine On
    RewriteRule ^/(.*)$ /sub2/$1 [L]

In this example, the RewriteRule directive is used to rewrite the URL for each subdomain. Requests to sub1.example.com will be rewritten to /sub1/ followed by the original path, and requests to sub2.example.com will be rewritten to /sub2/ followed by the original path.

Step 3: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit http://sub1.example.com/foo, Apache will serve the content from /var/www/sub1.example.com/foo, and when you visit http://sub2.example.com/bar, Apache will serve the content from /var/www/sub2.example.com/bar.

Subdomain routing can be used to create multi-tenant applications, where each subdomain represents a separate tenant with its own data and functionality.

Related Article: How to Design and Manage a Serverless Architecture

Configuring Virtual Hosts for Subdomains

To configure virtual hosts for subdomains in Apache, follow these steps:

Step 1: Create virtual host configuration files for each subdomain that you want to configure. For example, create sub1.example.com.conf and sub2.example.com.conf in the /etc/httpd/conf.d/ directory.

Step 2: Open each configuration file and add the following content:

    ServerName sub1.example.com
    DocumentRoot /var/www/sub1.example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    



    ServerName sub2.example.com
    DocumentRoot /var/www/sub2.example.com

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    

In this example, each subdomain is configured with its own ServerName and DocumentRoot. The block sets the options for accessing the files in the document root.

Step 3: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit http://sub1.example.com, Apache will serve the files from the document root specified for the subdomain sub1.example.com, and when you visit http://sub2.example.com, Apache will serve the files from the document root specified for the subdomain sub2.example.com.

You can create as many subdomains as needed by creating additional virtual host configuration files and updating the DNS records accordingly.

Mapping Multiple Domains to Subdomains

To map multiple domains to subdomains in Apache, follow these steps:

Step 1: Create virtual host configuration files for each domain that you want to map to a subdomain. For example, create example1.com.conf and example2.com.conf in the /etc/httpd/conf.d/ directory.

Step 2: Open each configuration file and add the following content:

    ServerName example1.com
    ServerAlias www.example1.com

    Redirect permanent / http://sub1.example.com/



    ServerName example2.com
    ServerAlias www.example2.com

    Redirect permanent / http://sub2.example.com/

In this example, each domain is configured with its own ServerName and ServerAlias. The Redirect directive is used to redirect requests to the corresponding subdomain.

Step 3: Save the configuration files and restart Apache for the changes to take effect.

Now, when you visit http://example1.com or http://www.example1.com, Apache will redirect the request to http://sub1.example.com, and when you visit http://example2.com or http://www.example2.com, Apache will redirect the request to http://sub2.example.com.

You can map multiple domains to subdomains by creating additional virtual host configuration files and specifying the appropriate redirects.

Additional Resources



- Configuring Multiple Subdomains in Apache

- Setting Up Wildcard Subdomains in Apache

- Redirecting a Subdomain to a Different URL in Apache

More Articles from the The DevOps Guide series:

Terraform Advanced Tips for AWS

Learn advanced tips to optimize your AWS infrastructure deployments with Terraform. Improve efficiency and simplify the process with topics like mana… read more

Ace Your DevOps Interview: Top 25 Questions and Answers

DevOps Interviews: Top 25 Questions and Answers is a guide to help you succeed in your DevOps interview. It covers commonly asked questions and provi… read more

Terraform Advanced Tips on Google Cloud

Learn advanced tips and techniques for using Terraform on Google Cloud. This article provides guidance on optimizing your infrastructure deployment, … read more

How to Manage and Optimize AWS EC2 Instances

Learn how to optimize your AWS EC2 instances with essential tips for cloud computing. Increase performance, reduce costs, and improve security. From … read more

Tutorial on Routing Multiple Subdomains in Nginx for DevOps

As software engineering continues to evolve, the complexity of web application deployment and testing has also increased. Traditional test environmen… read more

Quick and Easy Terraform Code Snippets

Managing infrastructure and deploying resources can be a daunting task, but with the help of Terraform code snippets, it can become quick and easy. T… read more

How to Install and Use Docker

The article provides a practical guide to installing and using Docker, a popular containerization platform. It highlights the advantages of Docker, s… read more

An Overview of DevOps Automation Tools

Reviewing and comparing different DevOps automation tools in use today, this article provides an overview of the key aspects of DevOps automation. It… read more

How to use AWS Lambda for Serverless Computing

AWS Lambda is a powerful tool for serverless computing, allowing you to build scalable and cost-effective applications without the need to manage ser… read more

How to Migrate a Monolith App to Microservices

Migrate your monolithic app to microservices for a simpler, more scalable system. Learn the benefits, real-world examples, and steps to breaking down… read more