Node.js deployment with pm2
For running Node.js applications in production, you need process manager who takes care of the node processes.
We explain how to use the PM2 for setting up a Node.js Application for production on this site.
PM2 is a daemon process manager that will help you manage and keep your application online.
- Log in via SSH with your Site User:
- Go to the root directory of your project:
- Install the latest pm2 via npm.
npm install pm2@latest -g
#Start the Application
Use the following command to start your application via pm2:
Replace the app-name variable with the name of your application.
pm2 start npm --name $app-name -- start
Your application is now running via pm2.
To save the pm2 configuration, execute the save command:
#Adding a Cron Job
To ensure, that your application is running after a reboot of your instance, you need to configure a cron job.
- First copy the output of the PATH variable:
The output will look similar to this:
- Edit the crontab for the site user.
- Add the following lines to it:
PATH=$PASTE_THE_OUTPUT_OF_$PATH@reboot pm2 resurrect &> /dev/null
- Reboot your instance and check if the application is running:
The status should be online to confirm that your Application is running after reboot.
PM2 provides a logs command to see the application's output, which helps troubleshoot.