VPS · Ubuntu 24.04

MongoDB Setup & Next.js Integration

Full, secure deployment procedure — install packages, clean YAML config files, isolate privileges with dedicated databases, customize system firewalls, connect Compass GUI, and connect a MERN or Next.js app safely.

May 20268 min read
Add GPG key & repo
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] \
  https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
Install & start
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod

Quick reference — URI patterns

Same VPS (internal)mongodb://user:pass@127.0.0.1:27017/db?authSource=db
Remote app / Compassmongodb://user:pass@VPS_IP:27017/db?authSource=db
Admin accessmongodb://adminUser:pass@VPS_IP:27017/?authSource=admin
.env.local keyMONGODB_URI=mongodb://user:pass@VPS_IP:27017/db?authSource=db