How to Update & Deploy Your Next.js App on a VPS Using Git and PM2
Keeping your Next.js app updated on your VPS is simple when you follow the right steps. Whether you’re pushing updates or deploying a fresh version, this quick guide will walk you through the process — from pulling the latest code to restarting your app with PM2.
✅ Step 1: Log In to Your VPS
Use SSH to access your server:
ssh root@your-server-ip
Make sure you have access permissions and the necessary SSH key or credentials.
📁 Step 2: Navigate to Your Project Directory
Move to the directory where your Next.js app lives (or where you want to deploy it):
cd /var/www/html/directory
Update the path if your project is located elsewhere.
🧬 Step 3: Pull the Latest Code from Git
Make sure you’re in the correct Git branch (e.g. main), then pull the latest updates:
git pull origin main
This will sync your server with the latest code from your remote repository.
🛠 Step 4: Build the Project
Next.js apps must be built before running in production:
npm install # if any dependencies changed
npm run build # build the latest version of the app
This compiles the app and prepares it for production.
🚀 Step 5: Run or Restart Your App with PM2
If your app is already managed by PM2, Read This If You didn’t Added Your Service on PM2:
pm2 restart your-app-name
🎉 Step 6: You’re Live!
That’s it! Your Next.js app is now updated and running the latest version.
Enjoy your app — faster, smarter, and up-to-date 🚀