Tag: pm2

  • How to host static site using pm2

    PM2 is a process manager for node.js applications. You can use PM2 to host static websites.

    To host a static website, create a folder, and put your files inside.

    mkdir /home/website

    Let’s create a static file, say index.html

    echo "Welcome" > /home/website/index.html

    Now you can make the site live using the following pm2 command

    pm2 serve /home/website 8082

    Now the static site will be available at

    http://localhost:8082

    If you have a Single Page Application (SPA) like Angular, React, you can use the option –spa with pm2 command so all non-existent pages get routed to index.html internally.

    pm2 serve --spa
  • Auto Start pm2 on boot

    To auto start pm2 on boot, run

    pm2 startup
    

    This will generate auto start script for your system.

    Once you started all applications, you can save it with

    pm2 save
    

    To uninstall, run

    pm2 unstartup systemd
    

    Running Application as Normal User

    First login as the user, then start the application using pm2.

    pm2 start app.js
    

    Now save it.

    pm2 save
    

    Generate command for auto startup

    pm2 startup
    

    It will show a command. That you need to run as user root. If you have sudo privilage, you can run it with sudo as same user.

    This will create a service with name pm2-USERNAME.

    After reboot, you will be able to check status with command

    systemctl status pm2-USERNAME
    

    See pm2

  • pm2 process manager for node.js

    Auto Start pm2 on boot
    How to host static site using pm2

    pm2 is a process manager for node.js applications. It is similar to forever.

    http://pm2.keymetrics.io

    First you need to install npm, on Ubuntu/Debian, run

    apt install npm

    To install pm2, run

    npm install pm2 -g

    Start an Application

    pm2 start app.js

    Start an Application with name

    pm2 start app.js -n "app-name-here"

    Start npm

    pm2 start npm --name my-app -- run start

    Start the Application in Cluster mode with

    pm2 start app.js -n "app-name-here" -i 5

    To Scale a clustered application, run

    pm2 scale app-name-here 2

    Start the processes on reboot

    pm2 save
    pm2 startup

    List running applications

    pm2 ls

    Controlling running application

    pm2 stop    
    pm2 restart 
    pm2 delete

    Restart all running applications

    pm2 restart all

    nodejs