Thumbnail

How to Keep Your Server Running in your VPS using PM2


Self-hosting is a great way to have flexibility and cut a bunch of server costs.

 

Assuming you already have an idea of what a VPS is (if you don't know ). It's time to keep it continuously running on the server. We can achieve that by using a process manager like pm2 

 

We're gonna use Next JS Application as our example.

 

1. Build your Next JS App.

Always make sure to build your application so it will get minified and remove unnecessary junks from your app. (Do not use dev environment)
COMMAND: yarn build

 

2. Install pm2 globally:

COMMAND: npm install pm2 -g

 

3. On your Next JS Project, start a Production Environment using pm2

 

COMMAND: pm2 start yarn -- start
  • this is the easiest way to start the pm2 server
  • pm2 start is the command to start the pm2
  • yarn start is the command to start the build next js app

COMMAND: pm2 start yarn --name "<name of your app>" -- start
  • the previous command works but to add more context on the running processes we can use the --name flag and supply the name of the process which is typically the app name
  • --name add a process name that shows in pm2 list command
  • -- start this specifies the yarn command in the package.json

 

If you don't have package.json in your folder you can use this command:
COMMAND: pm2 start next --name "<name of your app>" -- start

 

TIP: to specify a port add -p <port number> on your command

 

4. Make sure to start the server after your VPS shut down for any reasons

COMMAND: pm2 startup

 

5: Save the process:

COMMAND: pm2 save

 

You can now do other important things without having to worry about the up time of your app.
© 2025 Akademyas. All rights reserved
Privacy