How to Create and Configure Nginx Virtual Hosts on Ubuntu

0

Have you ever wondered how to host multiple websites on the same server without using virtual machines or complicated setups? Nginx virtual hosts are what you are looking for.

This guide will explain how to set up a virtual web host on Ubuntu using the Nginx web server. Nginx is a high performance web and reverse proxy server. It is lightweight, cross-platform, and open source.

What is a virtual host?

A virtual web host is a method of running or hosting multiple websites with different domain names on a single physical server or virtual machine.

Virtual hosting is widely used by web hosting companies in order to achieve economies of scale and meet the needs of multiple clients without spending a lot on resources or dedicated server hardware.

If you’ve used shared hosting before, it’s most likely a virtual host that’s at stake behind the scenes.

Step 1: Installing the Nginx server

If you haven’t installed Nginx, here’s how to quickly install it on Ubuntu using APT.

First, update your package information against the configured sources:

sudo apt update

Then install Nginx as follows:

sudo apt install nginx

Test Nginx

Start the Nginx service using the systemctl command.

sudo systemctl start nginx

In your web browser, go to http: // localhost: 80 to confirm if Nginx was installed successfully. If so, your browser will display a page similar to the one below.


nginx default website

Step 2: Create and configure the website

By default, the website served by Nginx runs on port 80 and is stored in the / var / www / html phone book.

VIDEO OF THE DAY MAKEUSE

To configure a virtual host, it is recommended that you place each separate website in a different directory, for better security and management.

Create a directory under the / var / www / phone book. You can name it VirtualHost but feel free to use any meaningful name you choose. To do this, go to / var / www directory using the cd command.

cd /var/www

Then create the website directory as follows:

mkdir -p VirtualHost

Created a index.html file in the directory using the following commands:

cd /VirtualHost 
touch index.html

Open the index.html with your favorite text editor and add the following lines of code to it:





Nginx: Web & Reverse proxy server


Welcome to Nginx


I've just configured a virtual host using Nginx web server on Linux



Save and close the file.

Learn more: How to create new files on Linux using touch

Step 3: Configure the Virtual Host

You can find the Nginx configuration files in the / etc / nginx phone book. To configure the virtual host, first create a virtual host configuration file for the site in the / etc / nginx / sites-enabled phone book.

cd /etc/nginx/sites-enabled

We named the file virtual_host but feel free to use any meaningful name you choose.

touch virtual_host

Open the file you just created, i.e. virtual_host, using your favorite text editor and paste the following lines of code into it:

server {
listen 81;
listen [::]:81;
server_name my.virtualhost.com;
root /var/www/VirtualHost;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}

Save and close the file.

Here are some of the important configurations in the file explained:

  • Listen: specifies that Nginx should serve the website at port 81 i.e. https: // localhost: 81.
  • Server name: You can give it any name since you are not using a real domain at this stage. I named mine my.virtualhost.com.
  • root: This is the location of the website. In this case, the / var / www / VirtualHost phone book.
  • index: specifies the website start page, which is index.html.

Step 4: Serve the website

Restart the Nginx server to save the changes you made.

sudo systemctl restart nginx

You can check the status of the Nginx server by running:

sudo systemctl status nginx

If everything looks correct, navigate to the URL http: // localhost: 81, in your web browser.


nginx server serving a website

You now have two websites on your server, one running on port 81 and one running on port 80.

Create and host your first website on Linux

This guide has shown you how to host multiple websites on Ubuntu using Nginx web server. Website development is one of the most in-demand engineering skills these days, so start your web development journey with PHP today.


simple-php-site
How To Build Your First Simple PHP Website

Want to build a website but don’t know where to start? Building a basic PHP website will put you on the path to web development.

Read more


About the Author


Source link

Share.

Comments are closed.