How to Host a Next.js App in Production on an Ubuntu VPS
Deploying a Next.js application to a production VPS can be intimidating, but this guide walks you through each step from setup to deployment.
🛠️ Prerequisites
- Ubuntu VPS with root access
- Node.js & npm installed
- Domain name (optional but recommended)
1. Connect to Your VPS
ssh root@your-server-ip
2. Clone Your Project
git clone https://github.com/your-user/your-nextjs-app.git
cd your-nextjs-app
3. Install Dependencies & Build
npm install
npm run build
4. Start with PM2
npm install -g pm2
pm2 start npm --name "next-app" -- start
pm2 save
pm2 startup
5. Configure NGINX
sudo nano /etc/nginx/sites-available/nextjs
Example config:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Then enable and restart NGINX:
sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled
sudo systemctl restart nginx
✅ Done!
Your Next.js app is now live on your VPS. Be sure to set up HTTPS with Let's Encrypt or a custom SSL certificate.