Signup/Sign In
PUBLISHED ON: FEBRUARY 27, 2023

How to add multiple subdomains to an Apache server?

Adding a subdomain to your Apache2 server allows you to host multiple websites on a single server, each with its own unique domain name. This can be useful for organizing and managing multiple websites, as well as for creating a more professional and polished online presence.

In this tutorial, we will show you how to configure your Apache2 server to support multiple subdomains, using a simple and straightforward process.

Step 1: Create a new directory for your subdomain

First, you need to create a new directory for your subdomain on your server. This will be the location where your subdomain's website files will be stored. To do this, use the following command:

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

This command creates a new directory called subdomain.domain.com in the /var/www directory on your server. The public_html directory within this new directory will be the root directory for your subdomain's website files.

Step 2: Set the correct permissions for the new directory

Next, you need to set the correct permissions for your new directory, so that your subdomain's website files can be accessed by the Apache2 server. To do this, use the following command:

chown -R www-data:www-data /var/www/subdomain.domain.com/public_html

This command sets the owner of the new directory to the "www-data" user, which is the user that the Apache2 server runs under. This will allow the Apache2 server to read and write to your subdomain's website files.

Step 3: Create a new virtual host configuration file

In order to configure your Apache2 server to support your new subdomain, you need to create a new virtual host configuration file. This file will contain the settings and directives that the Apache2 server uses to serve your subdomain's website.

To create a new virtual host configuration file, use the following command:

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

This command will open the "nano" text editor, allowing you to create and edit the new virtual host configuration file.

Step 4: Edit the virtual host configuration file

In the "nano" text editor, add the following lines to your virtual host configuration file:

<VirtualHost *:80>
    ServerName subdomain.domain.com
    ServerAlias www.subdomain.domain.com
    ServerAdmin admin@domain.com
    DocumentRoot /var/www/subdomain.domain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

These lines specify the settings and directives for your subdomain's virtual host. Make sure to replace "subdomain.domain.com" with the actual domain name of your subdomain, and "admin@domain.com" with the email address of the administrator for your subdomain.

Step 5: Enable the new virtual host

Once you have saved your virtual host configuration file, you need to enable it so that it will be used by the Apache2 server. To do this, use the following command:

a2ensite subdomain.domain.com.conf

This command enables the new virtual host configuration file, allowing it to be used by the Apache2 server.

Step 6: Restart the Apache2 server

After enabling your new virtual host configuration file, you need to restart the Apache2 server in order for the changes to take effect. To do this, use the following command:

service apache2 restart

This command will restart the Apache2 server, allowing it to use the new virtual host configuration file for your subdomain.

Conclusion:

In this tutorial, we showed you how to configure your Apache2 server to support multiple subdomains. By creating a new directory for your subdomain, setting the correct permissions, creating a new virtual host configuration file, and restarting the Apache2 server, you can easily add multiple subdomains to your server. This can help you to organize and manage multiple websites, and create a more professional and polished online presence.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.