Learn how to deploy your Next.js application with NGINX for optimal performance and scalability.
This guide provides a step-by-step walkthrough for deploying a Next.js application on a Linux server using Nginx as a reverse proxy and PM2 for process management. You will need a Linux server with Node.js and npm installed, a registered domain name or server IP address, and a basic understanding of the Linux command line to follow this guide. We will start by building the Next.js application for production, then install PM2 to manage the application process. Next, we will configure Nginx as a reverse proxy to handle incoming traffic and direct it to our application. Finally, we will ensure our application is running correctly and accessible via our domain name or server IP address.
This guide provides a comprehensive walkthrough of deploying a Next.js application on a Linux server using Nginx as a reverse proxy and PM2 for process management.
Prerequisites:
Steps:
Build your Next.js application:
Navigate to your Next.js project directory and run the following command to build the production-ready application:
npm run build
Install PM2:
PM2 is a process manager for Node.js applications that helps keep your application running and automatically restarts it in case of crashes. Install it globally using npm:
npm install -g pm2
Create a PM2 configuration file:
Create a file named ecosystem.config.js
in your project's root directory. This file will contain the configuration for PM2 to manage your Next.js application. Here's an example:
module.exports = {
apps: [{
name: 'your-app-name',
script: 'npm',
args: 'start',
cwd: './',
instances: 1,
exec_mode: 'cluster',
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
},
}],
};
Replace your-app-name
with a descriptive name for your application.
Start your application with PM2:
From your project's root directory, run the following command to start your application using PM2:
pm2 start ecosystem.config.js
This will start your Next.js application in the background and keep it running.
Install and configure Nginx:
Nginx will act as a reverse proxy, directing incoming traffic to your Next.js application. Install Nginx using your server's package manager. For example, on Ubuntu:
sudo apt-get install nginx
Create an Nginx server block:
Create a new server block configuration file for your application. For example, create a file named your-app-name
in the /etc/nginx/sites-available
directory. Here's an example configuration:
server {
listen 80;
server_name your-domain.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;
}
}
Replace your-domain.com
with your actual domain name or server IP address. The proxy_pass
directive should point to the port where your Next.js application is running (default is 3000).
Enable the server block:
Create a symbolic link from the server block configuration file to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/your-app-name /etc/nginx/sites-enabled/
Test and reload Nginx:
Test your Nginx configuration for any errors:
sudo nginx -t
If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Your Next.js application should now be accessible via your domain name or server IP address.
Additional Notes:
This PM2 configuration defines a single application named "your-app-name". It runs "npm start" in the current directory, using a single instance in cluster mode. The application doesn't automatically restart on file changes and restarts if it exceeds 1GB of memory. The NODE_ENV is set to "production".
module.exports = {
apps: [{
name: 'your-app-name',
script: 'npm',
args: 'start',
cwd: './',
instances: 1,
exec_mode: 'cluster',
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
},
}],
};
This code defines the PM2 configuration for your Next.js application.
Explanation:
name
: The name of your application in PM2.script
: The command to start your application. In this case, it's npm start
.args
: Arguments passed to the script.cwd
: The working directory for your application.instances
: The number of instances of your application to run.exec_mode
: The execution mode. cluster
mode utilizes multiple CPU cores.watch
: Whether to watch for file changes and restart the application automatically.max_memory_restart
: The maximum memory threshold before restarting the application.env
: Environment variables to set for your application.Remember to replace "your-app-name"
with the actual name of your Next.js application. This configuration file is used by PM2 to manage your application's processes, ensuring it runs smoothly and restarts automatically if needed.
proxy_pass
directive in your Nginx configuration points to the correct port where your Next.js application is running. The default port for Next.js applications is 3000, but you can customize it.This guide explains how to deploy a Next.js application on a Linux server using Nginx and PM2.
Step | Description |
---|---|
1. Build Next.js App | Run npm run build to create a production-ready build. |
2. Install PM2 | Install PM2 globally using npm install -g pm2 . |
3. Create PM2 Config File | Create ecosystem.config.js to define how PM2 manages your app. |
4. Start App with PM2 | Run pm2 start ecosystem.config.js to start your app in the background. |
5. Install and Configure Nginx | Install Nginx and configure it as a reverse proxy to direct traffic to your app. |
6. Create Nginx Server Block | Create a server block configuration file in /etc/nginx/sites-available to define how Nginx handles requests for your domain. |
7. Enable Server Block | Create a symbolic link from your server block file to /etc/nginx/sites-enabled . |
8. Test and Reload Nginx | Test your Nginx configuration with sudo nginx -t and reload it with sudo systemctl reload nginx . |
After these steps, your Next.js application should be accessible through your domain name or server IP address.
This guide demonstrated how to deploy a Next.js application on a Linux server using Nginx and PM2. By following these steps, you can ensure your application is served efficiently and reliably to your users. Remember to consider the additional notes for optimizing performance, security, and maintenance of your deployed application. By leveraging the combined power of Next.js, Nginx, and PM2, you can create a robust and scalable web application ready for production environments.
To make our Nextjs webapp accessible publicly on the internet, we need to push the code to a Linux server. But the Node.js server can’t handle the ...