DEV Community

Sadhan Sarker
Sadhan Sarker

Posted on

Multiple Service Deployments In A Single Host

๐Ÿ“Œ What is Nginx?

Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. It handles server-related aspects, like SSL and caching, completely transparent to the application behind it.

Here, I'm going to show how to do that in NodeJs, But a similar approach might be used for others,

Let's move on,๐Ÿ˜Ž

๐Ÿ“ŒFirst, Installing Nginx on Ubuntu

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install nginx -y
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ŒCheck Nginx status & start it,

Check status, and start it using the following commands:-

sudo systemctl status nginx
sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

To run Nginx on system startup, using command below:-

sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ŒDependency & Package Manager Varification

Check if Nodejs, and npm are already installed on server by using,

node --version
npm --version
Enter fullscreen mode Exit fullscreen mode

If not then, Install Nodejs using the following commands:-

sudo apt-get update
sudo apt-get install nodejs
Enter fullscreen mode Exit fullscreen mode

Now install npm using the following command:-

sudo apt-get install npm
Enter fullscreen mode Exit fullscreen mode

Verify Nodejs and npm installations using below commands

node --version
npm --version
Enter fullscreen mode Exit fullscreen mode

Now, It should return the Nodejs and npm installed versions.

๐Ÿ“ŒSetting up Nginx for Nodejs application

In my case, I have only one main domain and like to deploy an application in different subdomains.

We are going to do deploy multi-application in a single host using NGINX.
That means our Host Ip address is same, just deploy an application in different ports.

๐Ÿ”ฐ Application 1 Setup:-

vi /etc/nginx/conf.d/example1.sadhan.com.conf
Enter fullscreen mode Exit fullscreen mode
server {
    listen 80;

    server_name example1.sadhan.com www.example1.sadhan.com;

    location / {
        proxy_pass http://51.78.30.44:3001;
        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;
    }
}

Enter fullscreen mode Exit fullscreen mode

Run first, Nodejs application in demond

node home/project1/app.js &
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฐ Application 2 Setup:-

vi /etc/nginx/conf.d/example2.sadhan.com.conf
Enter fullscreen mode Exit fullscreen mode
server {
    listen 80;

    server_name example2.sadhan.com www.example2.sadhan.com;

    location / {
        proxy_pass http://51.78.30.44:3002;
        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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Run first, Nodejs application in demond

node home/project2/app.js &
Enter fullscreen mode Exit fullscreen mode

Note: & is using so that the application can be run in background. In my case, this is my public IP 51.78.30.44.
You need to configure DNS & Namespace properly in Domain Panel. So that the main IP properly mapped.

๐Ÿ“ŒFinally, Restart & Varifiy NGINX Server is running or not

service nginx restart
systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฐ Now open Browser and hit

๐Ÿ‘Œ Congratulations That's all about!. & Thanks for your time & passion.
Feel free to comments, If you have any issues & queries.

Bonus Troubleshoot, Firewall Configurations

If firewall is running, then might some ports are not exposed externally. To do so, open firewall ports following below guides

#---------- On Debian/Ubuntu ----------
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 3001/tcp
sudo ufw allow 3002/tcp
sudo ufw reload

#---------- On CentOS/RHEL ----------
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=3001/tcp
firewall-cmd --permanent --add-port=3002/tcp
firewall-cmd --reload 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“— Future Explore

If we want to run service after reboot then follow,

Top comments (1)

Collapse
 
sreesexplorer profile image
srees-explorer • Edited

i have done the steps mentioned but the same is not working from outside the machine
What did you mean by this
Note: & is using so that the application can be run in background. In my case, this is my public IP 51.78.30.44.
You need to configure DNS & Namespace properly in Domain Panel. So that the main IP properly mapped.