In this part we’ll cover how to install Nginx and how to proxy pass HTTP requests to Nodejs.

INSTALL NODEJS

You should visit the official website on installation instructions but as of today this works.

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Update npm

sudo npm install npm -g

CREAT A SIMPLE APP

mkdir ~/myapp
cd ~/myapp

npm init. Click enter for all options

npm init

Create index.js and edit it

nano index.js
var http = require("http");

http.createServer(function(request, response){
        response.end("My app");
}).listen(8000);

INSTALL PM2

We’ll need something to manage our app in case it crashes and also to keep it running.

sudo npm install pm2 -g

Run start index.js with pm2

pm2 start index.js

Run this command to run pm2 on startup

pm2 startup ubuntu

INSTALL NGINX

sudo apt-get install nginx -y

Proxy pass to our app

Open this file for editing and delete everything inside it

nano /etc/nginx/sites-available/default

Add the following inside the file

server{
    listen 80;
    server_name localhost;
    
    location / {
        proxy_pass http://localhost:8000;
        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;
    }
}

DONE!

Your app is up and running behind Nginx. Simply type in your VPS’s IP into the url.