Deploying a JavaScript Application on VPS: Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Deploy a JavaScript Application on a VPS?

Developing a modern web application in JavaScript (Node.js) is only half the battle. The second, equally important part is deployment, i.e., deploying your project on a real server accessible on the internet. Using a Virtual Private Server (VPS) gives you full control over the environment, unlike shared hosting. You can configure the server to your needs, install any dependencies, and ensure high performance.

In this guide, we will walk through the process of deploying a typical JavaScript (Node.js) application on a VPS step by step. We will cover all stages: from initial server setup to running the application in production mode using the PM2 process manager and the Nginx reverse proxy server. The article is aimed at developers already familiar with the basics of Node.js and the Linux command line.



1. VPS Preparation and Initial Setup

The first step is to rent a VPS from any provider (DigitalOcean, Linode, Vultr, or Russian equivalents). Recommended minimum plan: 1 vCPU, 1-2 GB RAM. Operating system — Ubuntu 22.04 LTS (or newer).



1.1. Connecting to the Server via SSH

After receiving the IP address and root password, connect to the server from your terminal:

ssh root@your_server_IP

If you are using an SSH key (recommended), specify the path to the private key:

ssh -i ~/.ssh/id_rsa root@your_server_IP


1.2. Creating a User and Basic Security

Never work as root permanently. Create a new user and grant them sudo privileges:

adduser myuserusermod -aG sudo myuser

Then configure an SSH key for the new user (copy the public key from your local machine) and disable password login in the /etc/ssh/sshd_config file:

PasswordAuthentication no

Restart the SSH service: sudo systemctl restart sshd.



1.3. Installing Necessary Software

Update the package manager and install Node.js (we recommend the LTS version via NodeSource), Git, and Nginx:

sudo apt update && sudo apt upgrade -ycurl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejs git nginx

Check the versions: node -v and npm -v.



2. Cloning the Repository and Installing Dependencies

Now that the server is ready, let's transfer your application code to it.



2.1. Cloning the Project

Navigate to the user's home directory and clone the repository from GitHub (or another Git hosting service):

cd /home/myusergit clone https://github.com/your_username/your_repository.git myappcd myapp


2.2. Installing Dependencies and Test Run

Install all npm packages listed in package.json:

npm install

If the project uses environment variables (e.g., for a database or API keys), create a .env file and fill it in:

nano .env

Example content:

PORT=3000DATABASE_URL=mongodb://localhost:27017/mydbNODE_ENV=production

Run the application manually to make sure it works:

node app.js

Check accessibility via curl http://localhost:3000 or open http://your_IP:3000 in a browser (after allowing the port in the firewall). If everything works, stop the process (Ctrl+C).



3. Configuring the PM2 Process Manager

An application started via node app.js will stop as soon as you close the SSH session. For persistent operation, PM2 is used — a process manager for Node.js that automatically restarts the application if it crashes and manages

Blogs

Book Recommendations