How to Deploy a Next.js App from GitHub, Step by Step
Learn how to deploy a Next.js app from GitHub step by step: production builds, environment variables, Git-based hosting, a VPS with Nginx, and auto-deploys.
By Devma Hosting Team · · 8 min read
To deploy a Next.js app from GitHub, push your code to a repository, connect it to a host that can run Node.js, add your environment variables, and have the host run npm run build and npm run start on every push. You can do that on a Git-connected app hosting platform, or on your own VPS using Nginx and a GitHub Actions workflow. Both routes support every Next.js feature, because Next.js runs as a standard Node.js server.
Key takeaways
- Next.js 16 needs Node.js 20.9 or later. Use a current LTS release, and keep the same version locally, in CI and in production.
- A Node.js server or Docker container supports all Next.js features. A static export supports only a subset.
NEXT_PUBLIC_variables are baked in at build time. Change one and you need to rebuild.output: 'standalone'produces a small, self-contained server that's ideal for Docker and VPS deploys.- When you self-host, put a reverse proxy such as Nginx in front, and turn off response buffering so streaming works.
What do you need before you deploy a Next.js app?
Check these five things first:
-
A GitHub repository containing your app.
-
Build and start scripts in
package.json.create-next-appadds them for you:"build": "next build"and"start": "next start". -
A committed lockfile (
package-lock.json,pnpm-lock.yamloryarn.lock), so the server installs exactly what you tested. -
A pinned Node.js version. The Next.js installation docs list 20.9 as the minimum. The Node.js release page shows Node.js 20 is now end-of-life and recommends Active or Maintenance LTS releases for production. A
.nvmrcfile makes your choice explicit:echo "24" > .nvmrc -
No secrets in Git. The default
create-next-apptemplate adds.env*files to.gitignore. Keep it that way, and set production values in your host's dashboard or on the server instead.
Step 1: Test a production build locally
Many deployment failures show up locally if you build the way the server will:
npm ci
npm run build
npm run start
Open http://localhost:3000 and click through the app. Fix any build errors here, where they're cheapest to debug. As of Next.js 16, next build no longer runs your linter, so run ESLint or Biome as a separate step.
Step 2: Push your code to GitHub
If the project isn't on GitHub yet:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin git@github.com:your-username/my-app.git
git push -u origin main
Double-check that .next/, node_modules/ and any .env*.local files didn't sneak into the commit.
Step 3: Choose how to deploy your Next.js app
The Next.js deployment docs list four approaches:
| Method | Next.js features | Effort | Best for |
|---|---|---|---|
| Git-connected app hosting | All, when the platform runs next start or Docker |
Low | Most apps and teams |
| Your own VPS (Node.js and Nginx) | All | Medium to high | Full control, custom services |
| Docker container | All | Medium | Teams already using containers |
Static export (output: 'export') |
Limited, no server features | Low | Marketing sites and single-page apps |
If you're unsure, start with a Git-connected platform. You can always move to a VPS later. Our VPS vs shared hosting comparison covers when that move makes sense.
Step 4, option A: Deploy with a Git-connected hosting platform
Most app hosting platforms follow the same flow:
- Connect GitHub. Authorize the platform's GitHub app, and grant it access to only the repositories it needs.
- Pick the repository and branch. Usually
mainfor production. - Confirm the commands. Build command
npm run build, start commandnpm run start. The platform should detect these automatically. - Set the Node.js version to match your
.nvmrc. - Add environment variables. Add the
NEXT_PUBLIC_ones before the first build, since they're inlined at build time. - Deploy, then add your domain. Point your DNS at the platform and make sure HTTPS is active before you share the URL.
From then on, every push to main triggers a new build and deploy. Many platforms also create preview deployments for pull requests.
Step 4, option B: Deploy to your own VPS
This route takes longer to set up, but you control everything. The example assumes Ubuntu, a non-root user called deploy, and Node.js, Git and Nginx already installed.
Give the server read-only access with a deploy key
On the server, create a key that only this server uses:
ssh-keygen -t ed25519 -C "deploy@my-app" -f ~/.ssh/my_app_deploy -N ""
cat ~/.ssh/my_app_deploy.pub
In GitHub, open the repository's Settings → Deploy keys, choose Add deploy key and paste the public key. GitHub's docs note that deploy keys are read-only by default and grant access to a single repository. That's exactly what a production server needs. Then clone the repository:
sudo mkdir -p /var/www/my-app && sudo chown deploy:deploy /var/www/my-app
GIT_SSH_COMMAND="ssh -i ~/.ssh/my_app_deploy" git clone git@github.com:your-username/my-app.git /var/www/my-app
cd /var/www/my-app
git config core.sshCommand "ssh -i ~/.ssh/my_app_deploy"
Create /var/www/my-app/.env.production with your production values. Next.js loads it automatically for both next build and next start. Then build once with npm ci && npm run build.
Run Next.js as a systemd service
Create /etc/systemd/system/my-app.service:
[Unit]
Description=My Next.js app
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/var/www/my-app
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/var/www/my-app/node_modules/.bin/next start -H 127.0.0.1
Restart=on-failure
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target
Binding to 127.0.0.1 means only Nginx can reach the app. If you installed Node.js with nvm rather than a system package, make sure the node binary is on systemd's PATH, or call it by its full path. When systemd stops the service it sends SIGTERM. The self-hosting guide says Next.js then finishes in-flight requests, and recommends allowing a 10–30 second drain period. Start the service with:
sudo systemctl daemon-reload
sudo systemctl enable --now my-app
sudo systemctl status my-app
Put Nginx in front of Next.js
The Next.js docs recommend a reverse proxy rather than exposing the Node.js server directly. A proxy can absorb malformed requests, slow connections and oversized payloads before they reach your app. Create a site config:
server {
listen 80;
server_name example.com www.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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Let streamed responses (Suspense, loading.js) reach the browser right away
proxy_buffering off;
}
}
Without that last line, Nginx buffers streamed responses and your loading states arrive all at once. Reload Nginx and add a free Let's Encrypt certificate with Certbot:
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d example.com -d www.example.com
Step 5: Deploy automatically on every push with GitHub Actions
First, add a deploy script on the server at /var/www/my-app/deploy.sh, and make it executable with chmod +x:
#!/usr/bin/env bash
set -euo pipefail
cd /var/www/my-app
git fetch origin main
git reset --hard origin/main
npm ci
npm run build
sudo systemctl restart my-app
Don't export NODE_ENV=production in this script. With it set, npm ci skips devDependencies, and your build tools, such as TypeScript or Tailwind, would be missing. To let the deploy user restart only this one service without a password, run sudo visudo -f /etc/sudoers.d/my-app and add:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart my-app
Next, add three repository secrets under Settings → Secrets and variables → Actions:
DEPLOY_SSH_KEY: a private key whose public half is in thedeployuser's~/.ssh/authorized_keysDEPLOY_KNOWN_HOSTS: your server's host key line, so the runner can verify the serverDEPLOY_HOST: the server's hostname or IP
Then create .github/workflows/deploy.yml:
name: Deploy
on:
push:
branches: [main]
concurrency: production-deploy
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version-file: .nvmrc
- run: npm ci
- run: npm run lint --if-present
- run: npm run build
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Run the deploy script on the server
env:
SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
HOST: ${{ secrets.DEPLOY_HOST }}
run: |
mkdir -p ~/.ssh
echo "$SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
echo "$KNOWN_HOSTS" > ~/.ssh/known_hosts
ssh deploy@"$HOST" /var/www/my-app/deploy.sh
The build job catches broken builds before they reach production, and concurrency stops two deploys from running at once. If your build needs NEXT_PUBLIC_ values, add them to the workflow as Actions variables too. Restarting a single instance causes a brief gap while the server comes back up. For true zero-downtime deploys, run two instances behind Nginx and restart them one at a time.
Option C: Deploy Next.js with Docker
For containers, enable standalone output in next.config.ts:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
output: 'standalone',
}
export default nextConfig
According to the output docs, next build then creates .next/standalone with a minimal server.js and only the dependencies your app needs. The public and .next/static folders aren't copied automatically, so add them yourself:
npm run build
cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/
PORT=3000 HOSTNAME=0.0.0.0 node .next/standalone/server.js
The official with-docker example wraps this in a multi-stage Dockerfile that runs as the non-root node user and exposes port 3000. It's the best starting point for your own image.
How do environment variables work in production?
This catches out almost everyone once, so it's worth spelling out (Next.js docs):
| Variable type | Where it's available | When its value is read |
|---|---|---|
Regular (e.g. DATABASE_URL) |
Server only | At runtime for dynamically rendered code. At build time for statically prerendered pages |
NEXT_PUBLIC_ prefixed |
Server and browser | At build time. The value is inlined into the JavaScript bundle |
Next.js checks process.env first, then .env.production.local, .env.local, .env.production and .env, and stops at the first match. If you build one Docker image and promote it through staging and production, keep environment-specific values server-side, because NEXT_PUBLIC_ values will be frozen in the image.
Common Next.js deployment problems and fixes
- Builds locally but fails on the server: check that Node.js versions match, and that no environment variables are missing. Also check import paths for case: macOS usually ignores filename case and Linux doesn't.
- "Failed to find Server Action" with multiple instances: every instance needs the same
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY, set at build time, as described in the self-hosting guide. - Loading states don't stream: turn off proxy buffering, as shown above.
- The build runs out of memory on a small VPS: add swap, or build in CI and copy the standalone output to the server.
- A deprecation warning about
middleware.ts: Next.js 16 renamed the convention toproxy.ts. See the upgrade guide for edge-runtime caveats.
Once you're live, run your domain through the SSL checker to confirm your certificate chain is valid. Use the DNS lookup tool to confirm your records point where you expect.
Deploying Next.js on Devma App Hosting
If you'd rather skip the server setup, Devma App Hosting deploys React, Next.js, Node.js, Laravel, Python and Docker apps straight from your Git repository, from $3/month. Push to your branch and it builds and runs the app for you. If you want the full-control route from this guide, our KVM VPS plans with NVMe storage start at $6.99/month.
Frequently asked questions
What Node.js version do I need to deploy Next.js?
Next.js 16 requires Node.js 20.9 or later. Node.js 20 has since reached end of life, so deploy on a current LTS release instead, and use the same major version locally, in CI and on your server.
Do I need Vercel to deploy a Next.js app?
No. The Next.js documentation says any provider that supports Node.js or Docker can run a Next.js app with all of its features. You run next build, then next start, ideally behind a reverse proxy such as Nginx.
Why don't my NEXT_PUBLIC_ environment variables update after I change them?
Variables prefixed with NEXT_PUBLIC_ are inlined into the JavaScript bundle when you run next build, so their values are frozen at build time. Change the value in your host's settings, then trigger a new build and deploy.
Can I deploy a Next.js app as a static site?
Yes, if your app doesn't need server features. Setting output to 'export' generates static HTML, CSS and JavaScript you can host on any web server. Features that need a server, such as Server Actions, cookies, redirects, proxy (formerly middleware), Incremental Static Regeneration and default image optimization, won't work in a static export.
What does output: 'standalone' do in Next.js?
It makes next build create a .next/standalone folder containing a minimal server.js and only the node_modules files your app needs. It's ideal for Docker images and VPS deploys. You must copy the public and .next/static folders into it yourself, or serve them from a CDN.
