Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»Nodejs»Deploying Node Application using PM2

    Deploying Node Application using PM2

    By RahulJune 28, 20222 Mins Read

    PM2 is an advanced, production process manager for Node.js applications. In this tutorial, you will learn how to deploy your Node.js applications on a production server using the pm2 tool. PM2 helps you monitor applications, their memory, and CPU uses. Also, provide easy commands to stop/start/restart all apps or individual apps.

    Step 1 – Install Node.js

    First, you need to node.js PPA in our system provides by the nodejs official website. We also need to install the python-software-properties package if not installed already.

    sudo apt install python-software-properties
    curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    

    After adding the required PPA file let’s install the Nodejs package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.

    sudo apt install nodejs
    

    Step 2 – Install PM2

    Now, Use npm to install the process manager for Node.js using the following command. This will install the latest version of pm2 on your system.

    sudo npm install pm2@latest -g
    

    Step 3 – Start Application with PM2

    Now create a PM2 configuration file. For this tutorial, I have two Node.js applications to host on the production server. The source code applications are available under /var/www/parse-apps/app1 and /var/www/parse-apps/app2 directories. Create a configuration file parse-apps.config.js with the following content. Both applications have an index.js startup JavaScript file to run my application.

    module.exports = {
      apps : [{
        name        : "My App 1",
        script      : "index.js",
        watch       : true,
        merge_logs  : true,
        cwd         : "/var/www/parse-apps/app1/",
       },{
        name        : "My App 2",
        script      : "index.js",
        watch       : true,
        merge_logs  : true,
        cwd         : "/var/www/parse-apps/app2/",
      }]
    }
    

    Now use the following command to start the application with pm2. In the below command, we are passing the parse-apps.config.js configuration file name. PM2 will read the configuration file and start all applications and assign a unique id.

    pm2 start parse-apps.config.js
    

    pm2 start services

    Step 4 – Manage Processes with PM2

    To list all processes registered under PM2 using the following command. This will also display the status of the application, process id, and other useful information.

    sudo pm2 list
    

    To view more details of the specific process, you can use the below command followed by id or process app name.

    sudo pm2 show 1
    

    pm2 show processes

    You can also monitor all processes CPU and memory uses in real-time.

    sudo pm2 monit
    

    pm2 monitor processes

    node node.js pm2
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    A Practical Guide for Reading ‘.env’ Files in Node.js

    Setting Up Angular on Ubuntu: Step-by-Step Guide

    Create a Basic CRUD API in NodeJS, ExpressJS and MySQL

    View 3 Comments

    3 Comments

    1. Pankaj on June 25, 2019 6:29 am

      Nice explanation about deploying node.js Application to Ubuntu Linux . Simple and effective..

      Reply
      • Shrati on June 25, 2019 8:59 am

        Please provide link .Pankaj

        Reply
    2. harish on January 12, 2019 3:26 pm

      pm2 systemd file for centos 7

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.