Self-Hosting 101

Self-Hosting 101 (Advanced Setup)

Own the infrastructure when you graduate from Vercel’s free tier. Follow the checklist to spin up an EC2 box, deploy manually with pnpm, and keep it running.


Quick Checklist

  1. Confirm GitHub, Git, Node.js, Supabase access from Dev Environment Setup.
  2. Install pnpm and prove the app builds locally.
  3. Create an AWS account and launch a Ubuntu EC2 instance.
  4. SSH in, install Node.js + pnpm, clone your repo.
  5. pnpm install, pnpm build, pnpm start on the server.
  6. Add PM2 (optional) and wire up a custom domain if needed.

1. Local Pre-Flight

  • Install pnpm once: npm install -g pnpm → verify with pnpm --version.
  • From your project root:
pnpm install
pnpm build
pnpm start   # visit http://localhost:3000
  • Commit and push the working state to GitHub so the server can pull it.

2. AWS Account & Instance

  1. Sign in at aws.amazon.com → choose the Free Tier.
  2. Search for EC2Launch instance with:
    • Name nextjs-server
    • Ubuntu 24.04 LTS
    • t2.micro (Free Tier)
    • New key pair (.pem download)
    • Allow HTTP (80) and HTTPS (443)
  3. Wait for the instance to boot, copy the Public IPv4 DNS.

SSH in:

chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@ec2-xx-xx-xx-xx.compute-1.amazonaws.com

3. Prepare the Server

Run these commands on the EC2 shell:

sudo apt update
sudo apt install -y git curl
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install -g pnpm
node --version && pnpm --version

Store environment variables (Supabase keys, etc.) in a .env file or the shell profile; keep secrets out of git.


4. Deploy from GitHub

git clone https://github.com/yourusername/your-repo.git
cd your-repo
cp .env.production .env   # if you keep a template
pnpm install
pnpm build
pnpm start

Visit http://<your-public-dns> in the browser—your Next.js app should load. Use Ctrl + C to stop the process.


5. Keep It Running (PM2)

sudo npm install -g pm2
pm2 start "pnpm start" --name "nextjs-app"
pm2 save
pm2 startup   # follow the printed instructions once

Useful checks:

  • pm2 status — ensure the process is online
  • pm2 logs nextjs-app — view runtime logs
  • sudo rebootpm2 resurrect (if needed after reboot)

6. Optional: Custom Domain

  1. Buy a domain (Namecheap, etc.).
  2. In AWS Route 53 → Hosted Zone → create an A record pointing to the EC2 public IP.
  3. Update nameservers with your registrar to the ones Route 53 provides.
  4. Consider adding Nginx + SSL (Let’s Encrypt) once traffic increases.

Validation Loop

StageCommandWhat to check
Localpnpm devUI works while coding
Local productionpnpm build && pnpm startBundled app renders
Remote buildpnpm install && pnpm buildDependencies available
Remote runtimepm2 statusApp stays online after deploy

Log any failures in Troubleshooting & Logs with the command you ran and the error snippet.


Practice Challenge

  1. Create a test Next.js app with pnpm create next-app.
  2. Push it to GitHub and deploy to EC2 using the flow above.
  3. Add a /status route that prints environment info.
  4. Confirm you can reach /status from the public URL.

You now control the full stack—from prompts to production hardware.

© 2025 Building with AI Course