Hi! Today I come to teach you to configure Nginx server to create multiple domains and subdomains in it. This tutorial comes from a question in a comment. Imagine that you have a server on which you want to host multiple sites and projects. Each page will have your domain and, of course, we will not hire a server for each domain. The objective is to configure what is known as virtual machine hosts. The truth is that it is really easy to do this. In Geeky Blogs we have implemented since we have several pages hosted on the same server.
The method for adding a domain is the same as for a subdomain, but the latter have to make a simple change: where to put "dominio.com" put "subdominio.dominio.com".
STEP 1: INSTALL NGINX
Of course, we have installed Nginx. You can follow this tutorial series on setting up a VPS server.
STEP 2: CREATE A NEW DIRECTORY
The second step in creating a virtual host is add a folder to our system. If we add a dominio.com, do the following:
|
$ sudo mkdir -p /var/www/dominio.com/public_html
STEP 3: PERMITS
|
It is necessary and very important to give the correct permissions to the user and group. Being a Web application, give permissions to www-data:
|
$ sudo chown -R www-data:www-data /var/www/dominio.com/public_html
|
|
$ sudo chown -R www-data:www-data /var/www/dominio.com/public_html
|
STEP 4: ADD CONTENT
After creating the folder, we need to add the content that will be displayed when the user navigates in the domain. Therefore, we will create a simple index.html file to see if it all works:
|
$ sudo nano /var/www/dominio.com/public_html/index.html
|
Now add any HTML code.
STEP 5: CREATING THE VIRTUAL HOST NGINX
Here we will tell Nginx is going to have a new virtual host to manage, ie when the user enters in the dominio.com browser, the server will need to read the configuration file to create for what to do. In Nginx there are two major routes. The first is sites-available, which contains the configuration files of virtual hosts available on the server. The other is sites-enabled, which contains configuration files enabled sites, ie those operating at the time. We will create a file for dominio.com:
|
$ sudo nano /etc/nginx/sites-available/dominio.com
|
Enter the following content:
server {
## Escucha en el puerto 80 (HTTP)
listen 80;
## Raíz donde se encuentra la página Web
root /var/www/dominio.com/public_html;
## Orden de prioridad de los archivos index
index index.html index.htm;
server_name dominio.com;
}
Despite having created this file, we still can not access our website, because the configuration is in sites-available, but not in sites-enabled. To enable virtual host will create a symbolic link between the two folders:
|
$ sudo ln -s /etc/nginx/sites-available/dominio.com /etc/nginx/sites-enabled/dominio.com
|
STEP 6: RESET NGINX
Unless restart Nginx, the changes will not apply:
|
$ sudo service nginx restart
|
STEP 7: SETTING THE DNS
Finally we must configure the DNS records. As my server is hosted in DigitalOcean, I will use your panel. To add a subdomain, create a CNAME record with the subdomain name and point to the machine. Something like: