ShortIQ

ShortIQ

Deployment

How to Deploy a Next.js Project on Ubuntu 24.04

A practical production guide for deploying a Next.js app on Ubuntu 24.04 with Node.js, PM2, Nginx, and HTTPS.

April 20, 2026ShortIQ Editorial Team

Advertisement

Why Ubuntu 24.04 Is a Good Base for Next.js

Ubuntu 24.04 is a practical choice for self-hosting a Next.js app because it is stable, widely documented, and easy to maintain over time. For many teams, it provides the right balance between control and simplicity. You can keep deployment predictable without needing a complex platform layer on day one.

For a production Next.js setup, the usual stack is straightforward: Node.js runs the app, PM2 keeps it alive, and Nginx sits in front as a reverse proxy. Add Let’s Encrypt for HTTPS and you have a clean deployment model that works well for startup products, internal tools, and SaaS marketing sites.

This guide focuses on that exact model so you can get a real Ubuntu 24.04 deployment running without unnecessary detours.

What You Need Before Deployment

Before you begin, make sure you have an Ubuntu 24.04 server, SSH access, and a domain or subdomain already pointed to the server IP. You should also have a Next.js project that builds successfully in production mode. Do not start the server setup until your application can pass a local production build.

A common working directory is something like /var/www/my-next-app. The exact path is not important as long as ownership and permissions are clean. In general, predictable paths and predictable users make server maintenance easier later.

It also helps to decide upfront how you will update the code: Git pull, CI/CD, or manual upload. The deployment flow is much easier to maintain when updates follow one consistent pattern.

  • Ubuntu 24.04 server with SSH access
  • A domain pointed to the server IP
  • A Next.js project that already passes npm run build
  • A clear deployment path such as git clone or CI/CD

Install System Packages and Node.js

Start by updating Ubuntu so you are not deploying onto stale system packages. Then install Node.js 20, which is a safe production choice for most current Next.js projects, and install Nginx so the reverse proxy layer is ready before the app goes live.

Run the commands below in order. The version checks at the end matter because they confirm the runtime layer is healthy before you spend time debugging the app itself.

bash
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs nginx

node -v
npm -v
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx
  • Update packages first
  • Install Node.js 20 and Nginx
  • Verify the runtime before moving on

Deploy the Next.js App Files

Once the server runtime is ready, put the code in a stable path such as /var/www/my-next-app. Many teams use git clone. After that, fix ownership, install dependencies, create the production env file, and build the app.

Treat npm run build as the hard gate. If it fails on the server, stop there and fix the problem before touching PM2 or Nginx. Most broken deployments are really broken builds with missing env values or dependency mismatches.

bash
cd /var/www
sudo git clone https://github.com/yourname/your-next-app.git my-next-app
sudo chown -R $USER:$USER /var/www/my-next-app

cd /var/www/my-next-app
npm install
nano .env.local
npm run build
env
NEXT_PUBLIC_SITE_URL=https://example.com
API_BASE_URL=https://api.example.com
  • Clone into a stable directory
  • Fix permissions before installing packages
  • Create .env.local before building

Run Next.js with PM2

PM2 is a simple way to keep the Next.js app running in the background. It handles restarts, gives you logs, and can bring the app back automatically after a server reboot. For many Ubuntu deployments, this is enough without introducing Docker.

Install PM2 globally, start the app through npm start, save the process list, then enable PM2 startup. After that, read the logs once before moving on so you know the app actually booted.

bash
sudo npm install -g pm2
cd /var/www/my-next-app

pm2 start npm --name "my-next-app" -- start
pm2 list
pm2 logs my-next-app
pm2 save
pm2 startup
  • Install PM2 globally
  • Start the app through npm
  • Enable startup so it survives reboot

Put Nginx in Front of the App

Nginx should proxy public requests to the local Next.js process on 127.0.0.1:3000. This keeps the Node app off the public edge and gives you a cleaner place to handle headers, domains, and TLS.

Create an Nginx site file, enable it, test the syntax, and reload Nginx. Confirm the domain works over HTTP before you add HTTPS.

nginx
server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1: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;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
bash
sudo nano /etc/nginx/sites-available/my-next-app
sudo ln -s /etc/nginx/sites-available/my-next-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
  • Create a dedicated server block
  • Point proxy_pass at the local Next.js port
  • Test before reloading

Add HTTPS with Let’s Encrypt

After the HTTP site works, add HTTPS with Certbot. On Ubuntu 24.04, the Nginx plugin is the easiest path because it can update the server block automatically.

Always run the dry-run renewal test too. Certificate renewal is part of the deployment, not an afterthought.

bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
  • Install Certbot for Nginx
  • Issue certificates for both root and www domain
  • Verify renewal

How to Update the App Safely Later

A clean update flow is just four steps: pull code, install dependencies, rebuild, restart PM2. That keeps deployments understandable and repeatable.

If you change environment values that affect the build, rebuild the app instead of only restarting PM2. That small habit prevents a lot of confusion in production.

bash
cd /var/www/my-next-app
git pull
npm install
npm run build
pm2 restart my-next-app
  • Keep one repeatable deployment sequence
  • Rebuild when build-time env values change

Common Deployment Problems on Ubuntu 24.04

A 502 Bad Gateway error usually means Nginx cannot reach the Next.js process. Start by checking whether PM2 is actually running the app and whether the process is listening on the expected port. In many cases, the bug is not Nginx itself but the app never starting correctly.

If the build fails on the server, check Node version, package lock consistency, and missing environment values. Deployment issues often look like infrastructure problems when they are really application build problems. That is why treating npm run build as a hard gate matters so much.

Static asset issues, mixed-content problems, or broken domains usually point back to incorrect public URLs, bad proxy headers, or inconsistent environment setup. Most fixes become much simpler once logs are checked in the right order: app first, then proxy, then DNS or certificate layers.

bash
pm2 list
pm2 logs my-next-app
sudo systemctl status nginx
sudo nginx -t
sudo tail -f /var/log/nginx/error.log

Recommended Next Steps After the First Deployment

Once the app is live, the next layer of maturity is not more complexity for its own sake. It is better operational discipline: backups, server monitoring, deployment automation, and a repeatable rollback process. Those are the upgrades that actually improve reliability.

If you are deploying a marketing or SaaS site, it is also worth connecting the deployment process to analytics, uptime checks, and release notes. A server is not only a place where the app runs. It is part of how the product is observed and maintained.

If you want to continue refining your infrastructure, the best internal next steps are to review related ShortIQ guides on campaign tracking, UTM workflows, and performance planning so deployment and growth operations stay connected.

  • Homepage: /
  • Blog hub: /blog
  • UTM builder page: /utm-builder-tool
  • Campaign tracking guide: /campaign-tracking-saas
  • Link tracking software page: /link-tracking-software

FAQ

What is the easiest way to deploy Next.js on Ubuntu 24.04?

A practical setup is Node.js plus PM2 for the app process and Nginx as a reverse proxy, with Let’s Encrypt handling HTTPS.

Do I need Docker to deploy Next.js on Ubuntu?

No. Docker can be useful, but many teams successfully deploy Next.js on Ubuntu with PM2 and Nginx alone.

Why does Nginx show 502 Bad Gateway with Next.js?

Usually because the Next.js app is not running correctly under PM2 or Nginx is proxying to the wrong local port.

Should I rebuild the app on every deployment?

Yes, especially when code or relevant environment variables have changed. The build should be treated as a required deployment step.

Is Ubuntu 24.04 good for production Next.js hosting?

Yes. Ubuntu 24.04 is a stable and widely supported base for hosting Next.js apps with Node.js, PM2, and Nginx.

Related free tools

If you want to turn this topic into action, use one of ShortIQ's free tools for campaign planning, UTM structure, or QR distribution.

Continue Reading

Explore more guides on link shortener SaaS strategy, Bitly alternatives, and white label link management.