Published on August 21, 2024By DeveloperBreeze

Tutorial: Adding a Subdomain on an Apache Server

Introduction

Subdomains are a powerful way to organize and manage different sections of a website. They allow you to create separate sections for various purposes, such as a blog, store, or support site, all under the same main domain. For example, if your main domain is example.com, you can create a subdomain like blog.example.com or shop.example.com.

This tutorial will guide you through the process of adding a subdomain on an Apache server. We will cover the necessary steps, including setting up DNS records, configuring Apache, and securing your subdomain with SSL.

Prerequisites

Before you begin, ensure you have the following:

  • A domain name registered with a DNS provider.

  • Access to a server running Apache (either a VPS or a dedicated server).

  • Administrative (root) access to the server.

  • Basic knowledge of SSH and command-line operations.

  • Apache installed and running on your server.

Step 1: Create a DNS Record for the Subdomain

The first step in setting up a subdomain is to create a DNS record that points to your server’s IP address. This tells the internet where to find your subdomain.

    • Log in to your DNS provider’s control panel.

    • Locate the DNS management section for your domain.

    • Create an A record for the subdomain:

- Name: Enter the subdomain name (e.g., blog for blog.example.com).

- Type: Select A.

- Value: Enter the IP address of your Apache server.

- TTL: You can leave this as the default value.

After adding the DNS record, it may take some time for the changes to propagate. You can check if the subdomain is pointing correctly by using tools like nslookup or dig.

Step 2: Set Up a Directory for the Subdomain

Next, you need to create a directory on your server where the files for your subdomain will reside.

    • SSH into your server:

ssh user@your-server-ip
   

    • Navigate to the web root directory (commonly /var/www):

cd /var/www
   

    • Create a new directory for the subdomain:

sudo mkdir -p /var/www/blog.example.com
   

    • Set the appropriate permissions:

sudo chown -R $USER:$USER /var/www/blog.example.com
   sudo chmod -R 755 /var/www
   

Step 3: Configure Apache for the Subdomain

Now that the directory is set up, you need to configure Apache to serve content from that directory when the subdomain is accessed.

    • Create a new virtual host file for the subdomain:

sudo nano /etc/apache2/sites-available/blog.example.com.conf
   

    • Add the following configuration to the file:

<VirtualHost *:80>
       ServerAdmin admin@example.com
       ServerName blog.example.com
       DocumentRoot /var/www/blog.example.com

       <Directory /var/www/blog.example.com>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/blog.example.com-error.log
       CustomLog ${APACHE_LOG_DIR}/blog.example.com-access.log combined
   </VirtualHost>
   

- ServerAdmin: Your email address or the administrator’s email address.

- ServerName: The subdomain you are setting up (blog.example.com).

- DocumentRoot: The directory where your subdomain’s files are located.

    • Enable the new virtual host:

sudo a2ensite blog.example.com.conf
   

    • Test the Apache configuration for syntax errors:

sudo apachectl configtest
   

    • Reload Apache to apply the changes:

sudo systemctl reload apache2
   

Step 4: Create an Index Page for the Subdomain

To verify that everything is working correctly, you can create a simple index.html page in the subdomain’s directory.

    • Create the index file:

nano /var/www/blog.example.com/index.html
   

    • Add some basic HTML:

<!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Welcome to Blog Subdomain</title>
   </head>
   <body>
       <h1>Hello, World!</h1>
       <p>This is the blog subdomain.</p>
   </body>
   </html>
   

    • Save and exit the file.

Step 5: (Optional) Secure Your Subdomain with SSL

If you want your subdomain to be served over HTTPS, you need to install an SSL certificate. You can obtain a free SSL certificate from Let’s Encrypt.

    • Install Certbot (if not already installed):

sudo apt-get install certbot python3-certbot-apache
   

    • Obtain and install the SSL certificate:

sudo certbot --apache -d blog.example.com
   

    • Follow the prompts to complete the installation. Certbot will automatically configure your Apache virtual host to use SSL.

    • Test the SSL setup by visiting https://blog.example.com in your browser.

Step 6: Test the Subdomain

Finally, open a web browser and navigate to your subdomain (e.g., http://blog.example.com). You should see the "Hello, World!" message from the index.html file you created.

Conclusion

You have successfully set up a subdomain on your Apache server. This setup allows you to host different sections of your website under various subdomains, providing better organization and management. If needed, you can repeat these steps to add more subdomains to your server.

Remember to keep your Apache configuration files organized and secure. Regularly monitor your server logs and update your SSL certificates to maintain a secure environment for your website visitors.

Comments

Please log in to leave a comment.

Continue Reading: