Loading…
Loading documentation...
Build a Next.js 16 app in standalone mode and run it under systemd so it starts at boot, restarts on failure and logs to the journal.
Commands checked on 2026-10-27 with Next.js 16, Node.js 24, Ubuntu 24.04, systemd 255.
A Next.js app running on your server as a systemd service, under a user of its own that can write nowhere but the app's cache. It starts when the server boots, comes back by itself within seconds if it crashes, and writes its output to the journal, where journalctl reads it.
sudo, set up as in Set up a fresh Ubuntu 24.04 server safely./srv/nextapp and listens on port 3000 on the server itself; a web server in front of it (Nginx or Caddy) takes the public traffic.Ubuntu 24.04 ships Node.js 18, which Next.js 16 does not support. NodeSource publishes Node.js 24 as an apt repository. Download its setup script, read it if you like, then run it.
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
node --version
The last command prints a version that starts with v24..
If you already have an app, skip to step 3 with its source in a folder on the server. Otherwise this makes the standard example, answering every question with its default:
npx --yes create-next-app@16 nextapp --ts --eslint --app --use-npm --yes
cd nextapp
Standalone mode puts a minimal server and only the packages it needs in .next/standalone, so the server does not need the whole node_modules folder. Add the setting to the config, then build.
cat > next.config.ts <<'EOF'
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;
EOF
npm run build
ls .next/standalone
The build ends with a table of routes, and .next/standalone holds server.js, package.json, a node_modules folder and a .next folder.
The standalone server does not copy the static files or the public folder; you copy them next to it (leave out the public line if your app has no such folder). The app runs as a system user that owns nothing but its cache folder.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin nextapp
sudo install -d -m 755 /srv/nextapp
sudo cp -a .next/standalone/. /srv/nextapp/
sudo cp -a .next/static /srv/nextapp/.next/static
sudo cp -a public /srv/nextapp/public
sudo install -d -m 750 -o nextapp -g nextapp /srv/nextapp/.next/cache
sudo chown -R root:root /srv/nextapp
sudo chown nextapp:nextapp /srv/nextapp/.next/cache
The unit names the user, the folder, the command and the environment, says to restart on failure, and takes away the rights the app does not need: it cannot write outside its cache folder, read home folders or gain new privileges.
sudo tee /etc/systemd/system/nextapp.service > /dev/null <<'EOF'
[Unit]
Description=Next.js app
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=nextapp
Group=nextapp
WorkingDirectory=/srv/nextapp
Environment=NODE_ENV=production
Environment=PORT=3000
Environment=HOSTNAME=127.0.0.1
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
ReadWritePaths=/srv/nextapp/.next/cache
[Install]
WantedBy=multi-user.target
EOF
sudo systemd-analyze verify /etc/systemd/system/nextapp.service
systemd-analyze verify prints nothing when the unit is valid.
enable --now starts the service and links it into the boot sequence in one command.
sudo systemctl daemon-reload
sudo systemctl enable --now nextapp
systemctl is-enabled nextapp
systemctl is-active nextapp
The last two lines read enabled and active.
Ask the app for its home page, then end its process the hard way and watch systemd bring it back:
curl -s --retry 5 --retry-connrefused --retry-delay 1 -o /dev/null -w '%{http_code}\n' http://127.0.0.1:3000/
sudo systemctl kill --kill-whom=main -s KILL nextapp
sleep 8
systemctl show nextapp -p ActiveState -p NRestarts
curl -s --retry 5 --retry-connrefused --retry-delay 1 -o /dev/null -w '%{http_code}\n' http://127.0.0.1:3000/
sudo journalctl -u nextapp -n 12 --no-pager
The app needs a second after it starts before it accepts connections, so curl tries again for a few seconds if the port is still closed. Both requests print 200. Between them systemctl show reads NRestarts=1 and ActiveState=active, and the journal shows Main process exited, code=killed, status=9/KILL, then Scheduled restart job, restart counter is at 1. five seconds later, and the app starting again.
Cannot find module '/srv/nextapp/server.js'. The build was not in standalone mode, so .next/standalone was never made. Check next.config.ts holds output: "standalone", then build and copy again./_next/static/ answer 404. The static folder was not copied next to the server. Copy .next/static to /srv/nextapp/.next/static and restart the service.EACCES or EROFS when a page is generated or an image is resized. The app is writing somewhere other than its cache folder, which the unit forbids. Add that folder to ReadWritePaths= if the app really needs it, run sudo systemctl daemon-reload, then restart.RestartSec=5 systemd tries again every five seconds and never gives up, so a crash at start shows as a climbing NRestarts. sudo journalctl -u nextapp -n 50 shows the error each time. Fix the cause, then sudo systemctl restart nextapp.curl answers from the server but the site is not reachable from outside. The app listens on 127.0.0.1 on purpose. Put a web server in front of it on the server, then serve that web server as the server's website: on the server's Console tab, under Website, give its port and click Serve website. The site then has its own address with HTTPS, private until you publish it.npm install.Environment= lines or an EnvironmentFile= readable only by root, never in the repository.WorkingDirectory= at it and restart. The old folder is your rollback.sudo journalctl -u nextapp after every release, not only when something breaks.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.