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:
Linux/Unix->OS Only->Ubuntu 24- Run Next.js with
next starton port3000 - Put
nginxin front as reverse proxy on80/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.jsblueprint: 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 buildIf 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 buildIf 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-lintIf 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 nextappConfigure 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 nginxEnable HTTPS:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comIn Lightsail console, also do this:
- Attach a Static IP
- Open firewall ports:
22,80,443 - Point DNS A record to the static IP
