Loading…
Loading documentation...
Run a production Next.js app on your server as a systemd service and publish it through a port forward.
Run a production Next.js application on a Linux server with Node.js and a systemd service, and publish it on your environment's public address.
Turn on public SSH on the server's page and copy the command from its How to connect card. Root cannot sign in, so start a root shell for the steps below:
ssh sauuser@SERVER_ADDRESS -p SSH_PORT
sudo -i
Install Node.js 24 LTS from NodeSource, and Git:
curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
apt install -y nodejs git
# Verify
node --version # v24.x
npm --version
The app runs as its own user, not as root:
adduser --disabled-password --gecos "" deploy
sudo -u deploy git clone https://github.com/your-org/your-app.git /home/deploy/app
cd /home/deploy/app
sudo -u deploy npm ci
sudo -u deploy npm run build
For a private repository, give the server a deploy key in your Git host, or clone with an access token.
systemd starts the app at boot and restarts it if it stops:
cat > /etc/systemd/system/nextjs.service <<'EOF'
[Unit]
Description=Next.js app
After=network.target
[Service]
User=deploy
WorkingDirectory=/home/deploy/app
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/npm start
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nextjs
# The app takes a few seconds to start: wait up to a minute for port 3000 to answer
timeout 60 sh -c 'until curl -s -o /dev/null http://127.0.0.1:3000/; do sleep 1; done'
curl -sI http://127.0.0.1:3000/ | head -n 1
Read its log with journalctl -u nextjs -f.
A Linux server has no public address of its own, and nothing reaches it until you publish a port. On Network > Public access, under Port forwards, click Add a forward and choose the server, the protocol, an External port from your environment's block (the form names the range; every port in it is 10000 or higher) and the Target port your service listens on. The forward's row then shows the address and port to Connect to, and Who can connect limits it to the addresses you name. A forward opens its own firewall rule, so you add none for it.
A site you run on the server itself is served from its own web server. On the server's Console tab, under Website, give the port your web server listens on (plain HTTP; 80 unless you changed it) and click Serve website. The site gets its own address with HTTPS at once and stays private until you click Publish website. To put it at your own domain, add the domain on Network > Public access under Add a domain and choose that server; the platform obtains and renews its certificate. An app installed from Servers > [your server] > Apps, or an Instant App, takes your domain under Your own domain the same way.
For this app, add a forward with protocol TCP and Target port 3000, then open http://ADDRESS:PORT.
A deploy script pulls, builds and restarts the service. The deploy user may restart this one service and nothing else:
cat > /etc/sudoers.d/nextjs-deploy <<'EOF'
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nextjs
EOF
chmod 440 /etc/sudoers.d/nextjs-deploy
visudo -cf /etc/sudoers.d/nextjs-deploy
cat > /home/deploy/deploy.sh <<'EOF'
#!/bin/bash
set -euo pipefail
cd /home/deploy/app
git pull --ff-only
npm ci
npm run build
sudo /usr/bin/systemctl restart nextjs
echo "Deployed at $(date)"
EOF
chown deploy:deploy /home/deploy/deploy.sh
chmod 755 /home/deploy/deploy.sh
Run sudo -u deploy /home/deploy/deploy.sh by hand, or from CI/CD.
Keep secrets in .env.production (not committed to Git), readable only by the deploy user. Next.js reads it at build and at start, so rebuild after changing it:
cat > /home/deploy/app/.env.production <<'EOF'
NEXT_PUBLIC_SITE_URL=http://ADDRESS:PORT
DATABASE_HOST=10.0.101.3
EOF
chown deploy:deploy /home/deploy/app/.env.production
chmod 600 /home/deploy/app/.env.production
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.