Managing a Next.js Application with PM2 |

Managing a Next.js Application with PM2

Posted on Sep 11, 2023

Ensuring that your web application remains live and resilient against potential crashes is a critical aspect of deployment. If you’re using Next.js, one of the tools you can employ to achieve this is pm2. In this blog post, I’ll guide you through the process of setting up and running your Next.js application with pm2.

What is PM2?

PM2 is a powerful, production-ready process manager for Node.js applications. It ensures that your application remains online by auto-restarting in case of crashes. Moreover, with its cluster mode, you can run your application across multiple CPU cores.

Let’s Dive In!

1. Installing PM2

If pm2 isn’t already part of your toolset, installing it is a breeze. Simply run:

npm install pm2 -g

Use sudo in case of Linux

2. Building the Next.js App for Production

Before deploying your application, make sure it’s in its best shape for production:

next build

3. Setting Up the PM2 Ecosystem File

Using an ecosystem file with pm2 not only makes the setup cleaner but also allows for fine-grained control over various configurations. Let’s set one up. Create an ecosystem.config.js at the root of your Next.js app and populate it with the following:

module.exports = {
  apps: [
    {
      name: 'nextjs-app',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        PORT: 3000, // You can specify other environment variables as needed.
      },
      instances: 1, // Use max instances for Cluster mode. You can adjust as needed.
      exec_mode: 'fork', // Alternatively you can user 'cluster' to enable cluster mode.
      autorestart: true, // Automatically restarts app if it crashes or is killed.
      watch: false, // Watch for changes in source files and restart app. Turn this off in production.
      max_memory_restart: '1G', // Restart the app if it reaches 1GB of memory.
    },
  ],
};

This configuration ensures that your app:

  • Uses cluster mode for optimal CPU utilization.
  • Restarts automatically if it crashes or consumes too much memory.
  • Doesn’t watch source files in production, which is ideal for performance.

4. Launching the Next.js App with PM2

With your ecosystem file ready, start your app with:

pm2 start ecosystem.config.js

5. Handy PM2 Commands

Navigating pm2 is a cinch. Here are some basic commands to manage your application:

  • Monitor: Keep an eye on your application’s status and logs.
pm2 monit
  • List: Check all applications managed by pm2.
pm2 list
  • Restart: Need a fresh start? Here’s how.
pm2 restart nextjs-app
  • Stop: Halt your application when required.
pm2 stop nextjs-app
  • Delete: Stop managing an app without deleting any source files.
pm2 delete nextjs-app

Wrapping Up

That’s all there is to it! With your Next.js application now under the protective wing of pm2, you can enjoy features like auto-restarts and cluster mode. Fine-tune the configurations as needed, and here’s to a robust and resilient web application!