Alternative of PM2 in debian 10

Hi there, If you are in trouble with PM2 in debian then here is a simple solution for you.

If you’re looking for alternatives to PM2 for managing your Next.js application processes on Debian 10, systemd would be best for you.

Managing a Next.js project on a VPS can be streamlined using systemd, a powerful system and service manager for Linux. This guide will walk you through setting up a systemd service to manage your Next.js application, ensuring it runs smoothly and restarts automatically if it crashes.

There few steps have to follow.

Step 1: Build Your Next.js Project

npm run build

Transfer all the build files to the server project directory.

Step 2: Create a systemd Service File

with the following command

sudo nano /etc/systemd/system/nextjs-app.service

Add the following configuration to the daemon service:

[Unit]
Description=Next.js Application
After=network.target

[Service]
Type=simple
User=youruser
Group=yourgroup
WorkingDirectory=/path/to/your/project
ExecStart=/usr/bin/env node /path/to/your/project/node_modules/.bin/next start -p 3000
Restart=always
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Step 3: Reload systemd to Apply the New Service

Run the following command

sudo systemctl daemon-reload

Step 4: Start and Enable the Service

sudo systemctl start nextjs-app

Enable the service to start automatically on boot:

sudo systemctl enable nextjs-app

Step 5: Verify the Service Status

sudo systemctl status nextjs-app

You should see output indicating that the service is active and running.

In a nutshell

By following these steps, you’ve successfully set up a systemd service to manage your Next.js application. This setup ensures that your application runs on a specified port, restarts automatically if it crashes, and starts automatically on system boot. Using systemd enhances the reliability and manageability of your Next.js deployment.