Keep application alive forever with PM2


  • Administrators

    PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

    More info: https://www.npmjs.com/package/pm2

    Installing PM2 and starting our application

    To install PM2 enter next command

    npm install pm2 -g
    

    After the installation lets start our application with PM2, we will also add a name for our website, in this case it is named cod4xwebadmin

    Navigate to the application root and type (if you have multiple applications repeat the step for each)

    pm2 start npm --name "cod4xwebadmin" -- start
    

    Our application is now started, even if we exit putty the website will still be alive.

    PM2 Commands

    Since we added a name cod4xwebadmin we can now execute pm2 commands only for this application (if you run multiple applications you can add a unique name for every application and this way you can handle them individually)

    Some basic commands:

    Start the cod4xwebadmin application

    pm2 start npm --name "cod4xwebadmin" -- start
    

    Stop

    pm2 stop cod4xwebadmin
    

    See the console log when it is started

    pm2 log cod4xwebadmin
    

    if you would have another application differently named then replace cod4xwebadmin with the correct name

    Or if you would like to start | stop all applications at once you can use all instead the application names, for example stop all applications you type

    pm2 stop all
    

    What if i restart my server, will the application be started automatically?

    In order to start on system boot we need to execute next commands
    Make sure you have started all applications and they run before you execute the commands

    To detect available init systems on your machine and generate a configuration, use:

    pm2 startup
    

    Save your process list with:

    pm2 save
    

    That's it, with this 2 commands our website will automatically start on system start


Log in to reply
 

480
107
90
Visitors Today
Powered & Designed by NeHo