Inteliny
DevOpsLevel: Intermediate25mVerified Production Blueprint

How to Deploy a Next.js Production Application on Ubuntu Server with Nginx

Learn to deploy a Next.js application on Ubuntu Server with Nginx for production environments

Inteliny Engineering

Principal Architect

Overview & Architecture Scope

Deploying a Next.js application to a production environment requires careful consideration of performance, security, and scalability. This guide is for developers and DevOps engineers looking to deploy their Next.js applications on Ubuntu Server with Nginx. By the end of this guide, you will have a fully deployed Next.js application with SSL, environment variables, image optimization, caching, compression, monitoring, security headers, and performance tuning. You will need an Ubuntu Server, a basic understanding of Linux commands, and a Next.js project ready for deployment.

Prerequisites & System Requirements

Ensure your development workstation or staging server fulfills the following prerequisites before initiating commands:

Node.js v18.0+ runtime environment
MongoDB v6.0+ database cluster
Active AWS or Cloudflare account with DNS access
Linux Ubuntu 22.04 LTS server instance
Basic knowledge of CLI bash & Git workflow

Target System Architecture Diagram

Browser
NGINX
Node / Express
MongoDB / Redis

Interactive Execution Checklist

0/10 Completed

Step-by-Step Implementation Guide

1

Step 1: Set Up Ubuntu Server

Start by setting up your Ubuntu Server. Ensure it's updated and you have a user with sudo privileges. Install necessary packages like git, curl, and build-essential. Update your package index and upgrade your system.

Execute Command Terminal:

bash
sudo apt update && sudo apt upgrade -y
Make sure your server is secure and up-to-date before proceeding.
2

Step 2: Install Nginx

Install Nginx on your Ubuntu Server. This will serve as your reverse proxy and handle SSL termination.

Execute Command Terminal:

bash
sudo apt install nginx -y
Nginx is crucial for handling requests and securing your application.
3

Step 3: Install PM2

Install PM2, a process manager for Node.js applications. It will keep your Next.js application running in the background.

Execute Command Terminal:

bash
sudo npm install pm2 -g
PM2 is essential for managing and keeping your application alive.
4

Step 4: Configure Nginx

Configure Nginx to serve your Next.js application. Create a new configuration file in /etc/nginx/sites-available/ and link it to sites-enabled.

Execute Command Terminal:

bash
sudo nano /etc/nginx/sites-available/nextjs
Your Nginx configuration should point to your Next.js application and handle SSL certificates.
5

Step 5: Set Up SSL with Let's Encrypt

Install Let's Encrypt and generate SSL certificates for your domain. This will secure your application.

Execute Command Terminal:

bash
sudo add-apt-repository ppa:certbot/certbot && sudo apt install certbot python3-certbot-nginx -y
SSL is crucial for securing your application and protecting user data.
6

Step 6: Deploy Next.js Application

Clone your Next.js repository to your server, install dependencies, and build your application for production.

Execute Command Terminal:

bash
git clone https://github.com/your-repo/nextjs-app.git && cd nextjs-app && npm install && npm run build
Ensure your application is built for production to optimize performance.
7

Step 7: Configure Environment Variables

Set environment variables for your Next.js application. This could include database connections, API keys, or other sensitive information.

Execute Command Terminal:

bash
export DATABASE_URL='your-database-url'
Environment variables should be secured and not committed to your version control.
8

Step 8: Optimize Images

Optimize images in your Next.js application to improve page load times. Use tools like ImageOptim or ShortPixel.

Execute Command Terminal:

bash
npx next-optimized-images
Image optimization can significantly improve your application's performance.
9

Step 9: Enable Caching and Compression

Enable caching and compression in your Nginx configuration to reduce the load on your server and improve page load times.

Execute Command Terminal:

bash
sudo nano /etc/nginx/nginx.conf
Caching and compression can greatly enhance your application's performance and user experience.
10

Step 10: Monitor Your Application

Set up monitoring tools like New Relic or Datadog to keep an eye on your application's performance and identify potential issues.

Execute Command Terminal:

bash
sudo apt install newrelic-sysmond -y
Monitoring is crucial for maintaining high performance and quickly identifying issues.

Pro Tips & Optimizations

Use a CDN to further reduce page load times and improve user experience.
Regularly update your dependencies to ensure you have the latest security patches.
Implement security headers in your Nginx configuration to protect against common web vulnerabilities.
Test your application thoroughly after deployment to ensure everything is working as expected.

Common Pitfalls to Avoid

Forgetting to update your package index before installing new packages, which can lead to compatibility issues.
Not securing your environment variables, which can expose sensitive information.
Failing to configure caching and compression, resulting in poor application performance.
Conclusion & Next Steps

By following these steps, you have successfully deployed your Next.js application on Ubuntu Server with Nginx, ensuring a secure, scalable, and high-performance production environment. Remember to continuously monitor your application and update your dependencies to maintain security and performance. With this setup, you're ready to serve your users a fast and secure web application.

Production Best Practices & Hardening

Security Hardening

Disable root SSH access, enforce key-based auth, and enable UFW firewall on ports 80/443.

Memory Management

Set Node.js max-old-space-size to 80% of total RAM to avoid Linux OOM-killer crashes.

Frequently Asked Questions

Yes, all NGINX, Docker, and PM2 deployment steps can be packaged into Infrastructure as Code (IaC) playbooks.

Need Help Implementing This?

Partner with Inteliny's principal architects to audit your stack, automate CI/CD, and accelerate deployment.