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:
- Entry File: The name of the file PM2 needs to execute (e.g.,
server.js). - 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:
- It installs your NPM packages via
npm install --production. - It spawns a PM2 background worker specifically jailed to the user account that owns the domain. PM2 guarantees that if your app crashes, it will restart instantly.
- It injects a custom
location /block into your Nginx vHost file, configuringproxy_pass http://127.0.0.1:3000;to forward all external web traffic directly into your PM2 instance.
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.