Setup Vhost Nginx Debian 9.3


Setup Vhost Nginx Debian 9.3

Introduction

This article will provide step-by-step instructions to setup Nginx Virtual Hosts (vhosts) on a Debian 9.3 system. Nginx is a web server software used to host web applications and static content such as images, JavaScript files, and other resources. With virtual hosts, you can serve multiple websites on a single VPS server. Setup Vhost Nginx Debian 9.3, will provide an easy way to serve multiple applications on a single server, allowing you to optimize performance.

Prerequisites

  • A Debian 9.3 server with root privileges.
  • A sudo user with root privileges.
  • Nginx installed and running on your system.
  • A domain name with DNS records pointed to your server.

Step 1: Setup Directory Structure for Vhosts

The first step is to create a directory structure to store your virtual hosts. We will create a ‘vhosts’ directory within the root user’s home directory. This directory will contain all the subdirectories for each virtual host.

Use the following command to create a ‘vhosts’ directory within root user’s home directory:

mkdir -p ~/vhosts && cd ~/vhosts

Next, create a directory for each website you want to host. For our example, we will create a directory in the ‘vhosts’ directory for domain1.com

mkdir domain1.com

These directories should be owned by root and therefore should remain protected. Therefore, you should strictly limit access to these directories. This can be done using the following command:

chmod -R 700 domain1.com

Step 2: Configure Nginx for Virtual Hosting

You must now configure Nginx to serve the virtual hosts. To do this, you must add a server block to the Nginx configuration. This will define how Nginx treats each virtual host.

First, create a configuration file for your domain. For our example, we will call it domain1.conf and it will be stored in the /etc/nginx/sites-enabled directory:

touch /etc/nginx/sites-enabled/domain1.conf

Next, open the configuration file using your text editor:

nano /etc/nginx/sites-enabled/domain1.conf

Now, add the following lines to the configuration file, replacing domain1.com with your domain name:

server {
listen 80;
listen [::]:80;

server_name domain1.com www.domain1.com;

root /home/vhosts/domain1.com;
index index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ =404;
}
}

Save and close the file. Now, you must check the syntax of your configuration file. This can be done using the following command:

nginx -t

If the syntax check is successful, restart Nginx for the changes to take effect. Use the following command to restart Nginx:

systemctl restart nginx

Step 3: Copy Web Content to the Virtual Hosts

Now, you can copy the web content to each virtual host directory. For our example, we will copy the web content to the domain1.com directory. Use rsync command to copy the web content:

rsync -av /path/to/source/ /home/vhosts/domain1.com

Replace /path/to/source with the path of the directory containing your web content. This command will copy all the files and directories in this directory to the domain1.com directory.

Step 4: Configure DNS Records

The last step is to configure the DNS records for your domain. You must add an A record for your domain pointing to the IP address of your server. You may also need to add other records such as CNAME, MX and TXT records.

After you have configured your DNS records, you can use the ‘nslookup’ command to check that your domain name is pointing to correct IP address. Use the following command:

nslookup domain1.com

If the returned IP address matches the IP address of your server, you have successfully configured your DNS records.

Conclusion

In this article, we have shown how to setup virtual hosts for Nginx on a Debian 9.3 server. We have discussed the prerequisites, configured Nginx to serve the virtual hosts, copied the web content to the virtual hosts directory, and configured the DNS records for the domain. We hope you have found this article helpful.

Thank you for reading this article!

FAQs

Q. How Do I Configure Nginx For Virtual Hosts?

A. To configure Nginx for virtual hosts, you must create a configuration file for each website you want to host. Each configuration file should define how Nginx should treat the website. You can also specify the root directory for the website. After you have configured the virtual hosts, you must check the syntax of your configuration file and restart Nginx for the changes to take effect.

Q. How Do I Copy Web Content To The Virtual Hosts?

A. To copy web content to the virtual hosts, you can use the ‘rsync’ command. This command will copy the content from the source directory to the virtual hosts directory. Make sure that the ownership of the files and directories is set to the root user of your server.

Q. How Do I Configure DNS Records for My Domain?

A. To configure DNS records, you must add an A record for your domain pointing to the IP address of your server. The A record should be configured with the correct IP address. You can then use the ‘nslookup’ command to check if your DNS records are correctly configured.

Leave a Reply

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