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 Ideal Solution for Your Node.js Application?

\n

You've written a cool JavaScript (Node.js) application, tested it locally, and now you want it to be available online 24/7. Shared hosting is often unsuitable for Node.js due to limitations, and cloud services (Heroku, Railway) can be expensive or unpredictable in terms of performance. A Virtual Private Server (VPS) is the sweet spot: full control, high performance, and an affordable price.

\n

In this step-by-step guide, we will walk through the process of deploying a JavaScript (Node.js) application on a VPS from scratch. You will learn how to set up the server, install the necessary software, securely run the application, and make it accessible via a domain. All code examples are functional and tested.

\n\n

1. Server Preparation: Initial VPS Setup

\n

First, you will need to rent a VPS from any provider (DigitalOcean, Vultr, Timeweb, Selectel). Minimum specifications: 1 vCPU, 1-2 GB RAM, 20-30 GB SSD. The operating system should be Ubuntu 22.04 LTS (the most stable version).

\n\n

1.1. Connecting to the Server via SSH

\n

After receiving the IP address and password, connect to the server via terminal (Mac/Linux) or PuTTY (Windows):

\n
ssh root@your_server_IP
\n

Immediately change the password and create a new user (working as root is bad practice):

\n
adduser deploy_user\nusermod -aG sudo deploy_user\nsu - deploy_user
\n\n

1.2. Updating the System and Installing Basic Packages

\n
sudo apt update && sudo apt upgrade -y\nsudo apt install -y curl git ufw build-essential
\n

Set up the basic UFW firewall (leave only SSH and the ports we will use):

\n
sudo ufw allow OpenSSH\nsudo ufw allow 80/tcp\nsudo ufw allow 443/tcp\nsudo ufw enable
\n\n

2. Installing Node.js and the PM2 Process Manager

\n

To run a JavaScript application on the server, we need Node.js. We will use the official NodeSource version manager to install the latest LTS version:

\n\n

2.1. Installing Node.js (v20 LTS)

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

Verify the installation:

\n
node -v   # v20.x.x\nnpm -v    # 10.x.x
\n\n

2.2. Installing and Configuring PM2

\n

PM2 is an advanced process manager for Node.js. It automatically restarts the application on crash, manages logs, and allows running multiple instances (clustering).

\n
sudo npm install -g pm2
\n

Configure PM2 to start automatically on server reboot:

\n
pm2 startup systemd\nsudo env PATH=$PATH:/usr/bin pm2 startup systemd -u deploy_user --hp /home/deploy_user
\n\n

3. Deploying the Application and Running via PM2

\n

Now we are ready to transfer your JavaScript application to the server. There are two popular methods: via Git (recommended) or via SCP/FileZilla.

\n\n

3.1. Cloning the Repository (Git)

\n
cd /home/deploy_user\ngit clone https://github.com/your_username/your_repository.git myapp\ncd myapp
\n

Install dependencies and build the project (if required):

\n
npm install\n# If there is a build script: npm run build
\n\n

3.2. Creating the .env Configuration File

\n

Never store secrets in code. Create a .env file in the project folder:

\n
nano .env
\n

Example content:

\n
PORT=3000\nNODE_ENV=production\nDATABASE_URL=mongodb://localhost:27017/myapp\nJWT_SECRET=your_super_secret_key
\n\n

3.3. Starting the Application via PM2

\n

Assume the entry point of your application is app.js or server.js. Start it:

\n
pm2 start app.js --name "myapp"
\n

Check the status:

\n
pm2 status\npm2 logs myapp                

Blogs

Book Recommendations