Multiple Service Deployments In A Single Host
Tags: nginx, server, tutorial, node
Published: 2020-02-19 17:15:23


## 📌 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

## 📌Check Nginx status & start it,

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

    sudo systemctl status nginx
    sudo systemctl start nginx

To run Nginx on system startup, using command below:-
    
    sudo systemctl enable nginx


## 📌Dependency & Package Manager Varification

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

    node --version
    npm --version

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

    sudo apt-get update
    sudo apt-get install nodejs

Now install `npm` using the following command:-

    sudo apt-get install npm

Verify `Nodejs` and `npm` installations using below commands

    node --version
    npm --version

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

```
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;
    }
}

```

Run first, Nodejs application in demond

    node home/project1/app.js &


---


🔰 Application 2 Setup:-

    vi /etc/nginx/conf.d/example2.sadhan.com.conf

```
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;
    }
}
```

Run first, Nodejs application in demond

    node home/project2/app.js &


> 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


🔰 Now open Browser and hit

- http://example1.sadhan.com # it will be different from your subdomain
- http://example2.sadhan.com # it will be different from your subdomain


👌 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

```bash
#---------- 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 
```

## 📗 Future Explore
If we want to run service after reboot then follow,

- https://pm2.keymetrics.io/
- https://www.npmjs.com/package/forever