Create Subdomain Nginx Debian 9 Server


Create Subdomain Nginx Debian 9 Server

Understanding Subdomain

A subdomain is a subsection of a domain that a user can create to host one or more websites, usually for a specific purpose. For example, if your domain is example.com, you can create a subdomain called images.example.com or blog.example.com. You can then create separate website to host on each subdomain.

While setting up a subdomain, you need to configure DNS records and web server settings, so that the user can access the website. In this article, we will discuss how to create a subdomain in a Nginx web server on Debian 9.

Prerequisites

To complete this tutorial, you will need the following:

  • A Debian 9 server with Nginx installed
  • A domain name (e.g. example.com)
  • Root access to the server

Step 1 – Configure DNS Records

Before you can set up a subdomain, you need to configure the DNS records to point to the IP address of your server. To do this, log in to your domain registrar and add an A record for the subdomain (e.g. blog), with the IP address of your server.

Step 2 – Create Nginx Virtual Host for Subdomain

Once you have configured the DNS records, you need to create a new virtual host. To do this, open the Nginx configuration file for your domain and create a new server block.

In the server block, add the following directives:

  • server_name: This should be the full domain name of the subdomain (e.g. blog.example.com)
  • Root: This should be the root directory where you want to store the files for the subdomain (e.g. /var/www/blog)

When you have finished configuring the server block, save and close the file. Then, create the root directory for the subdomain and give it the appropriate permissions.

Now, you need to restart Nginx for the changes to take effect. To do this, run the following command:


sudo systemctl restart nginx

Step 3 – Create a Website for the Subdomain

Once Nginx has been restarted, you can create a website for the subdomain. To do this, create an index.html file in the root directory for the subdomain (e.g. /var/www/blog) and add some basic HTML code. You can then test the website by opening it in a web browser.

Conclusion

In this article, we discussed how to create a subdomain in a Nginx web server on Debian 9. We showed how to configure the DNS records and Nginx virtual host for the subdomain, and how to create a website for the subdomain. We hope you now have a better understanding of how to create subdomains in Nginx on Debian 9.

FAQs

Q1: How do I set up a subdomain?

A1: To set up a subdomain, you need to configure the DNS records to point to your server, then create a virtual host for the subdomain in Nginx, and finally create a website for the subdomain.

Q2: How do I add a subdomain in Nginx?

A2: To add a subdomain in Nginx, you need to create a virtual host for the subdomain in the Nginx configuration file and restart Nginx for the changes to take effect.

Q3: How do I restart Nginx?

A3: To restart Nginx, you can run the following command: sudo systemctl restart nginx.

Thank you for reading this article. Please read other articles for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *