Run a Node JS app in VPS
By: Farhaz Alam
Tags: Vps, Node, NPM, Server, Azure

Image from https://www.codementor.io/

In this article, I will be discussing how we can run a Node Js app inside a VPS so that we can access it from anywhere.

I have a basic VPS with Ubuntu and a subdomain setup where I will be setting up my node application. If you don't know how to set up a subdomain, you can check my previous article. First of all login into your VPS by using putty or Powershell by using the command ssh username@host .

Step 1- Installing Node

Install Node JS and NPM by using the command:

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

$ sudo apt-get install -y nodejs

You can always verify the installation by checking their version numbers:

$ node -v

$ npm -v

Step 2- Setting up PM2

Now our Node js is installed, now all you have to do is to take any sample code that you want to run and navigate to that folder. My Node app is configured to run at port 2121. Run the app to check if it is running.

Install the PM2 package using the command:

$ sudo npm install pm2@latest -g

Now we will run our Node App using the PM2 command, make sure you are inside your Node project Folder:

$ pm2 start index.js

After this, you could see something like this:

Now that our app is running by PM2, we will make sure that the app runs even after a server restart, we can do so by:

$ pm2 startup systemd

The above command’s output will generate another command to be run, it will look something like this:

sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u farhazalam --hp /home/farhazalam

Just run that command by copying and pasting in your own terminal.

Save the process by using the command:

$ pm2 save

Now take a break and restart your VPS.

After your machine has successfully restarted, ssh into it and start the service with systemctl:

$ sudo systemctl start pm2-farhazalam

Dont forget to use your username instead of farhazalam .

Now, you can check the status by typing:

$ systemctl status pm2-farhazalam

Stop, start, restart, or check your pm2 instance by running:

$ pm2 stop name_of_process

$ pm2 start name_of_process

$ pm2 restart name_of_process

$ pm2 status

Step 3- Set up Nginx Server Block

Open the server block in which you want to configure your node app, I will be using my subdomain for this. You can also use the default server block to set up. Open the server block by typing:

$ sudo nano /etc/nginx/sites-available/api.jugaadapp.me

Search for the location block inside the server block and paste the following lines.

Note: Don’t forget to change the localhost port to the port which you have set for your node js app. Note: Comment the line try_files $uri $uri/ =404; by adding a # before it.

The final block will look something like this:

server {

...

location / {

proxy_pass http://localhost:2121;

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;

# try_files $uri $uri/ =404; }

...

}

Optional You can also add another location block if you want to run some other node js with different ports but on the same domain. you can do it by adding like this:

server {

...

location /api2 {

proxy_pass http://localhost:3131;

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;

}

...

}

You can access the second location by www.domain.com/api2 .

Now all that is left is to check for syntax errors and then restart your Nginx server:

$ sudo nginx -t

$ sudo systemctl restart nginx

Voila, you have now your node js app running in your server which can be accessed from anywhere.

ENJOY!!