Last updated on September 10th, 2024 at 05:51 am
Often website administrators need to host and manage several websites on a single NGINX instance. Virtual hosts allow you to host multiple websites and domains from single web server. It saves time and money to run multiple websites from a single machine instead of deploying separate servers for each domain. In fact, virtual hosts can also be used to run separate subdomains of a website. In this article, we will learn how to setup NGINX virtual hosts on CentOS.
How to Setup NGINX Virtual Hosts on CentOS
Here are the steps to setup NGINX virtual hosts on CentOS. Please make sure you have installed NGINX in CentOS before you proceed further.
In this article, we will create two virtual hosts for domains domain1.com and domain2.com
1. Create Directories
Each website will obviously have separate directory to store its files and data. Since /var/www is the root directory of NGINX, we will need to create separate sub directories in it, one for each domain. Open terminal and create 2 directories, one for each virtual host, at /var/www. We will place our website files in these 2 directories.
# sudo mkdir -p /var/www/domain1.com/html # sudo mkdir -p /var/www/domain2.com/html
Bonus Read : How to Enable CORS in NGINX
2. Change File Ownership
Every process needs a system user to be able to access required files and directories. It is important to allow the NGINX server to use a system user for this purpose. Run the following commands to change the ownership of these new directories to the present logged in user.
# sudo chown -R $USER:$USER /var/www/domain1.com/html # sudo chown -R $USER:$USER /var/www/domain2.com/html
3. Grant Required Permission
We also need to grant NGINX the required permission for accessing files in /var/www directory.
# sudo chmod -R 755 /var/www
4. Create Virtual Host Configuration files
Next, create 2 virtual host configuration files, one for each domain, at /etc/nginx/sites-available
First, create virtual host configuration file for domain1.com domain
# sudo vim /etc/nginx/sites-available/domain1.com.conf
The above command will open a new file in a text editor. Add the following basic configuration in this file.
server { listen 80; listen [::]:80; server_name domain1.com; root /var/www/domain1.com/html; index index.html; location / { try_files $uri $uri/ =404; } }
In the above commands we define a server block for domain1.com that listens to port 80 and serves content located at /var/www/domain1.com/html
Similarly, create virtual host configuration file for domain2.com domain
# sudo vim /etc/nginx/sites-available/domain2.com.conf
The above command will open a new file in a text editor. Add the following basic configuration in this file.
server { listen 80; listen [::]:80; server_name domain2.com; root /var/www/domain2.com/html; index index.html; location / { try_files $uri $uri/ =404; } }
In the above commands we define a server block for domain2.com that listens to port 80 and serves content located at /var/www/domain2.com/html
Bonus Read : How to Enable TLS1.3 in NGINX
5. Create HTML pages
Next, create sample index.html pages for both domain. They will act as the home pages for each domain. After complete configuration, when you to try to access these domains, you will see the following HTML pages. Create index.html file for domain1.com in a text editor
# sudo vim /var/www/domain1.com/html/index.html
Add the following HTML:
<html> <head> <title>Welcome to Domain1.com!</title> </head> <body> <h1>Success! The Domain1.com NGINX Virtual Host is working!</h1> </body> </html>
Similarly, create index.html file for domain2.com
# sudo vim /var/www/domain2.com/html/index.html
Add the following HTML:
<html> <head> <title>Welcome to Domain2.com!</title> </head> <body> <h1>Success! The Domain2.com NGINX Virtual Host is working!</h1> </body> </html>
Bonus Read : How to Fix 503 Service Temporarily Unavailable in NGINX
6. Enable Virtual Hosts
Enable virtual hosts by adding symbolic links between the files present at /etc/nginx/sites-available and /etc/nginx/sites-enabled.
# sudo ln -s /etc/nginx/sites-available/domain1.com.conf /etc/nginx/sites-enabled/domain1.com.conf # sudo ln -s /etc/nginx/sites-available/domain2.com.conf /etc/nginx/sites-enabled/domain2.com.conf
7. Restart/Reload NGINX Server
Restart/Reload NGINX server to apply changes.
# sudo nginx -s reload
OR
# sudo service nginx restart
8. Test Virtual Hosts
Open browser and enter http://domain1.com and http://domain2.com one by one. You will see the index pages of both domains respectively.
Conclusion
In this article, we have learnt how to setup virtual hosts for NGINX server. You can use these steps to configure virtual hosts for multiple domains and subdomains on a single server. As much as possible, try to run multiple sites on a single server since they are easy to maintain and cost effective. Hopefully, now you can easily setup NGINX virtual hosts on Ubuntu.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.