Next.js on AWS Lightsail Setup Guide

Next.js on AWS Lightsail Setup Guide

This is BASH (runs in Linux terminal). this is basically what you run on the ubuntu server to ensure everything is installed and configured correctly. Off course changing "example.com" etc. w

Published: Today3 views0 likesAuthor: ComputerScienceX

0 total likes

Next.js on AWS Lightsail Setup Guide

As of February 17, 2026, Lightsail has both Node.js and Node.js packaged by Bitnami blueprints. For a Next.js app, the cleanest setup is:

  1. Linux/Unix -> OS Only -> Ubuntu 24
  2. Run Next.js with next start on port 3000
  3. Put nginx in front as reverse proxy on 80/443

This gives you full control and predictable paths.

Where to put your Next.js app

  • Recommended (OS-only Ubuntu): /var/www/nextapp
  • If you choose Lightsail Node.js blueprint: deploy to /var/www/html/
  • If you choose Node.js packaged by Bitnami: use /opt/bitnami/projects/<app>

Terminal guide (Ubuntu 24 OS-only blueprint)

# 1) Base packages
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx git curl build-essential
 
# 2) Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
 
# 3) App folder
sudo mkdir -p /var/www/nextapp
sudo chown -R $USER:$USER /var/www/nextapp
cd /var/www/nextapp
 
# 4) Get your code (pick one)
git clone <your-repo-url> .
# OR copy files with scp/rsync from local machine
 
# 5) Enable pnpm + install + build
# (use sudo here because Node from apt often needs root to create /usr/bin/pnpm shim)
sudo corepack enable
corepack prepare pnpm@latest --activate
# fallback if pnpm is still not available
command -v pnpm >/dev/null 2>&1 || sudo npm install -g pnpm@latest
 
# install dependencies from package.json (this includes next/react/react-dom if already defined)
pnpm install
# install Next.js packages if missing
pnpm ls next >/dev/null 2>&1 || pnpm add next react react-dom
pnpm run build

If pnpm install is Killed on a small instance, add swap and retry:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
pnpm install
pnpm run build

If next build fails with build worker exited ... SIGABRT, run a lower-memory build:

rm -rf .next
NODE_OPTIONS="--max-old-space-size=1536" pnpm exec next build --no-lint

If it still fails, increase instance RAM (or build in CI/local and deploy artifacts).

Create a service so app auto-starts on reboot:

sudo tee /etc/systemd/system/nextapp.service > /dev/null << 'EOF'
[Unit]
Description=Next.js app
After=network.target
 
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/var/www/nextapp
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/env pnpm start
Restart=always
RestartSec=5
 
[Install]
WantedBy=multi-user.target
EOF
 
sudo systemctl daemon-reload
sudo systemctl enable --now nextapp
sudo systemctl status nextapp

Configure nginx reverse proxy:

sudo tee /etc/nginx/sites-available/nextapp > /dev/null << 'EOF'
server {
  listen 80;
  server_name yourdomain.com www.yourdomain.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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
EOF
 
sudo ln -s /etc/nginx/sites-available/nextapp /etc/nginx/sites-enabled/nextapp
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl enable nginx

Enable HTTPS:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

In Lightsail console, also do this:

  1. Attach a Static IP
  2. Open firewall ports: 22, 80, 443
  3. Point DNS A record to the static IP