How to install Virtual host on Nginx Centos 8

What is Nginx ?

Nginx (Engine X) is a most popular, powerful and high-performance open-source HTTP web server and reverse proxy server with a scalable event-driven (asynchronous) architecture. It can also be used as a load balancer, mail proxy, and HTTP cache due to its speed, stability, feature-rich set, easy configuration, and low resource utilization.

How To Install Nginx on Centos 8 ?

dnf install nginx

Run and enable nginx service

systemctl start nginx
systemctl enable nginx
systemctl status nginx

From the output, the ‘active‘ status indicates that the Apache webserver is up and running.

Setting Up Nginx Virtual Hosts:

  1. Create an Nginx Document Root Directory

A virtual host is a separate file that contains configurations that allow you to set up a separate domain from the default one. For this guide, we will set up a virtual host for the domain

sudo mkdir -p /var/www/

Next, edit the file permissions use the $USER environment variable as shown.

sudo chown -R $USER:$USER /var/www/

Next, assign the right directory permissions recursively as shown:

sudo chmod -R 755 /var/www/

2. Create a Sample Page for the Domain

sudo vim  /var/www/

Inside the file, paste the following sample content.

    <title>Welcome to!</title>
    <h1>virtual host is up and perfectly working!</h1>

3. Create an Nginx Virtual Host in CentOS

sudo vim /etc/nginx/conf.d/

and add below

server {
        listen 80;
        listen [::]:80;

        root /var/www/;
        index index.html index.htm index.nginx-debian.html;


        location / {
                try_files $uri $uri/ =404;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;


To confirm that all Nginx configurations are sound and error-free, execute the command:

sudo nginx -t 

4. Restart Nginx

Restart Nginx server for the changes to be applied.

sudo systemctl restart nginx

Last thing , you need to open any browser and issue

Unix/Linux Guru and FOSS supporter


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook