Search code examples
node.jshapi.jspm2

Run pm2 with super user access


I have a node app that i want to run with root access, I've tried running it with this

sudo pm2 start <my-app> --watch

The reason i really i need it is because in my code there is a command to create a new folder its still failed, just for information i am using hapijs framework inside my node app,

It gave me this error

errno: -13,
0|<my-app> |   code: 'EACCES',
0|<my-app> |   syscall: 'mkdir',
0|<my-app> |   path: 'uploaded_file/122334455/' }

I've tried running pm2 with super user, modifying .pm2 directory with 777 but no luck.

How should i do to fix this issue?

Thankyou.


Solution

  • First giving 777 permission is not a good solution as it will expose your user uploaded content to read by anyone and over that giving 777 permission to .pm2 directory doesn't make any sense as it only stores configurations for pm2 even if you want to go with giving 777 permission you should change permission of /my-app/uploaded_files/.

    Better approach here would be to allow owner of pm2 process to write to /my-app/uploaded_files/ folder which can be done by two ways :-

    • By creating a user group and adding pm2 process owner in that group(create user group and adding user in group in Linux) and then changing /my-app/uploaded_files/ directory's owner to group_name you have created using sudo chown -R :group_name /my-app/uploaded_files/
    • By changing owner of /my-app/uploaded_files/ folder to user pm2 using sudo chown -R user_name: /my-app/uploaded_files/

    Both of above methods work work but using group should be more preferred as using that in future if we want to allow permission to one more user to same directory we can just add one more user to the group and everything else would be same.

    Hope it clarifies let me know in comments if anything needs more clarification.