How to Deploy a Next.js App and API with Coolify on Your Own VPS
Step-by-step guide to deploying a Next.js frontend and Node.js API with Coolify, Docker and Cloudflare on one VPS: env vars, SSL, backups and rollbacks.
Why self-host with Coolify
Managed platforms are convenient, but costs climb as soon as you add a backend, a database and several environments. Coolify gives you push-to-deploy on a VPS you control, using Docker under the hood. You keep the Git workflow your team already knows, and the monthly bill is mostly the price of the server.
This guide covers the setup I use for a typical product: a Next.js frontend, a separate Node.js API, a database, and Cloudflare in front. It is the same shape as the AEPS payments dashboard, where the dashboard and backend run as separate services deployed through Coolify.
1. Prepare the server
Start with a fresh Ubuntu VPS with at least 2 GB of RAM. Point a domain at it through Cloudflare, and keep the proxy off (grey cloud) until SSL is issued.
Before installing anything, do the basics: create a non-root user with SSH key login, disable password login, and turn on a firewall that allows only SSH, HTTP and HTTPS. If the server has 2–4 GB of RAM, add a swap file. Next.js builds are memory-hungry, and swap is the difference between a slow build and a failed one.
2. Install Coolify
Install Coolify with its official install script, open the dashboard on port 8000, create your admin account, and connect your GitHub account or add a deploy key for private repositories.
Do this step right after installing, because until the admin account exists anyone who finds the address can claim it. Then give the dashboard its own domain (for example coolify.yourdomain.com) with SSL, and close port 8000 in the firewall so the panel is only reachable over HTTPS.
3. Deploy the Next.js frontend
Create a new resource from your repository and choose the Nixpacks or Dockerfile build pack. Set the port to 3000, add environment variables such as NEXT_PUBLIC_API_URL, and attach your domain. Coolify requests the SSL certificate for you.
If you write your own Dockerfile, set output: "standalone" in next.config.js. Next.js then produces a small self-contained server in .next/standalone, so the final image only needs that folder plus public and .next/static. Images get much smaller and deploys get faster.
4. Get environment variables right
This is where most first deployments go wrong. Variables that start with NEXT_PUBLIC_ are written into the JavaScript bundle at build time, not read when the server starts. If you change one in Coolify, you must rebuild, not just restart, and the variable has to be available during the build (Coolify lets you mark a variable as a build variable).
Server-only secrets such as database passwords and API keys should never start with NEXT_PUBLIC_, because anything with that prefix is visible to every visitor. Keep a separate set of variables for staging and production, and never commit a .env file to the repository.
5. Deploy the API as a separate service
Keep the backend in its own repository or folder and deploy it as a second resource on an api subdomain. Add your database credentials as environment variables, never in the code.
Because the frontend and API now live on different subdomains, the API must send CORS headers that allow your frontend domain, and nothing broader. Running them as separate services also means you can redeploy the API without touching the frontend, which matters when a single API also serves a mobile app.
6. Add the database and backups
Coolify can run MySQL, PostgreSQL or Redis as resources on the same server. Keep the database port closed to the internet and connect to it from the API over the internal Docker network. Then set up scheduled backups to S3-compatible storage outside the server. A backup stored on the same VPS will not help you on the day that VPS fails, and it is worth restoring one backup to a test database once, so you know the process works before you need it.
7. Health checks, auto-deploy and rollbacks
Turn on automatic deployment so a push to your main branch builds and ships the app. Add a health check for each service, such as a simple /health endpoint on the API. With a health check in place, Coolify can wait until the new container is healthy before it switches traffic, so a broken build does not replace a working one.
If a release still goes wrong, Coolify keeps earlier deployments, so you can roll back to the previous version from the dashboard while you fix the bug. That is far calmer than rebuilding an old commit under pressure.
8. Wildcard subdomains for multi-tenant apps
If every customer needs their own subdomain, add a wildcard DNS record in Cloudflare and configure the wildcard domain on the frontend resource. Read the subdomain in your app to load the right tenant. I used this approach in a multi-tenant server setup where each customer gets their own address on one server.
9. Turn on the Cloudflare proxy
Once your certificates are issued and the sites load over HTTPS, switch the DNS records to proxied (orange cloud) and set Cloudflare's SSL/TLS mode to Full (strict). Avoid the Flexible mode: it sends traffic to your server over plain HTTP, and combined with an HTTPS redirect on the server it causes an endless redirect loop.
Common problems
Builds failing on memory usually mean the VPS is too small or needs swap. A 502 after deploy usually means the app listens on a different port than the one configured. SSL errors usually mean the Cloudflare proxy was on before the certificate was issued. A frontend still calling the old API address after you changed it means a NEXT_PUBLIC_ variable was changed without a rebuild.
Go-live checklist
Before you send real users to the new server, check that: the Coolify dashboard is behind HTTPS and port 8000 is closed; secrets are in environment variables, not in Git; the database is not exposed publicly; off-server backups run and one restore has been tested; health checks are set for every service; the Cloudflare SSL mode is Full (strict); and someone on your team knows how to roll back.
Need this set up for your product? I do this regularly for clients and can have your app live on your own server. See my deployment and server setup service for what is included.
Case studies using this approach
AEPS & payments dashboard
Platform-owner dashboard and backend for a payments product, containerised and deployed with Coolify.
Read case study →InfrastructureMulti-tenant server setup
Wildcard subdomains behind Cloudflare, with frontend and backend apps deployed through Coolify.
Read case study →Hire me for this
Deployment & DevOps
A finished app that isn't live earns nothing. I set up servers, containers, domains and SSL so your product runs on…
Learn more →ServiceNext.js & React development
I build fast, SEO-friendly web applications with Next.js, React and TypeScript: SaaS products, client portals, admin dashboards…
Learn more →