To host a Node.js app, you run a process around the clock that answers requests, next to its database, on a server that restarts it on its own and updates without interrupting service. This guide follows a Node.js or Next.js app with PostgreSQL into production, step by step, based on the official documentation.
By Simon Vasconcelos Lee
Published on 27 November 20269-minute read
NEXT_PUBLIC_ variables, which end up in the browser’s JavaScript.In production, the app becomes a service: a permanent Node.js process, a web server in front of it, a database beside it, and the means to restart, log and update the whole. That is what a server dedicated to the app brings: permanent processes, a database tuned for it, background jobs and reserved resources.
| Component | Role | Common choice |
|---|---|---|
| Node.js | Runs the app | An LTS version |
| Process manager | Starts and restarts the app, keeps its logs | systemd or PM2 |
| Reverse proxy | Receives traffic, holds the HTTPS certificate | Nginx |
| Certificate | Encrypts traffic | Let's Encrypt, renewed by Certbot |
| Database | Keeps the data | PostgreSQL, on the same server at launch |
| Firewall | Opens SSH, HTTP and HTTPS, and closes the rest | ufw or nftables |
Each line is installed once, then maintained. On a Managed VPS, the host looks after this foundation for you and proactively monitors the app: it spots errors, slowdowns, expensive queries and shrinking disk space, then recommends the fix, which you apply, yourself or through your AI assistant.
The Node.js documentation reserves production for LTS versions ("Active LTS" or "Maintenance LTS"), which as a rule receive critical fixes for 30 months in total. In November 2026:
| Version | Status | End of maintenance |
|---|---|---|
| Node.js 26 | Active LTS since 28 October 2026 | 30 April 2029 |
| Node.js 24 | Maintenance LTS since 20 October 2026 | 30 April 2028 |
| Node.js 22 | Maintenance LTS | 30 April 2027 |
A new project starts on Node.js 26; Next.js requires Node.js 20.9 at minimum, which all three versions exceed. From Node.js 27 on, the rhythm becomes yearly: one major version a year, each moving to LTS after six months.
Started by hand, a process stops with the SSH session. Two tools take over:
Restart=on-failure, the setting its documentation recommends for long-running services, restarts it after an error. Logs go to the system journal, capped by default at 10% of the disk and 4 GB.pm2 startup creates the startup service, pm2 save records the list of apps to restart, and cluster mode (pm2 start app.js -i max) starts one process per core. Its logs are written to ~/.pm2/logs, and the pm2-logrotate module caps their size.[Unit]
Description=My Next.js app
After=network.target postgresql.service
[Service]
User=my-app
WorkingDirectory=/srv/my-app/current/.next/standalone
Environment=NODE_ENV=production PORT=3000 HOSTNAME=127.0.0.1
EnvironmentFile=/etc/my-app/secrets.env
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target
systemctl enable --now my-app enables and starts the service; journalctl -u my-app -f follows its logs. The app runs under a dedicated account, with no administrator rights. For Express, Fastify or NestJS, ExecStart points to the app’s entry point. With PM2, a Node.js update ends with pm2 unstartup then pm2 startup, to launch the new binary.
The Next.js documentation recommends a reverse proxy such as Nginx in front of the Node.js server: it absorbs malformed requests, slow connections, oversized uploads and traffic surges. next start listens on all interfaces by default (0.0.0.0): make it listen on 127.0.0.1, as in the unit above, so that Nginx is its only way in.
server {
listen 80;
server_name app.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-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
sudo certbot --nginx then obtains the Let's Encrypt certificate and installs it in this block; Certbot renews it on its own, and sudo certbot renew --dry-run tests the renewal. The Upgrade and Connection headers let WebSockets through.
For Next.js streaming (Suspense, loading.js), Nginx must pass responses on as they arrive: the app sends the X-Accel-Buffering: no header, declared in headers() in next.config.js, and Nginx stops buffering them.
Deployed as a Node.js server, Next.js supports all of its features: next build compiles, next start serves the app, on port 3000 by default. With output: 'standalone' in next.config.js, the build produces a self-contained .next/standalone folder, dependencies included, and a server.js to start with node, next to which two folders are copied:
cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/
On the configuration side, three rules:
DATABASE_URL, API keys) stay on the server and are read at runtime. Put them in a file readable only by the service account (chmod 600), loaded by EnvironmentFile=, outside the Git repository.NEXT_PUBLIC_ are written into the JavaScript sent to the browser at next build time: keep this prefix for public values, and rebuild to change one.NODE_ENV=production: many libraries read this variable, and the Node.js documentation says to always set it in production.The port is set outside the .env files, since the Next.js HTTP server starts before they are read: pass it through PORT in the systemd unit. For the most sensitive secrets, the systemd documentation offers LoadCredential=: environment variables are passed on to child processes, and those set with Environment= can be read by unprivileged accounts.
At launch, PostgreSQL fits on the same server as the app: no network latency, a single server to back up. By default, it accepts local connections only (listen_addresses = 'localhost'): keep that setting, and give the app its own role, without superuser rights. The node-postgres pool opens at most 10 connections by default, and PostgreSQL typically accepts 100: add up the pools of your instances to stay under that ceiling.
| What runs | Memory to plan for |
|---|---|
| A Node.js process (API, Next.js) | 60 to 300 MB, depending on the code |
Running next build on the server | 1 to 2 GB for a few minutes, on top of the rest |
| PostgreSQL | 25% of memory for shared_buffers, plus a few MB per connection |
| A Node.js API and its PostgreSQL database, first users | 4 GB in total |
| Next.js built on the server, its API and PostgreSQL | 6 to 8 GB in total |
The Node.js heap size is capped with --max-old-space-size, in MiB: on a machine with 2 GiB, the Node.js documentation suggests 1536, to leave room for the rest of the system. Building in your continuous integration, then deploying only the result, removes the build peak.
releases/2026-11-17): npm ci installs exactly the versions in package-lock.json, then npm run build, while the live app keeps running.current link points to the new folder, then the service restarts. On receiving SIGTERM, Next.js finishes the requests in progress; its documentation advises allowing it 10 to 30 seconds, hence TimeoutStopSec=30.current back to the previous folder.To remove the restart time as well, run two instances of the same build, on ports 3000 and 3001, in an Nginx upstream block, and restart them one after the other: Nginx hands requests to whichever one answers (proxy_next_upstream error timeout, its default setting). With PM2 in cluster mode, pm2 reload chains the restarts the same way. If the app revalidates pages (ISR), the Next.js documentation describes the cache to share between instances.
npm audit reports known vulnerabilities and npm audit fix applies compatible fixes; with --force, it can install major versions, to be tested first.npx next upgrade moves to the latest version; each release brings its security fixes, and following them closely keeps every update small.With a Simafri Managed VPS, our teams keep your server up to date, monitor it day and night and step in without waiting for your call; they also proactively monitor your Node.js app and recommend what will make it faster and more secure, which you apply, yourself or through your AI assistant. Encrypted backups every night, kept off the machine, and complimentary migration. If you would rather stay in control, VPS Pro includes Full-Stack Assistance: we diagnose and tell you what to do, from the operating system down to your application code.
An LTS version. Node.js 26 has been in Active LTS since 28 October 2026, maintained until 30 April 2029; Node.js 24, in Maintenance LTS, is maintained until 30 April 2028.
Both restart the app after an error and when the server boots. systemd is already on the system and stores logs in the system journal. PM2 adds cluster mode and zero-downtime reloads, with pm2 startup then pm2 save to survive a reboot.
4 GB for a Node.js API and its PostgreSQL database in their early days, 6 to 8 GB if Next.js is built on the server, since the build alone takes 1 to 2 GB for a few minutes.
Build the new version in its own folder while the old one runs, then switch over. To remove the restart time, run two instances behind Nginx and restart them one after the other, or use pm2 reload in cluster mode.
Simafri
Let's talk about your website
Tell us about your project in a few words: we will get back to you quickly.
Thank you! Your request has been sent. We'll get back to you shortly.
Prefer email? Write to us at support@simafri.com.