How to Install Multiple Domains in Nginx

How to Install Multiple Domains in Nginx

If you are looking for a guide on how to install multiple domains in nginx, here’s everything you need to know. 

In nginx, you can host multiple domains with the help of server blocks. You can choose VPS as a hosting platform, so all your websites can stay on a single server. We will see how to install multiple domains in nginx in this article. We start with a brief discussion on nginx

Then, we will provide the basic steps to configure multiple domains in nginx.

What is nginx?

Nginx is one of the most popular open-source web servers. It is not only a web server, but it is also used as a reverse proxy, load balancer, mail proxy, and HTTP cache. It is even used in media streaming, caching, and reverse proxy features. 

It also provides a high-speed and dependable solution to its user. It can support multiple domains with the help of server blocks.

Install multiple domains in nginx

Now, we will discuss how to install multiple domains in nginx. The basic steps to configure multiple domains are as follows: 

  • Installation of nginx.
  • Enable firewall.
  • Change the config file.
  • Create server blocks.
  • Create folders to host websites.
  • Upload website.
  • Restart the nginx server.

Install nginx

First, update the Linux repository cache with the help of the following command:

$sudo apt update

Then, install nginx server by executing the following command on your Terminal:

$sudo apt install -y nginx


Finally, you may now verify if you’ve installed nginx with the help of the following command:

$nginx -v


You may see the following output:

nginx version: nginx/1.10.3 (Ubuntu)


Configuration of firewall

Once you’ve installed nginx, you need to configure the firewall. Run the following commands on the Terminal:

$ sudo ufw allow ‘Nginx HTTPS’
$ sudo ufw enable


You will see the following output:

nginx https

Change the nginx config file

Let us create two .conf files for the sites we want to host. Note that by default, the configuration file for nginx is /etc/nginx/nginx.conf. You can then add all the domains to this configuration. However, it is not recommended, which will make the configuration file clearer. 

Furthermore, you’re advised to create an individual configuration file for each domain. Browse the nginx root folder and search for nginx.conf file. Now, make sure that the conf file has the following entry:

include /etc/nginx/conf.d/*.conf;


The previous line implies that nginx looks for the configuration file in a certain location on startup.

Create server blocks to define multiple domains

The following are the two domains that we want to host: example1.com and example2.com. Using your favorite editor, open example1.com.conf in the nginx root folder as follows:

$sudo vi /etc/nginx/conf.d/example1.com.conf


In this example, the root folder is /etc/nginx/conf.d/. Now, enter the following information in the file:

server {
listen 80 default_server;
listen [::]:80 default_server;  
root /var/www/example1.com;  
index index.html;  
server_name example1.com www.example1.com; 
location / { try_files $uri $uri/ =404;}
}


Here, we have specified the root of the first website as /var/www/example1.com. The server will listen on port 80. We have also set the index file as index.html. So, this file will show up when a request comes for the root folder. The server name we have provided is example1.com. 

Now, create another file example2.com.conf, in the nginx root folder containing the following information:

server {
listen 80;
listen [::]:80;  
root /var/www/example2.com;  
index index.html;  
server_name example2.com www.example2.com; 
location / { try_files $uri $uri/ =404;}
}


In the above file, we have specified the root of the second website as /var/www/example2.com.

Hosting website by creating folders

As the next step, we will create two folders example1.com and example2.com, for both websites. To create the first folder, use the following command:

$sudo mkdir -p /var/www/example1.com


Note: Here, we have used the mkdir command to create the directory. ‘mkdir’ stands for make directory, and -p specifies the parent directory.

Next, use the following command to create a second folder:

$sudo mkdir -p /var/www/example2.com


You might need to give ownership of the directory. For this purpose, you should use the chown command. With it, you can change the owner who owns the file. The following command shows how you can change the ownership:

$ sudo  chown -R $USER:USER / var/www/example1.com/
$ sudo  chown -R $USER:USER / var/www/example2.com/


Now, log in with the user that owns the file. Finally, change the permission of the file with the help of the following commands:

$ sudo  chmod -R 755 / var/www/example1.com/
$ sudo  chmod -R 755 / var/www/example2.com/


Using the chmod command with 755, we have given all the permissions to the user for this directory.

Upload the website to the folder

We will place the website’s static contents in the respective folder. Navigate to the following folders and copy the contents:

/var/www/example1.com Place the example1.com static files in this folder

/var/www/example2.com Place the example2.com static files in this folder

Restart nginx server

In the final step, we will restart the nginx server. For this purpose, type the following command on the Terminal:

$sudo systemctl restart nginx


In the final step, you can browse the website on your browser. You should see the index page of your domain.

In this article, we have discussed how to host and install multiple domains in nginx. Using nginx, we can host numerous domains, and in it, the server block is already enabled by default. And lastly, for multiple domains, we need to create more directories. 

And we’ve reached the end of this write-up. If you have any questions, feel free to leave a comment down below.

If this guide helped you, please share it. 

Related Posts