Deploying a JavaScript Application on VPS: A Step-by-Step Guide from A to Z

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 VPS is the Right Choice for Your Node.js Application

You've written a cool application in JavaScript (Node.js), tested it locally, and now it's ready to conquer the world. But how do you move it from the cozy development environment to the harsh world of production? The answer is simple — VPS (Virtual Private Server). Unlike shared hosting, a VPS gives you full control over the server, root access, and the ability to fine-tune the environment for your Node.js application's needs.

In this step-by-step guide, we will go through the entire process: from purchasing a server and installing Node.js to configuring a domain, SSL certificate, and the PM2 process manager. You will learn how to make your site stable, fast, and secure. Let's go!



Step 1: Preparing the VPS and Connecting to the Server

First, you need to rent a VPS. Any popular providers will work: DigitalOcean, Linode, Vultr, or their Russian equivalents. The minimum configuration for a small application is 1 vCPU and 1 GB RAM (with Ubuntu 20.04 or 22.04 LTS).

After creating the server, you will receive an IP address and a root password. Connect via SSH:

ssh root@your_IP_address

If you are using macOS or Linux, the SSH client is already built-in. On Windows, you can use PowerShell or Putty.

Update the system immediately:

apt update && apt upgrade -y

Tip: create a separate user (not root) for daily work. This will improve security:

adduser myuserusermod -aG sudo myusersu - myuser


Installing Node.js and npm

We will install the latest stable version of Node.js via NodeSource — this is the most reliable method:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejs

Check the versions:

node --version   # v20.x.xnpm --version    # 10.x.x

Great! Now we have an environment to run JavaScript code on the server.



Step 2: Uploading the Application to the Server and Installing Dependencies

There are several ways to transfer the code. The simplest for beginners is to use git clone. If your code is stored on GitHub/GitLab, run:

git clone https://github.com/your_account/your_repository.git /var/www/myappcd /var/www/myapp

If the repository is private, configure SSH keys or use a Personal Access Token.

An alternative method is to upload files via SCP (from your local console):

scp -r /path/to/your/project myuser@your_IP:/var/www/myapp

After transferring the code, install the dependencies:

cd /var/www/myappnpm install --production

The --production flag ensures that dev-dependencies (tests, bundlers, etc.) are not installed, saving space and improving security.



Configuring Environment Variables

Never store passwords and API keys in the code. Create a .env file in the project root:

nano .env

Example content:

PORT=3000DB_HOST=localhostDB_USER=myuserDB_PASS=supersecretJWT_SECRET=myjwtsecret123

Make sure this file is added to .gitignore. To read variables in the code, use the dotenv package (it should already be in your dependencies).



Step 3: Running the Application with PM2 and Setting Up Auto-Start

If you run node app.js and close the SSH session, the application will stop. We need a process manager that will keep the application running 24/7. PM2 is the ideal choice for Node.js.

Install PM2 globally:

sudo npm install -g pm2

Start the application:

pm2 start app.js --name "myapp"

If your application uses port 3000,

Blogs

Book Recommendations