1

I am struggling with under which accounts I should be running the daemon and how to add applications.

I currently have it under root.
PM2 v4.2.3: God Daemon (/root/.pm2)

But when I run my bash script to add another application, I am logged in as brad with sudo permissions. So it then wants to start another daemon as

PM2 v4.2.3: God Daemon (/brad/.pm2)

and add the application to the brad instance, not the root one.

I am not sure just how to structure this. Ultimately the goal is to have a php webpage run the bash script to add the application. The nginx web server and php-frm run as www-data user. So am I supposed to run the God Daemon as that user as well and give www-data sudo permissions? I am worried about security here. I would appreciate any help on this as I think it is pretty important on getting it right. I am using ubuntu 18.4

1 Answer 1

2

Welcome to StackExchange. It is recommended that you run PM2 under the same (isolated) user as the apps that you wish to control, in order for it to work simply.

First setup node code as a local user, and make sure to execute all 'npm' commands as the local user, not root only:

  • If you run 'npm -g install xyz' as root, the packages go to "/usr/local/lib/node_modules/". "-g" for global, else they go to the root user directory.

  • If you run 'npm install xyz' as a user, the packages go to "/home//.pm2/node_modules/"... which is the method I recommend.

Then, to get a correct startup script for PM2, do 'pm2 startup -u <username> upstart' for older distros (eg. Ubuntu 14.04) or 'pm2 startup -u <username>' for newer distros that use systemd (script tries to auto-detect), where '<username>' is 'brad' in your case, or whatever else you choose. We created a separate user for our nodeJS apps, so that everything is isolated under the one user, which also makes it easier to manage. However, be aware that if you then login as "brad" or "root" and run pm2, it will run against the logged-in user's directory, and will thus not show the apps running under the other users. I.e. running pm2 as "brad" will show apps managed under "brad".

There are other ways of setting things up, according to the pm2 man pages, but I found that it did not work nicely.

3
  • Thanks, that helps. Do you know how much more ram/cpu is used to use pm2 as apposed to just running the node.js server with systemd? I like the ease of PM2 just not sure I can afford the over head, any thoughts?
    – Daxcor
    Jan 23, 2020 at 16:15
  • Checked with 'ps -o pid,user,%mem,command ax | sort -b -k3 -r' and my PM2 manager (runing 3 web instances) is using 0.1% of 62G, which includes the apps themselves I think... so hard to differentiate. Edit: node apps running separate at < 0.1%. So yes, it adds some overhead.
    – sarlacii
    Jan 28, 2020 at 9:13
  • Oh, and @Daxcor, if this was helpful, please tick the "accepted answer" tick, and/or vote up with "This answer was useful" arrow. It helps others find useful replies.
    – sarlacii
    Jan 28, 2020 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .