■ AN INDEPENDENT FIELD GUIDE TO TECHNOLOGY EDITORIAL EDITION / 001
D/4 DIGIFOUR
← ALL GUIDES

How to Deploy a Node.js Application on Ubuntu VPS

A practical deployment workflow using Node.js, systemd, Nginx and HTTPS on Ubuntu.

Editorial note: This article provides general technical guidance. Features, prices and provider terms may change. Verify current information before choosing a service.

Overview

This tutorial demonstrates a small Node.js HTTP application running on an Ubuntu VPS behind Nginx, managed by systemd. It is a starting point, not a complete security architecture.

Requirements: Ubuntu VPS, a domain pointed at the server, a non-root user with sudo access, and permission to configure system services. Substitute your own domain and application paths in the examples.

1. Update the operating system

sudo apt update
sudo apt upgrade

Review pending changes and reboot if required by system updates. Set up a firewall and secure SSH access before exposing a production service.

2. Install Node.js

Use a maintained Node.js LTS release from a trusted source and verify its version. Ubuntu's repositories may not always contain the LTS version required by your application.

node --version
npm --version

Do not install an arbitrary third-party script without reviewing its source and suitability for your environment.

3. Create an application directory

Run application services under a dedicated unprivileged account. The following assumes you have already created a system user named nodeapp.

sudo mkdir -p /opt/my-node-app
sudo chown nodeapp:nodeapp /opt/my-node-app
sudo chmod 750 /opt/my-node-app

Copy the application code into that directory, then install dependencies using the lockfile.

cd /opt/my-node-app
npm ci --omit=dev

Run dependency installation as the application owner. Ensure the package configuration contains a valid start script.

4. Create a systemd service

Replace the Node.js executable path with the result of command -v node on your server.

[Unit]
Description=My Node.js application
After=network.target

[Service]
Type=simple
User=nodeapp
Group=nodeapp
WorkingDirectory=/opt/my-node-app
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Save the configuration as /etc/systemd/system/my-node-app.service, then load and start it.

sudo systemctl daemon-reload
sudo systemctl enable --now my-node-app
sudo systemctl status my-node-app

Prefer binding the Node.js application to 127.0.0.1:3000 when only Nginx should connect to it.

5. Configure an Nginx reverse proxy

Create a separate server block for the domain. This example assumes Nginx is already installed.

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Check the configuration before applying it.

sudo nginx -t
sudo systemctl reload nginx

6. Enable HTTPS

Once the domain resolves to your server and inbound port 80 is reachable, obtain a TLS certificate from a trusted certificate authority.

If Certbot's Nginx integration is installed, a typical command is:

sudo certbot --nginx -d example.com

Verify HTTPS and check that certificate renewal is configured. Do not use example.com literally for a real deployment.

7. Monitor your application

sudo journalctl -u my-node-app -n 100 --no-pager
sudo systemctl status my-node-app
sudo nginx -t

Add application health checks, structured logging, resource monitoring and alerts appropriate to the service.

8. Plan backups and rollback

Keep versioned application releases and a rollback procedure. Back up persistent data independently from the virtual server and test restores periodically.

Deployment checklist

← Browse all guides