Guides

How to Deploy a Node.js App to a VPS Server

Muhammad SaadApril 17, 20265 min read
How to Deploy a Node.js App to a VPS Server

Deploying your first Node.js application to a production server can feel intimidating. You've built something amazing locally, but now you need to get it live on the internet where real users can access it. The good news? With the right approach, deploying to a VPS (Virtual Private Server) is straightforward and gives you complete control over your application.

In this guide, I'll walk you through deploying a Node.js app to a VPS server step-by-step, from initial server setup to running your application in production.

Why Choose a VPS for Node.js Hosting?

Before we dive in, let's quickly cover why a VPS is often the best choice for Node.js applications:

  • Full control: You control the entire server environment
  • Better performance: Dedicated resources mean consistent speed
  • Scalability: Easy to upgrade as your app grows
  • Cost-effective: More affordable than managed services for long-term projects
  • Learning opportunity: Understanding server management makes you a better developer

Prerequisites

For this tutorial, you'll need:

  • A Node.js application ready to deploy
  • A VPS running Ubuntu 22.04 (we'll use Ubuntu, but these steps work on most Linux distributions)
  • SSH access to your server
  • A domain name (optional but recommended)
  • Basic familiarity with the command line

Step 1: Initial Server Setup

First, SSH into your server:

ssh root@your-server-ip

Update your system packages:

apt update && apt upgrade -y

Create a new user for better security (never run applications as root):

adduser nodeapp
usermod -aG sudo nodeapp

Switch to the new user:

su - nodeapp

Step 2: Install Node.js

We'll use NodeSource to install the latest LTS version of Node.js:

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

Verify the installation:

node --version
npm --version

Step 3: Install and Configure PM2

PM2 is a process manager that keeps your Node.js app running, restarts it if it crashes, and makes deployment much easier.

sudo npm install -g pm2

Step 4: Upload Your Application

There are several ways to get your code onto the server. The most professional approach is using Git:

cd ~
git clone https://github.com/yourusername/your-app.git
cd your-app

Install your dependencies:

npm install --production

Step 5: Configure Environment Variables

Create a .env file for your production environment variables:

nano .env

Add your configuration (never commit this file to Git):

PORT=3000
NODE_ENV=production
DATABASE_URL=your_database_connection_string

Step 6: Start Your App with PM2

Start your application using PM2:

pm2 start app.js --name "my-app"

Make PM2 restart on server reboot:

pm2 startup systemd
pm2 save

Check your app status:

pm2 status
pm2 logs my-app

Step 7: Set Up Nginx as a Reverse Proxy

While your Node.js app runs on port 3000, you want users to access it via port 80 (HTTP) or 443 (HTTPS). Nginx handles this perfectly.

Install Nginx:

sudo apt install nginx -y

Create a new Nginx configuration:

sudo nano /etc/nginx/sites-available/my-app

Add this configuration:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }
}

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 8: Set Up SSL with Let's Encrypt

Secure your application with a free SSL certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Certbot will automatically configure HTTPS and set up auto-renewal.

Step 9: Configure Firewall

Allow necessary ports through the firewall:

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Monitoring and Maintenance

Keep an eye on your application:

# View logs
pm2 logs my-app

# Monitor resources
pm2 monit

# Restart your app
pm2 restart my-app

Set up automatic updates for security patches:

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

Common Deployment Issues and Fixes

App crashes on startup: Check logs with pm2 logs - usually environment variable or dependency issues.

Can't connect to database: Ensure your database allows connections from your server's IP address.

502 Bad Gateway: Your Node.js app isn't running. Check pm2 status and restart if needed.

Deploy Updates with Ease

When you need to deploy updates:

cd ~/your-app
git pull
npm install --production
pm2 restart my-app

Consider setting up a deployment script to automate this process.

Take Your App Live with Confidence

You've now deployed a production-ready Node.js application! Your app is running behind a professional web server, secured with SSL, and monitored by PM2 to ensure uptime.

Ready to host your Node.js application? DeployBase offers VPS hosting optimized for Node.js applications with SSD storage, guaranteed uptime, and expert support when you need it. Get started today and take your project from localhost to live in minutes.

Share this article

Muhammad Saad

Muhammad Saad

DeployBase Team

Ready to Get Started?

Join thousands of developers who trust DeployBase for their hosting needs.

View Plans