Loading…
Loading documentation...
Build and run a production Next.js App Router application under systemd with Caddy automatic HTTPS and firewall rules on a Linux VPS.
Commands checked on 2026-11-12 with Ubuntu 24.04 LTS, Node.js 24 LTS, Next.js 16, Caddy 2.8.
Your Next.js application will run as an unprivileged systemd service bound to 127.0.0.1:3000 on your Servers Are Up Linux VPS, fronted by Caddy with automatic Let's Encrypt TLS certificates (HTTPS + HTTP/3), structured journald logs, automatic restart on failure, and perimeter firewall rules permitting only ports 80 and 443.
/dashboard/instances). If you have not created one yet, complete Order your first Linux server and sign in with SSH.app.example.com) whose DNS records you can edit, or a zone hosted in Network > Cloud DNS zones (/dashboard/cloud-dns).output: 'standalone' in next.config.mjs (recommended for compact, reproducible server deployments).203.0.113.25) from Compute > Servers (/dashboard/instances)./dashboard/cloud-dns), create an A record for app.example.com pointing to 203.0.113.25 with a 300 second TTL./dashboard/network?tab=firewall) and enable inbound TCP 80 (for HTTP-01 ACME verification and HTTPS redirects) and TCP/UDP 443 (for TLS 1.3 and HTTP/3) to your server. Keep port 3000 closed to the public internet.Sign in to your server over SSH or the browser console and install Node.js 24 LTS alongside the official Caddy package:
sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg git debian-keyring debian-archive-keyring apt-transport-https
curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt-get update && sudo apt-get install -y nodejs caddy
sudo useradd --system --create-home --home-dir /srv/nextjs --shell /usr/sbin/nologin nextapp
Deploy your application files into /srv/nextjs/app and store runtime environment variables in a root-owned /etc/nextjs/app.env file readable only by nextapp:
sudo install -d -m 0750 -o root -g nextapp /etc/nextjs
sudo tee /etc/nextjs/app.env > /dev/null <<'EOF'
NODE_ENV=production
PORT=3000
HOSTNAME=127.0.0.1
NEXT_TELEMETRY_DISABLED=1
APP_URL=https://app.example.com
EOF
sudo chmod 0640 /etc/nextjs/app.env
sudo -u nextapp -s /bin/bash -c '
git clone https://github.com/your-org/your-nextjs-app.git /srv/nextjs/app
cd /srv/nextjs/app
npm ci
npm run build
cp -r public .next/standalone/public
cp -r .next/static .next/standalone/.next/static
'
Create /etc/systemd/system/nextjs-app.service so systemd supervises the Node.js process, restarts it automatically if it crashes, and isolates the filesystem:
sudo tee /etc/systemd/system/nextjs-app.service > /dev/null <<'EOF'
[Unit]
Description=Next.js Production Server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=nextapp
Group=nextapp
WorkingDirectory=/srv/nextjs/app/.next/standalone
EnvironmentFile=/etc/nextjs/app.env
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ReadWritePaths=/srv/nextjs/app/.next/cache
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now nextjs-app
Replace /etc/caddy/Caddyfile with a reverse-proxy block for app.example.com that forwards traffic to 127.0.0.1:3000 and adds strict transport headers:
sudo tee /etc/caddy/Caddyfile > /dev/null <<'EOF'
app.example.com {
encode zstd gzip
header {
Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
-Server
}
reverse_proxy 127.0.0.1:3000 {
health_uri /api/health
health_interval 15s
}
}
EOF
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
Verify both the local loopback listener on the server and the public TLS endpoint from outside:
sudo systemctl status nextjs-app --no-pager
curl -fsSI http://127.0.0.1:3000 | head -n 5
curl -fsSI https://app.example.com | grep -E "HTTP/|strict-transport-security"
You should see HTTP/2 200 alongside the strict-transport-security header, and sudo journalctl -u nextjs-app -n 30 will display clean startup output without errors.
solving challenges: dial tcp ... i/o timeout when issuing the certificate. Either DNS for app.example.com has not propagated to 203.0.113.25 yet (dig +short app.example.com), or inbound TCP port 80 is still closed in Network > Firewall (/dashboard/network?tab=firewall).404 Not Found in standalone mode. Next.js standalone builds do not copy public/ or .next/static/ automatically; run the two cp -r commands in Step 3 on every release.502 Bad Gateway from Caddy immediately after deploy. Check sudo journalctl -u nextjs-app -n 50 --no-pager. Usually a required variable in /etc/nextjs/app.env is missing or HOSTNAME was bound to an unreachable interface instead of 127.0.0.1./dashboard/key-vault) and render /etc/nextjs/app.env during CI/CD rather than storing secrets in Git or shell history./srv/nextjs/releases/20261112-1) and flip a /srv/nextjs/current symlink before running sudo systemctl restart nextjs-app so rollbacks take less than two seconds./dashboard/instances before major Node.js or OS upgrades.Every Servers Are Up virtual machine and container includes an integrated HTML5 browser-based SSH, RDP, and VNC console in your dashboard, default-off inbound firewall rules with per-IP allow-lists, and full programmatic control via REST API v1 and Model Context Protocol (MCP). Explore our Virtual Datacenter Plans or reach out to Servers Are Up Support.