How to start a NextJS app using PM2 and Yarn

server
2 min read

PM2 when combined with NPM/Yarn provides a powerful way to manage your NextJS app. It adds a host of options to deal with your apps like start, stop, restart, logs, running as a service, etc.

I was recently setting up a server to host one of my personal sites, so I thought of sharing a few posts on how I actually set up my server to host Next.JS sites in production. This post tries to capture the steps that can be used to start any NodeJS backed application either using npm or yarn.

Prerequisites:

9 easy steps to manage the NextJS app with PM2 and Yarn:

Get the status of apps already running with PM2

pm2 list

Run this command from the root of the project directory. Replace test with a name that you want to give your app for easy identification.

If using yarn:

pm2 start yarn --name test -- start -- --port 3000

Or if using npm:

pm2 start npm --name test -- start -- --port 3000

Verify the new app is added to PM2

pm2 list

Get details for the PM2 process

pm2 show 0

Get streaming logs created by the app

pm2 logs 0

Configure PM2 to start automatically after server reboot

pm2 startup

Freeze the list of apps to be started when PM2 starts after the server reboot

pm2 save

Stop the PM2 process with the mentioned ID

pm2 stop 0

Restart the PM2 process with the mentioned ID

pm2 restart 0

PM2 itself offers a lot of options to manage your apps in production and can be further referenced in official docs.