04 / Blog

← Back to Blog

How to Deploy a Next.js App to Your Own Server

People often ask me why not just use Vercel. My answer is usually about control and cost. If the app needs to sit close to an internal database or has to run on infrastructure you own, a server of your own starts to make more sense. At Caius Studio I look after Linux servers for several projects, so this guide comes from that habit.

What You Need

You need a VPS running Ubuntu or Debian, SSH access, an LTS version of Node.js, Nginx, and a domain pointing to the server's IP. I usually use a regular user with sudo rather than logging in directly as root.

Also make sure your Next.js project builds on your laptop first. If the build fails there, it will almost certainly fail on the server too.

Prepare a Standalone Build

Add standalone output in next.config.js so Next.js bundles everything it needs into one folder:

const nextConfig = {
  output: 'standalone',
};
module.exports = nextConfig;

Then, on the server, pull the code and build it:

git clone https://github.com/nama/proyek.git
cd proyek
npm ci
npm run build

The output lands in .next/standalone. The public folder and .next/static are not copied automatically, so copy them yourself:

cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/

Run It with PM2

To keep the app alive after a server restart or a crash, I use PM2:

sudo npm install -g pm2
PORT=3000 pm2 start .next/standalone/server.js --name proyek
pm2 save
pm2 startup

The app now runs on port 3000 and can only be reached from inside the server. That is what you want, because Nginx will be the one facing the internet.

Docker is another option. With a multi-stage Dockerfile, the build and runtime stages are separated, which keeps the final image smaller. If your team already works with Docker, use that. If not, PM2 is enough for many cases.

Set Up Nginx as a Reverse Proxy

Create a config file at /etc/nginx/sites-available/proyek:

server {
    listen 80;
    server_name domainanda.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable it and check the configuration:

sudo ln -s /etc/nginx/sites-available/proyek /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

The Upgrade header matters if the app later uses WebSockets. If it doesn't, leaving it in does no harm.

Add SSL

Let's Encrypt certificates can be installed for free with Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d domainanda.com

Certbot sets up automatic renewal. To confirm it works, run sudo certbot renew --dry-run every so often.

Handling Updates

For each new release, pull the code, rebuild, and restart:

git pull
npm ci
npm run build
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
pm2 restart proyek

These steps are safer wrapped in a single shell script, so nothing gets skipped when you are in a hurry. I forgot to copy the static folder the first time I did this, and that is how I ended up putting it into a script.

  • Next.js
  • Deploy
  • VPS
  • Nginx
  • Linux