Stackrium Logo Stackrium

Setting up PM2 Reverse Proxies securely behind Nginx

S
Stackrium Engineering
Published on Oct 18, 2026 Node.js

Running an Express.js or Next.js app directly on port 80 or 443 is a terrible idea for production. You lose the ability to easily issue Let's Encrypt SSL certificates, and Node is notoriously inefficient at serving static assets like images and CSS.

The industry standard is to run your Node.js application on a high, internal port (like 3000 or 8080) using a process manager like PM2, and then place Nginx in front of it as a Reverse Proxy. Stackrium automates this entire architecture.

1. Prepare Your Node Application

First, create a system user and domain via the Stackrium dashboard. Then, upload your Node application into the domain's public_html folder.

Ensure your main entry file (e.g., server.js or app.js) is explicitly telling your app to listen on an internal port. You should use environment variables for this so Stackrium can pass the correct port dynamically:

const express = require('express');
const app = express();

// Use the port provided by the environment, or fallback to 3000
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Hello from behind Nginx!');
});

app.listen(PORT, () => {
    console.log(`Server running on internal port ${PORT}`);
});

2. The Stackrium PM2 Deployment

Navigate to the Web & Git tab, locate your domain, and click the App Engines button. Select Deploy Node.js.

Stackrium will ask you for two critical pieces of information:

  1. Entry File: The name of the file PM2 needs to execute (e.g., server.js).
  2. Internal Port: The port your app is listening on (e.g., 3000).

What happens when you click Deploy?

Stackrium executes a complex set of commands to isolate your app and configure Nginx natively:

3. Managing PM2 Lifecycle

If you push new code to your server, you need to tell PM2 to restart the app to load the new changes into memory. You don't need to touch the terminal.

Simply navigate to the domain's settings in Stackrium and click Restart App Worker. This safely restarts the specific PM2 process associated with that domain without affecting any other applications on your server.

Reverting: If you ever need to tear down the Node.js application, use the Revert to PHP tool. Stackrium will safely kill the PM2 daemon and remove the Reverse Proxy block from Nginx, restoring standard web routing.
Back to Tutorials Next: Automating CI/CD Deployments