Process managers like PM2

Process managers like PM2

tools: forever, supervisord, pm2

Assume we have working node.js.

It is possible to use SystemD as it is the default process manager on most Linux environments, but we have better ideas here as PM2 can be used also with ReactPHP for example.

Running Nodejs in production How To Set Up a Node.js Application for Production on Debian 10 | DigitalOcean

Install globally:

npm install pm2 -g
# ensure pm2 starts on boot
pm2 startup
# service pm2-root status

You can also start pm2 at startup as other user with:

sudo env PATH=$PATH:/usr/local/bin pm2 startup -u safeuser

Let’s create our node.js application with the following lines of code:

cd ~/service

cat <<'EOF' > hello.js

const http = require('http');

const hostname = 'localhost';
const port = 4444;

http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

EOF

Test with:

pm2 start hello.js

This simple command will start your app and will ensure automatic restart if it crashes, and automatic start on server reboots.

For more command examples, this is perfect: https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

pm2 start --name app_name app.js
pm2 start app_name --watch

pm2 list

pm2 stop app_name
pm2 reload app_name
pm2 restart app_name

pm2 logs app_name
pm2 monit
pm2 info app_name

Node in ISPConfig

How to Deploy Node JS to Ubuntu 20.04

nginx config:

location /service/hello {
  proxy_pass http://localhost:444;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

You can add additional location blocks to the same server block to access other applications on the same server.

PHP process manager?

  • seregazhuk/php-watcher
    Automatically restart PHP application once the source code changes. Same as pm2/forever/nodemon but written in a pure PHP and available via Composer
date 31. Oct 2020 | modified 17. Jan 2023
filename: Linux » Process Managers