Self-host a production Next.js + Payload CMS + MongoDB stack on a single VPS using Coolify. Step-by-step, copy-paste-ready, written from a real production deployment.
Maintained by Digital Vantage. Pairs with the
nextjs-payload-starterrepo, which ships with a Coolify-ready Dockerfile.Prefer prose? The same material is written up as an article: 🇵🇱 Self-hosting z Coolify · 🇬🇧 Self-hosting with Coolify
- What you'll have at the end
- Why self-host with Coolify
- Prerequisites
- Step 1 — Install Coolify on the VPS
- Step 2 — Point a domain at the VPS via Cloudflare
- Step 3 — Add Coolify's own domain (and SSL)
- Step 4 — Create the project + add MongoDB
- Step 5 — Connect your GitHub repo
- Step 6 — Environment variables
- Step 7 — Persistent volume for media uploads
- Step 8 — Deploy
- Step 9 — Flip Cloudflare to proxied + verify SSL
- Step 10 — Auto-deploy on git push
- Database backups
- Troubleshooting
- Security checklist
- Performance tips
- Going further
- Useful links
After ~45 minutes of focused work:
- Next.js 16 + Payload CMS 3 app running on your own VPS, served at your domain over HTTPS.
- MongoDB 8.0 running as a managed Coolify service on the same VPS.
- Automatic redeploys on every
git pushtomain. - Let's Encrypt SSL behind a Cloudflare proxy.
- Persistent media uploads that survive redeploys.
- Daily database backups via Coolify's built-in scheduler.
This guide assumes you start from zero. If you already have Coolify installed, jump to Step 4.
Three reasons most B2B teams pick this path over Vercel + MongoDB Atlas:
| Concern | Vercel + Atlas | Coolify on VPS |
|---|---|---|
| Monthly cost (small) | $20–50 (Vercel Pro) + $9–57 (Atlas M10) = $30–100+ | $5–20 (VPS, all-in) |
| Data sovereignty | US-based providers, GDPR via SCC | Choose your jurisdiction (OVH FR/PL, Hetzner DE, …) |
| Egress / bandwidth | Vercel meters bandwidth, Atlas charges transfer | Most VPS plans include generous transfer (often unmetered) |
| Lock-in | Vendor-specific routing, ISR semantics | Plain Docker containers — move to any other host in an evening |
Trade-off: you own backups, security patching and uptime. If that's not a fair trade, stick with Vercel — there's no shame in not running infra.
This guide is for the case where the trade is worth it: a Polish or EU B2B site that needs predictable costs, data inside the EU, and the freedom to run any side service (queue worker, custom cron, scraper) on the same box.
Before starting you need:
- A VPS (any provider). Recommended minimum: 2 vCPU, 4 GB RAM, 40 GB SSD, Ubuntu 22.04+ or Debian 12+.
- Tested with: OVH VPS Essential / Value, Hetzner CX22, Mikrus 3.0 Pro.
- Coolify itself uses ~1 GB RAM at idle; the rest is for your app + MongoDB. Builds need a transient ~2 GB extra (or swap — see Performance tips).
- A domain name with DNS managed in Cloudflare (free plan is enough). If your domain is on a different registrar, point its nameservers at Cloudflare first — takes 5 minutes.
- A GitHub repository containing your Next.js + Payload app.
- If you don't have one yet, fork
DigitalVantage/nextjs-payload-starter— this guide assumes its layout (Dockerfileat the root, Payload'sdb-mongodbadapter,output: 'standalone'innext.config.ts).
- If you don't have one yet, fork
- SSH access to the VPS as
rootor as a sudoer. Key-based auth strongly preferred — see Security checklist.
SSH into your VPS:
ssh root@<your-vps-ip>Run the official installer:
curl -fsSL https://cdn.coollabs.io/coolify/install.sh | bashWhat this does:
- Detects and installs Docker if missing.
- Pulls the
coollabsio/coolifyimages and starts the dashboard on port8000. - Sets up the internal Docker network and Traefik reverse proxy on ports
80and443. - Generates an initial admin invite URL printed at the end.
When it finishes, the script prints something like:
Your initial setup URL: http://<vps-ip>:8000
Open that in a browser and create the first admin account. Use a strong password — this is the keys-to-the-kingdom UI.
Pitfall #1: if your host blocks outgoing connections by default (rare on OVH/Hetzner, common on locked-down corporate networks), the installer hangs on the Docker pull. From a different terminal, SSH in and run
docker ps— if it returns nothing meaningful for >2 minutes, your firewall is the issue. Allow outbound:443and retry.
In Cloudflare DNS for your domain (example.com), create two A records:
| Type | Name | Content | Proxy status |
|---|---|---|---|
| A | coolify |
<vps-ip> |
DNS only (gray cloud) |
| A | cms (or www, …) |
<vps-ip> |
DNS only for now |
coolify.example.comis the Coolify dashboard itself. Keep it DNS-only permanently — the dashboard doesn't need DDoS protection and Cloudflare proxying interferes with the Let's Encrypt HTTP-01 challenge.cms.example.comis your actual app. Keep it DNS-only until the first SSL cert is issued, then flip to Proxied in Step 9.
Wait ~30 seconds for DNS to propagate, then verify:
dig +short coolify.example.com
dig +short cms.example.com
# Both should return your VPS IPPitfall #2: Cloudflare's "Proxied" mode + Let's Encrypt's HTTP-01 challenge will conflict on first issuance — the challenge request lands on Cloudflare's edge, not your VPS. Use the gray cloud until the cert is issued, then flip to orange. Alternative: switch Coolify to the DNS-01 challenge (more setup but works with proxy from day one).
Back in the Coolify UI:
- Settings → Configuration → Instance Domain: set to
https://coolify.example.com. - Click Save. Coolify auto-generates a Let's Encrypt cert via Traefik.
- Wait ~30 seconds, then reload — the URL bar should now show HTTPS for the dashboard itself.
If the cert doesn't appear, check Servers → localhost → Proxy Logs for Traefik errors. The most common cause is the DNS A record pointing somewhere else (typo, propagation lag).
In Coolify:
- Projects → + New Project, name it e.g.
nextjs-payload-prod. - Pick or create a Server. The local server is auto-created on install — that's the one you want for a single-VPS setup.
- Inside the project, click + New Resource → Database → MongoDB.
- Pick MongoDB 8.0. Set:
- Username:
payload - Password: click "Generate" (Coolify produces a strong random one — copy it now or you'll have to look it up later)
- Database name:
payload
- Username:
- Click Deploy. After ~30s the database is up.
Pin the minor version. Pick
8.0, not8orlatest. MongoDB ships Rapid Releases (8.1, 8.2, 8.3) alongside the supported8.0line, and the floating8tag does not reliably track8.0— at the time of writing it points at an older digest than8.0itself. Rapid Releases are also short-lived: 8.2 reached end-of-life in July 2026.8.0is the line that keeps getting security patches.
Coolify shows a Connection String in the resource view, looking like:
mongodb://payload:<password>@<container-name>:27017/payload?authSource=admin
Copy this — you'll paste it as DATABASE_URL in Step 6.
Note: the host part is the Docker container name. Coolify auto-resolves it for any other resource in the same project — no need to expose port
27017externally. Don't expose27017to the public unless you have a real reason to (and even then, use TLS + IP allow-listing).
-
Coolify → Sources → + Add → GitHub App. Install the Coolify GitHub App on your account or organization and grant it access to the repo. This sets up the webhook and OAuth flow in one step.
-
Inside the project, click + New Resource → Application.
-
Pick the GitHub source you just added, choose your repo, branch
main. -
Build Pack: Dockerfile (the starter ships one at the project root).
-
Set Domain to
https://cms.example.com. -
Set Port Exposes to
3000(Next.js standalone server). -
Don't deploy yet — set environment variables first.
In the application's Environment Variables tab, add:
DATABASE_URL=mongodb://payload:<password>@<mongo-container>:27017/payload?authSource=admin
PAYLOAD_SECRET=<output of: openssl rand -base64 32>
NEXT_PUBLIC_SERVER_URL=https://cms.example.com
CRON_SECRET=<output of: openssl rand -hex 32>
PREVIEW_SECRET=<output of: openssl rand -hex 32>
NODE_ENV=productionGenerate the secrets locally on your laptop:
openssl rand -base64 32 # PAYLOAD_SECRET — used to sign JWTs
openssl rand -hex 32 # CRON_SECRET — used by Payload's job runner
openssl rand -hex 32 # PREVIEW_SECRET — validates draft preview URLsMark PAYLOAD_SECRET, CRON_SECRET, PREVIEW_SECRET as Secret in Coolify (the lock icon) so they're hidden from build logs and the UI's edit view.
Pitfall #3:
NEXT_PUBLIC_*vars are baked into the client JS bundle at build time, not runtime. ChangingNEXT_PUBLIC_SERVER_URLrequires a rebuild, not just a restart. Coolify shows a "Build Variable" vs "Runtime Variable" toggle next to each env var — setNEXT_PUBLIC_*ones to "Build Variable" so they're available duringnext build. Everything else stays as a runtime variable.
Payload writes uploaded images to public/media/ by default. Without a persistent volume, every redeploy wipes the directory.
In the application config → Storages → + Add Persistent Storage:
- Mount Path:
/app/public/media - Type: Volume (not bind mount — bind mounts tie you to a specific host path)
- Volume Name:
payload-media(Coolify creates and persists it under/var/lib/docker/volumes/)
Note: for production at any meaningful scale, swap the local volume for S3-compatible object storage (Hetzner Object Storage, Cloudflare R2, MinIO on a separate VPS, AWS S3). The local-volume approach is fine up to a few GB of uploads — beyond that, restoring from backup gets painful. See Going further.
Click Deploy. Watch the logs in real time:
- Pulling repository — Coolify clones your branch from GitHub.
- Building Dockerfile — runs
pnpm install --frozen-lockfile, thenpnpm build. - Starting container —
node server.js(Next.js standalone entry point). - Health check passes — Traefik adds the route and starts serving traffic at
cms.example.com.
The first build takes 5–10 minutes with no Docker layer cache. Subsequent builds reuse layers and finish in 1–2 minutes.
Once the deploy is green, hit https://cms.example.com/admin and create the first Payload admin user. Don't lose this password — there's no "forgot password" flow without an email adapter configured (see Going further).
Once https://cms.example.com works with the gray cloud (DNS-only), switch the Cloudflare A record proxy status to Proxied (orange cloud). You now get:
- DDoS protection and bot mitigation at Cloudflare's edge.
- Free unmetered bandwidth (egress through Cloudflare doesn't count against your VPS plan).
- HTTP/3, Brotli compression, automatic IPv6.
- Edge caching for
/_next/static/*(configurable via Cloudflare Cache Rules). Page Rules are deprecated and are not offered at all on Free zones created after 2024-07-01 — use Cache Rules.
In Cloudflare → SSL/TLS → Overview, set the encryption mode to Full (strict). This tells Cloudflare to validate the origin cert (the Let's Encrypt one Coolify just issued) — anything weaker leaves you vulnerable to MITM between Cloudflare and your VPS.
Verify end-to-end:
curl -sI https://cms.example.com | head -5
# Expect: HTTP/2 200, server: cloudflareCoolify already configured a webhook when you connected the repo in Step 5. Test it:
git commit --allow-empty -m "test deploy"
git push origin mainWithin 5–10 seconds the Coolify dashboard shows a new deployment running. The webhook delivery and logs are visible in GitHub → repo → Settings → Webhooks → Recent Deliveries if you ever need to debug.
Coolify has built-in scheduled backups. In the MongoDB resource → Backups tab:
- Frequency:
Daily, 02:00server time (cron:0 2 * * *) - Retention:
7 daysfor hot backups; longer if your storage permits. - Storage: Local volume by default. For real disaster recovery, configure an S3 destination (Coolify supports it natively) so a single VPS loss doesn't take backups down with the data.
For an ad-hoc manual backup. Coolify starts MongoDB with authentication enabled, so
mongodump needs credentials — without them it fails with Authentication failed.
Read them from the container's own environment so the password never lands in your
shell history:
docker exec <mongo-container> sh -c 'mongodump --archive --gzip \
-u "$MONGO_INITDB_ROOT_USERNAME" \
-p "$MONGO_INITDB_ROOT_PASSWORD" \
--authenticationDatabase admin' > backup-$(date +%F).gzTo restore:
gunzip -c backup-2026-05-07.gz | docker exec -i <mongo-container> sh -c 'mongorestore --archive --drop \
-u "$MONGO_INITDB_ROOT_USERNAME" \
-p "$MONGO_INITDB_ROOT_PASSWORD" \
--authenticationDatabase admin'Note:
--dropwipes each collection before restoring it. Rehearse on a throwaway database name first, not on the live one.Test the restore at least once before you need it. A backup you've never restored is barely a backup.
The five errors you're statistically likely to hit, in roughly the order people hit them.
Two different problems produce this, and they need different fixes.
The containers aren't on the same Docker network. Coolify auto-resolves a database's container name only for resources inside the same project. If the app and the MongoDB resource live in different projects, the hostname never resolves and every connection is refused, cold start or not. Simplest fix: move both into one project.
MongoDB genuinely wasn't ready yet. Coolify has no depends_on for Dockerfile-based
applications — that setting exists only for Docker Compose resources — so start ordering
isn't something you can configure away here. In practice the mongoose driver already
retries for serverSelectionTimeoutMS (30 s by default), which absorbs a normal Mongo
cold start. If your app still loses the race, deploy the MongoDB resource first, wait for
it to report healthy, then deploy the app.
You forgot to set DATABASE_URL, or there's a typo in it. With no connection string,
the mongoose adapter falls back to its built-in default of 127.0.0.1:27017, which
inside the app container resolves to nothing. Triple-check the env var — the connection
string from Coolify's MongoDB resource view is the canonical source.
Next.js production build briefly needs 2–3 GB RAM. On a 4 GB VPS with MongoDB also running, the build can OOM. Two fixes:
-
Add swap (cheap, works immediately):
fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile echo '/swapfile none swap sw 0 0' >> /etc/fstab
-
Upgrade the VPS to 8 GB RAM if your traffic justifies it.
Triggered by repeated DNS or domain config errors during initial setup. Coolify uses LE production certs by default.
The limit you hit here is "New Certificates per Exact Set of Identifiers" — 5 certificates per 7 days for the same set of hostnames, which is exactly what retrying cms.example.com over and over does. The roomier limit of 50 certificates per registered domain per 7 days is almost certainly not your problem.
If you hit it, you have two options:
- Wait ~34 hours. Capacity refills continuously — one duplicate certificate every 34 hours — so there is no weekly reset to sit out.
- Configure the DNS-01 challenge with a wildcard cert — covers all subdomains, only counts as one certificate. Worth doing anyway if you plan multiple environments (
staging.example.com,preview.example.com, …).
Cloudflare's free plan times out at 100 seconds. Long Payload imports, migrations, or heavy GraphQL queries can exceed that. Two workarounds:
-
For one-off admin tasks, skip the proxy and work directly against the VPS.
Careful: the
output: 'standalone'runtime image contains onlyserver.js,.next/static,publicand the dependencies Next.js traced — there is nopnpmand nopayloadCLI, sodocker exec <app-container> pnpm payload migratefails withpnpm: not found.Run Payload migrations from a full local checkout instead, tunnelling to the database over SSH:
# 1. find the Mongo container's address on the Docker network ssh root@<vps-ip> "docker inspect -f \ '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <mongo-container>" # 2. forward it to your laptop and leave this running ssh -L 27017:<mongo-ip>:27017 root@<vps-ip> # 3. in a second terminal, run against the tunnel DATABASE_URL='mongodb://payload:<password>@127.0.0.1:27017/payload?authSource=admin' \ pnpm payload migrate
With the MongoDB adapter most schema changes need no migration at all — Payload writes documents in the new shape and reads tolerate the old one. Reach for migrations when you need a data transform, not a schema one.
-
For app-level long requests, move the work to a Payload job/queue (
payload.jobs.queue(...)) and return immediately — Cloudflare-friendly by design.
Before sharing the URL publicly:
-
UFW or iptables: allow only
22,80,443inbound. -
Disable root SSH login, use key-based auth only:
# /etc/ssh/sshd_config PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes -
fail2banfor SSH brute-force protection. -
Coolify dashboard behind a strong password + 2FA enabled.
-
PAYLOAD_SECRET≥ 32 bytes, never reused across environments. -
MongoDB exposed only on the internal Docker network, not on the host.
-
Cloudflare WAF rule to block direct access to the origin IP (or set up an IP allow-list at the firewall level — only Cloudflare's IP ranges on
:443). -
Automated daily backups, with at least one test restore documented.
-
Rotate secrets every 90 days (
PAYLOAD_SECRET, DB password, API tokens). -
Restrict
corsinpayload.config.tsto your real production origins — the starter usesgetServerSideURL()which readsNEXT_PUBLIC_SERVER_URL, so this is correct as long as the env var is right.
- Enable swap on a small VPS even if you're not currently OOMing — Linux makes intelligent use of it for caching.
- Cloudflare cache rules for static assets:
- URL pattern
*/_next/static/*→ Cache TTL: 1 year, Edge TTL: 1 month. - URL pattern
*/api/*→ Bypass cache.
- URL pattern
- Next.js
output: 'standalone'(already set in the starter) — produces the smallest possible production image, ~150 MB vs ~600 MB for the default. - MongoDB indexes are auto-created by Payload for collection-level queries, but verify with
db.<collection>.getIndexes()inmongoshafter adding new fields with frequent queries. docker system prune --volumes -fmonthly to reclaim disk from old images and dangling layers — Coolify keeps the last 3 builds by default but old images add up.- Image optimization: serve uploads through Next.js
<Image>(already wired in the starter vianext/image+ Payload'sgetURLhelper) — Cloudflare caches the optimized variants automatically.
- Object storage for media — swap the local volume for S3-compatible storage. Payload's storage adapters wire this up in ~10 lines of config —
@payloadcms/storage-s3covers every S3-compatible provider. Recommended providers: Hetzner Object Storage (€1/TB), Cloudflare R2 (free egress), Backblaze B2. - Transactional email — plug Resend, Postmark or SendGrid into Payload's
emailconfig (any nodemailer-compatible adapter works). Without this, password reset / forgot-password flows are unavailable. - Multi-environment — clone the application in Coolify pointing at a
developbranch withstaging.example.com. Use the same MongoDB resource with a different database name (payload_staging). - Monitoring — Coolify integrates with Plausible / Umami for traffic; for app-level errors, plug Sentry into Next.js (
@sentry/nextjs). - CI before deploy — the
nextjs-payload-starterships GitHub Actions that run lint + typecheck + tests + build on every PR; combined with branch protection, that means broken main never reaches Coolify.
- Starter repo (the code half of this guide): https://github.com/DigitalVantage/nextjs-payload-starter
- Article version (PL): https://www.digitalvantage.pl/artykuly/strony-internetowe/technologie/self-hosting-coolify
- Article version (EN): https://www.digitalvantage.eu/posts/websites-internet/technologies/self-hosting-coolify
- Coolify docs: https://coolify.io/docs
- Payload CMS docs: https://payloadcms.com/docs
- Next.js docs: https://nextjs.org/docs
- OVH VPS plans: https://www.ovhcloud.com/en/vps/
- Hetzner Cloud: https://www.hetzner.com/cloud
Spotted an error, an outdated screenshot, a missing pitfall? Open an issue or PR. See CONTRIBUTING.md for the workflow.
MIT — Copyright © 2026 Digital Vantage.
Use it, fork it, translate it, republish it — commercially too. The licence asks for one thing back: keep the copyright notice with the copy, so readers can trace the guide to its source. The guide's Markdown files carry that notice as a comment at the top; if you republish somewhere that strips comments, paste this line instead:
Coolify Deployment Guide — © 2026 Digital Vantage — https://www.digitalvantage.pl/go/git — MIT
Digital Vantage — partnerstwo technologiczne dla firm B2B w Polsce.
- Website: digitalvantage.pl (PL) · digitalvantage.eu (EN)
- Email: kontakt@digitalvantage.pl
- GitHub: @DigitalVantage
If you need a partner to deploy, scale or maintain a Payload + Next.js platform — get in touch.