How to Install PM2 in Ubuntu 22.04

Install pm2 in ubuntu 22.04; Through this tutorial, we will show you how to install and configure pm2 in ubuntu 22.04 using terminal or command line.

How to Install PM2 on Ubuntu 22.04

Follow the following steps to install and use pm2 with the node js app on ubuntu 22.04:

  • Step 1 – Install PM2 Using NPM
  • Step 2 – Build Node.js App
  • Step 3 – Start Node JS Application Using PM2
  • Step 4 – Manage the Application

Step 1 – Install PM2 Using NPM

Execute the following command on terminal or command line to install PM2 via the NPM package manager:

npm install pm2 -g

Step 2 – Build Node.js App

Create a new file named app.js in your favorite text editor.

Paste the following contents into the file.

const http = require('http');

const hostname = '0.0.0.0';
const port = 80;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Exit and save the file.

Step 3 – Start Node JS Application Using PM2

Execute the following command on terminal to start a Node.js application with PM2:

pm2 start app.js

Note that :- we may use several additional parameters when starting an application:

  • --name <app_name> – specify an application name
  • --watch – restart your application automatically when a file changes
  • --max-memory--restart <100MB> – a memory limit that triggers an automatic restart when reached
  • --log <log_file> – specify a log file
  • -- arg1 arg2 – pass extra arguments to the script
  • --restart-delay <2000ms> – delay between automatic restarts in milliseconds
  • --time – prefix logs with time
  • --no-autorestart – turn off automatic application restarts
  • --cron – a cron job pattern for automatic application restarts
  • --no-daemon – do not run the application in a daemon process

Visit the official documentation to view all available parameters.

Step 4 – Manage the Application

Let’s use the following commands on the terminal to restart, reload, stop and delete operations in node js app with pm2.

To restart the application.

pm2 restart app

To reload an application.

pm2 reload app

To stop an application.

pm2 stop app

To delete an application.

pm2 delete app

Conclusion

Install pm2 in ubuntu 22.04; Through this tutorial, we have learned how to install and configure pm2 in ubuntu 22.04.

Recommended CENTOS Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *