Your Cart
Loading
How To Host Multiple Websites With NGINX

How To Host Multiple Websites With NGINX

In this tutorial, you will learn how to set up multiple websites on a single server using NGINX. NGINX is a web server designed to handle a high number of connections simultaneously.


Due to its superb ability to handle a lot of connections and speed, many high-traffic websites have been using NGINX’s service.

Some of these online giants are Google, Netflix, Adobe, Cloudflare, WordPress.com, and many more.

Install Nginx on Ubuntu


You can install NGINX from the package manager using the following command.


sudo apt install nginx


Once done with that change directory to /var/www/html. This is the location where you need to add your website files. I already have a website in this location.



If you want to host another site on the same server, you need to create a new folder for the second site using the following command.


mkdir test


Test is the folder name, and change the directory to the folder using ‘cd test’ in the terminal.



Here create a basic HTML page using nano text editor.


nano index.html


Add the following line.


<h1>This is my second website!</h1>


Save and close the file.


Create the Configuration File

Next, you will need to create a host configuration file for the second website by typing.


nano /etc/nginx/sites-available/server.conf


Add the following lines:


server {
listen 80;
server_name test.com;

location / {
root /var/www/html/test;
}
}


This configuration listens for requests on port 80 for the domain name test.com. It then defines the server’s root directory. The root directory is where NGINX looks for the website files.

Next, enable the configuration file with the following command.


ln -s /etc/nginx/sites-available/server.conf /etc/nginx/sites-enabled/


You can check Nginx for any syntax error with the following command:


nginx –t



If everything goes fine, you should get the following output. At this point, you need to restart NGINX for the changes to take effect.


systemctl restart nginx


Test Your Websites

Open your web browser and type the URL ‘test.com’ in the address bar.



You can see the second website is working fine.

Now in the NGINX location (/var/www/html) I have my first website (webtest.com).

So, in the browser, I will type ‘webtest.com’ to test my first site.




And you can see both websites are running on the same NGINX web server.


Conclusion

Multiple websites can be hosted on NGINX using server blocks. If you choose a private server (VPS) as your hosting platform, all your websites can reside on a single server, giving you the flexibility to take control of things yourself.


There is no limit to the number of sites that you can host on your server with NGINX. But, make sure that your server has enough disk space, CPU, and RAM.