Deploy a Node.js App on a DigitalOcean Droplet

The app process can stop when your SSH session ends, even while the Droplet stays up. I’m curious about the handoff that turns that private listener into a public route.

You’ll keep the app process running and put Nginx in front of its loopback port. Start by tracing how a request travels from your domain to Node.js on the Droplet.

How a Node.js app reaches your domain

A Droplet is your Linux server. Nginx handles public requests on ports 80 and 443, then forwards them to the separate Node.js process on its private loopback port.

DNS points the domain to the Droplet’s public IP, the firewall admits web traffic, and PM2 keeps the app process available behind Nginx. The app port stays private, so the browser never connects to it directly.

Diagram showing browser traffic moving through Nginx to a Node.js app on loopback
Nginx accepts the public request and forwards it to the app on the same Droplet.

What you need on the Droplet

Start with an Ubuntu Droplet and a non-root account that can run sudo. The app needs a start command and its environment values, while a custom hostname also needs a domain you can edit.

ItemReady state
Droplet accessUbuntu server, SSH access, and a sudo-capable deployment account
Node.js appRepository or source files, an entry point, and required environment variables
RuntimeA supported Node.js LTS release that matches the app’s dependencies
Process managerPM2 installed for the deployment account
DomainAn editable DNS zone for a hostname such as app.example.com
NetworkInbound SSH plus HTTP and HTTPS where used, with no public rule for the app port

Deploy the app and configure its public route

Work through the server setup first, then connect DNS, Nginx, and HTTPS. Keep one private app port consistent in the Node process and Nginx upstream.

Step 1. Install Git and Nginx

Run the package commands from your sudo-capable account on the Ubuntu Droplet. The update refreshes Ubuntu’s package index before Git and Nginx are installed.

sudo apt update
sudo apt install -y git nginx

Install a Node.js version manager for the deployment account using the nvm project’s installation instructions, then choose the LTS release your app supports.

nvm install --lts
node --version

Node.js lists v24.21.0 in LTS and v26.10.0 in Current, while its release schedule says production apps should use an Active or Maintenance LTS release.

Step 2. Put the app on the server

Clone your own repository into a directory owned by the deployment account, or transfer the source files there. Keep secrets out of Git and make them available through the app’s supported environment configuration.

If the app uses npm packages, install its committed production dependency tree from the project root. The npm ci documentation says the command needs a lockfile and stops if it disagrees with package.json.

npm ci --omit=dev

The omit flag leaves development-only packages out of the server install. The built-in sample below has no external npm dependencies, so it does not need this step.

The sample’s server.mjs file uses Node’s built-in HTTP module for a health route on 127.0.0.1:3101, so it needs no external package.

import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';

const host = '127.0.0.1';
const port = Number(process.env.PORT ?? 3101);

export function createAppServer() {
  return createServer((request, response) => {
    if (request.url === '/health') {
      response.writeHead(200, { 'content-type': 'application/json' });
      response.end(JSON.stringify({ status: 'ok' }));
      return;
    }

    response.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
    response.end('Node.js app is running.\n');
  });
}

if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
  createAppServer().listen(port, host, () => {
    console.log(`Listening on http://${host}:${port}`);
  });
}

An existing Express or Fastify app can use the same loopback binding. Set PORT to match its listener to the Nginx upstream.

Run the health test on an ephemeral port before setting up PM2 or DNS, then assert the HTTP status and JSON body and close the server in a finally block.

import assert from 'node:assert/strict';
import { createAppServer } from './server.mjs';

const server = createAppServer();
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));

try {
  const { port } = server.address();
  const response = await fetch(`http://127.0.0.1:${port}/health`);
  assert.equal(response.status, 200);
  assert.deepEqual(await response.json(), { status: 'ok' });
  console.log(`HTTP ${response.status}`);
  console.log('{"status":"ok"}');
} finally {
  await new Promise((resolve, reject) => {
    server.close((error) => (error ? reject(error) : resolve()));
  });
}

I ran this test on Node.js v26.10.0, and it returned HTTP 200 with the expected JSON body. That confirms the app handler locally, not the Droplet’s DNS, PM2, Nginx, or firewall.

Terminal output from the Node.js health-route smoke test showing HTTP 200 and status ok
The smoke test receives HTTP 200 and the expected health response.

Step 3. Keep the process alive with PM2

PM2 is a separate process manager, so install its CLI for the deployment account’s active Node.js runtime before registering the app.

npm install --global pm2

The global install keeps PM2 outside the app’s package manifest. Start the app under the account that owns its files and set the port to match the Nginx upstream.

PORT=3101 pm2 start server.mjs --name my-app

PM2 needs a systemd service before it can restore this app after reboot. The PM2 startup guide shows the command for your account and Node path.

pm2 startup

Run the systemd command printed by PM2 exactly as shown, then save the app list and check its state.

pm2 save
pm2 status

After the server boots again, PM2 restores the saved process list for the deployment account. Regenerate the startup command after a Node.js path change.

Step 4. Point DNS at the Droplet and allow web traffic

Add an A record for your chosen hostname that points to the Droplet’s public IPv4 address. The DigitalOcean DNS guide notes that cached TTL values can delay when a change appears in resolvers.

Inbound portAllow it for
22SSH from your administration address
80HTTP and the Certbot domain challenge
443HTTPS requests
3101Keep it private. Nginx reaches this app port over loopback.

DigitalOcean Cloud Firewalls and UFW are separate filters. Apply the required inbound rules to each one you use, and preserve SSH access before enabling a host firewall.

Step 5. Put Nginx in front of the app

Create the server block for your hostname and pass each request to the app’s loopback port through Nginx’s proxy_pass directive.

server {
    listen 80;
    server_name app.example.com;

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

Enable the file at /etc/nginx/sites-available/app.example.com and run sudo nginx -t before reloading the Nginx service.

sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/app.example.com
sudo nginx -t
sudo systemctl reload nginx

Nginx’s syntax test confirms that it can parse the enabled configuration. The upstream needs a separate health check.

Terminal showing the successful output from sudo nginx -t
Nginx prints this success message after its syntax check passes.

Step 6. Add HTTPS after HTTP works

Request the certificate after the A record resolves to this Droplet and inbound port 80 reaches Nginx. Install Certbot using its Nginx and Linux instructions, then ask it to configure the hostname.

sudo certbot --nginx -d app.example.com

Replace app.example.com with the hostname in your DNS record. Certbot needs the HTTP route to reach the Droplet so it can prove control of that name before it installs the certificate.

Diagnose the failures that block traffic

Check the layer that first stops responding instead of changing DNS, PM2, and Nginx at once. Each symptom narrows the next check.

SymptomCheck next
The app stops after SSH closes or a rebootRun pm2 status, confirm the app is online, and verify the systemd command from pm2 startup ran before pm2 save.
Nginx returns 502 Bad GatewayCompare the app’s PORT value with proxy_pass, then check the PM2 process and app logs.
The domain does not reach the DropletCheck that its A record returns the Droplet’s public IPv4 address and that both firewall layers allow the web port.
Certbot cannot validate the hostnameConfirm the A record resolves to this server and inbound port 80 reaches the Nginx server block for that name.

A 502 means Nginx could not get a response from the configured upstream. If the app works on its local health route but the domain does not, check the server block and firewall before changing the app.

Keep the app port private

Leave the Node.js listener on loopback and let Nginx own public ports 80 and 443. After deploying new code, check the named PM2 process before you investigate DNS again.

pm2 status

Frequently asked questions

Each failure belongs to the process, proxy, or DNS layer.

Do I need to open the Node.js port in the firewall?

No. Keep the app port private and allow inbound traffic on SSH and the web ports you use.

Will PM2 keep the app running after a reboot?

Yes, after you run the systemd command printed by pm2 startup and save the process list with pm2 save.

Why does Nginx return 502 Bad Gateway?

Nginx cannot reach the configured upstream. Check that PM2 has an online process and the app listens on the same loopback port named by proxy_pass.

How long does DigitalOcean DNS take to update?

It depends on cached TTL values. Check that the A record returns the Droplet’s IPv4 address before requesting a certificate.

Pankaj Kumar
Pankaj Kumar

Pankaj Kumar is the founder and CEO of CodeForGeek, with more than 14 years in IT. He is an open-source enthusiast who enjoys sharing what he learns through CodeForGeek and YouTube, with a focus on Python, data analytics, machine learning, Angular, Node.js, and Kafka.

Articles: 337