In the earlier I have mentioned that how to create web server i.e. Apache. Now I would like to explain about how to install the web server Nginx and to run the multiple applications in a single system we need to create the virtual hosts. Suppose we have install the Nginx web server then default path to execute our files under “/var/www/html” but we want to change another folder then we can for the configurations file. The configuration file under “/etc/nginx/site-avialable/default”, in the below I will explain about how to create the virtual hosts for different applications like PHP, NodeJS, ReactJS, GraphQL.

How to create virtual hosts in Nginx

How to create virtual hosts in Nginx published by Anil Kumar Panigrahi

Installation of Nginx:

Step 1: Connect the server using SSH

1
ssh -i "[Your Server].pem" ubuntu@[IP Address]

Step 2: Run the command to install the Nginx in your Ubuntu system.

1
sudo apt install Nginx

It will asking for permission to install, just give ‘Y’ and that will install in your system.
Step 3: To enable the rules for the Nginx

1
sudo ufw app list

Step 4: Above command will provide the list of options to add it, to add the full run the below command

1
sudo ufw allow 'Nginx Full'

Step 5: Now we access the your server IP in the browser and it will denotes the Nginx start-up page
Step 6: To start the Nginx web server

1
sudo systemctl start nginx

Step 7: To stop the Nginx web server

1
sudo systemctl stop nginx

Step 8: To get the status the Nginx web server

1
sudo systemctl status nginx

Step 9: To restart the Nginx web server

1
sudo systemctl restart nginx

Step 10: If we change the Nginx configurations then we can check as

1
sudo nginx -t

Virtual host for PHP custom application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
        #listen 443;
       listen 443 ssl;
        root [Document Root];
        index index.php index.nginx-debian.html;
        server_name [Domain Name];
       
        ssl_certificate [SSL Certificate File];
        ssl_certificate_key [SSL Certificate Key File];

        location ~* \.(?:manifest|appcache|html?|xml|json)$ {
                expires -1;
                # access_log logs/static.log; # I don't usually include a static log
       }

        location ~* \.(?:css|js)$ {
                try_files $uri =404;
                expires 1y;
                access_log off;
                add_header Cache-Control "public";
        }

    location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php7.4-fpm.sock;
         }

        location / {
           try_files $uri $uri/ /index.php?$args;
        }
}

Virtual host for NodeJS custom application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
upstream [Domain Name] {
        server 127.0.0.1:6000;
        keepalive 100000;
}

server {
        listen 80;
        server_name [Domain Name];
        return 301 https://[Domain Name];
}

server {
        listen 443 ssl;
        root [Document Root];
        index index.php index.nginx-debian.html;
        server_name [Domain Name];
       
        ssl_certificate [SSL Certificate File];
        ssl_certificate_key [SSL Certificate Key File];

        location ~* \.(?:manifest|appcache|html?|xml|json)$ {
                expires -1;
                # access_log logs/static.log; # I don't usually include a static log
       }
       
        location / {
                 proxy_pass http://localhost:6000;
                 proxy_buffering on;
                 proxy_http_version 1.1;
                 proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection 'Upgrade';
                 proxy_set_header Host $host;
                 proxy_cache_bypass $http_upgrade;
        }
   
        location ~* \.(?:css|js)$ {
                try_files $uri =404;
                expires 1y;
                access_log off;
                add_header Cache-Control "public";
        }
}

In the next articles I am going to provide the code for below.
Virtual host for ReactJS custom application:

Virtual host for GraphQL custom application:


2 Comments

How to create virtual hosts in Nginx - Part 2 - Anil Labs · February 19, 2022 at 10:37 am

[…] I mentioned in the earlier post regarding the How to create virtual host for PHP and NodeJS applications, now in this post we will learn to how create the virtual hosts for the ReactJS and GraphQL […]

Install Symfony 5 with PHP 7.4 on Ubuntu with Nginx - Anil Labs · July 12, 2022 at 8:00 am

[…] this post I will try to provide the simple steps to install the another PHP framework Symfony with Nginx. Presently we have Symfony 6 with PHP 8.* versions. But when we have PHP 7.4 these steps will be […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *