How to host a Next.js app in Production on an Ubuntu VPS

How to host a Next.js app in Production on an Ubuntu VPS

How to Host a Next.js App in Production on an Ubuntu VPS

"Deploying a Next.js application on an Ubuntu Virtual Private Server (VPS) offers unparalleled control, flexibility, and cost-effectiveness compared to managed platforms. By leveraging robust tools like PM2 for process management and Nginx as a reverse proxy, developers can ensure their applications run smoothly, securely, and handle high traffic volumes efficiently."

  • By: Talha Shahid Khan

  • Updated: October 10, 2026

In recent years, Next.js has become the go-to framework for building fast, SEO-friendly React applications. While managed platforms like Vercel make deployment incredibly simple, hosting your Next.js app on an Ubuntu Virtual Private Server (VPS) provides more control over your infrastructure, eliminates vendor lock-in, and can significantly reduce costs as your application scales.

If you are ready to take your deployment skills to the next level, this guide will walk you through the essential steps to get your Next.js application running in production on an Ubuntu VPS.

Prerequisites for Deployment

Before we dive into the setup, you will need a few things:

  • An Ubuntu VPS (20.04 or 22.04 LTS) from providers like DigitalOcean, Linode, or AWS.
  • A registered domain name pointed to your server's public IP address.
  • Root or sudo access to your server.
  • Your Next.js project pushed to a Git repository (like GitHub or GitLab).

Step 1: Prepare Your Server and Install Node.js

First, connect to your server via SSH. It is always a good practice to start by updating your package lists to ensure you have the latest security patches and software updates.

sudo apt update && sudo apt upgrade -y

Next, you need to install Node.js. The easiest way to get the latest LTS version is via NodeSource:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

Verify the installation by checking the versions: node -v and npm -v.

Step 2: Clone and Build Your Next.js Project

Navigate to the directory where you want to store your application, typically /var/www/, and clone your repository.

cd /var/www
sudo git clone https://github.com/yourusername/your-nextjs-app.git
cd your-nextjs-app

Install your project dependencies and create a production build. The build process optimizes your application for the best performance.

npm install
npm run build

Step 3: Keep Your App Alive with PM2

If you simply run npm start, your app will stop the moment you close your SSH session. To keep your Next.js app running in the background and automatically restart it if it crashes, we use a process manager called PM2.

Install PM2 globally:

sudo npm install -g pm2

Start your Next.js application using PM2:

pm2 start npm --name "nextjs-app" -- start

To ensure PM2 automatically starts your app when the server reboots, run:

pm2 startup
pm2 save

Step 4: Configure Nginx as a Reverse Proxy

By default, Next.js runs on port 3000. We need to use Nginx to listen on port 80 (HTTP) and route that traffic to your Next.js app.

Install Nginx:

sudo apt install nginx -y

Create a new Nginx configuration file for your site:

sudo nano /etc/nginx/sites-available/yourdomain.com

Paste the following configuration, making sure to replace yourdomain.com with your actual domain:

server {
    listen 80;
    server_name yourdomain.com www.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;
    }
}

Enable the configuration by creating a symlink to the sites-enabled directory, test Nginx, and restart the service:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 5: Secure Your App with SSL

Security is non-negotiable for modern web applications. Let's Encrypt provides free SSL certificates that you can easily install using Certbot.

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the prompts to configure your certificate. Certbot will automatically update your Nginx configuration to route traffic to HTTPS (port 443) and set up auto-renewal.

Conclusion

Congratulations! Your Next.js application is now successfully running in a robust production environment. By utilizing an Ubuntu VPS, PM2, Nginx, and Let's Encrypt, you have built a scalable and secure hosting architecture.

As your traffic grows, this setup gives you the flexibility to easily upgrade your server resources, tweak your Nginx caching strategies, and maintain full ownership over your deployment pipeline. Happy coding!