In this post, I will explain how to host multiple web apps on a single server without using Domain Name System (DNS)

I was working on two web applications for my employer which will be used only for internal purposes. So to host this applications where employees can access from inside the office premises, I setup us an Ubuntu server with Nginx, PHP and MySQL.

Nginx can listen to multiple ports at the same time. So I enabled port 80 and 81, and cloned both the application to two separate folders /var/www/application1 and /var/www/application2 .

You can refer to following article to install and setup firewall on Ubuntu 18.04 https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04

The Nginx config file in path /etc/nginx/sites-available/projects looks like below. Note that projects is the config file name in this case.

server {
    listen 80;

    root /var/www/application1/public;

    index index.html index.htm index.nginx-debian.html;

    server_name 192.168.1.1;

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

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

server {
    listen 81;

    root /var/www/application2/public;

    index index.html index.htm index.nginx-debian.html;

    server_name 192.168.1.1:81;

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

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

Above is the two server blocks I wrote to handle the incoming request and redirect to intended application. Hence when you type 192.168.1.1 in browser it will open application1. Likewise when you type 192.168.1.1:81 it will go to application2.