PM2: How to auto-restart Node.js app on server reboot

Updated: December 30, 2023 By: Guest Contributor Post a comment

Introduction

Whether you are running a Node.js application in a production environment or just setting up a personal project on a virtual private server, ensuring your application remains alive after a server reboot is critical. PM2 is a popular process manager for Node.js applications that makes handling this task simple and efficient, even across server reboots. In this tutorial, we’ll explore the steps required to setup your Node.js application with PM2 for automatic restarting upon server boot. We’ll start with the basics and progressively cover more advanced configurations suitable for varied use-cases.

Prerequisites:

  • Node.js and npm installed
  • Basic knowledge of the command line
  • A Node.js application ready to be served

Setting Up PM2

First, you need to install PM2 globally by running:

npm install pm2 -g

Once the installation is complete, navigate to your application’s directory and start it using PM2:

pm2 start app.js --name my-app

Replace ‘app.js’ with the main entry file of your Node.js app and ‘my-app’ with a name of your choice for the process.

Configuring PM2 for Startup

To ensure that your application starts automatically on server reboot, use the PM2 startup command:

pm2 startup systemd

After running this command, PM2 will provide an output with a command that you need to execute with superuser privileges:

sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u username --hp /home/username

Make sure to replace ‘username’ with your actual username on the system.

Now PM2 knows that it should resurrect your processes on reboot. To save the current list of running processes for PM2 to restart after a reboot, execute the following:

pm2 save

Managing your Application with PM2

You can manage and inspect your application using the PM2 CLI:

To list all processes:

pm2 list

To restart a process:

pm2 restart my-app

To stop a process:

pm2 stop my-app

You may also want to check logs, monitor, and do other things with your app which are out of the scope for this article but can be found withpm2 --help.

Advanced Configuration: Ecosystem File

For a more complex setup, you can use PM2’s Ecosystem file, which allows you to define and manage complex application configurations. Create an ecosystem.config.js file in your application’s root directory:

module.exports = {
  apps : [{
    name: 'my-app',
    script: './app.js',
    instances: 'max',
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],
  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/my-app',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

To start your application with the ecosystem file, you would use:

pm2 start ecosystem.config.js

To set up the startup script using an ecosystem file:

pm2 startup -u `whoami` --hp `echo $HOME`

Conclusion

We’ve covered the essential steps for managing your Node.js application process with PM2, ensuring it restarts automatically after a server reboot. By harnessing PM2’s straightforward command line tools and the potential of the ecosystem.config.js, after-the-fact app management and configuration scaling can cater to many different project requirements, setting the stage for a reliable production environment.

Should you face any issues while setting up the auto-restart mechanism with PM2, you can refer to PM2’s comprehensive documentation or seek community support for your specific problem. Happy Coding!