Node.js VPS Deployment: A Comprehensive Guide
author image

Author:

Naman AroraTue, 16 Jan, 2024

Welcome to this guide on deploying a Node.js app on a Virtual Private Server (VPS). Whether you prefer to have control over your deployment environment or simply enjoy the hands-on experience, using a VPS is a powerful choice. In this tutorial, we'll go through the process step by step.

So, let's begin.

Step 1 - Install Nodejs

Let's download nodejs from Nodesource. NodeSource is a company which provides enterprise-grade Node support and maintains a repository containing the latest versions of Node.js.

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

Let's install nodejs now:

sudo apt-get install -y nodejs

Check the installation of node and npm using the following commands:

node --version && npm --version

You will see the versions of nodejs and npm

Step 2 - Creating a sample nodejs file

Before deploying a real application, let's create a simple Node.js app for demonstration purposes:

sudo nano app.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Lets now install express so that we can run this app server:

npm install express

let's test the app.

node app.js

Visit http://server-ip:3000 to see the "hello world" page.

Step 3 - Using a Process Manager

To ensure your application runs continuously, even if issues arise, we'll use pm2 as a process manager.

Install and start the application using the following commands:

sudo npm i pm2 -g

Start the application using the following command:

pm2 start app.js

Step 4 - Configuring Reverse proxy

To simplify access to your app and enhance security, configure Nginx as a reverse proxy.

Install Nginx and create a configuration file for your Node.js app:

sudo apt install nginx
sudo nano /etc/nginx/sites-available/myApp

Copy the following content to the conf file:

server{
  server_name <<ENTER YOUR SERVER ADDRESS HERE>>;

      location / {
        proxy_pass http://localhost:<<PORT USED BY APP>>;
        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;
    }
}

Activate the configuration:

sudo ln -s /etc/nginx/sites-available/myApp /etc/nginx/sites-enabled

Visit http://your-ip/ to see your application working seamlessly.

Conclusion:

Congratulations! You've successfully deployed a Node.js app on your VPS using NodeSource, pm2 as a process manager, and Nginx as a reverse proxy. Keep in mind that the sample app used here is not suitable for production. Always deploy production-ready applications, considering factors like scalability and security.

Happy coding!

author image

Naman Arora

Technical Author with a passion for translating the complexities of software, computers, and emerging technologies into accessible and engaging content. Armed with a background in computer science, I blend technical expertise with a flair for effective communication in my writing. Join me on this tech-savvy journey as we explore coding languages, unravel the nuances of software architecture, and stay informed about the latest tech trends. Let's navigate the digital frontier together!

Portfolio designed and developed by Naman Arora