Home Ask Login Register

Developers Planet

Your answer is one click away!

Piwwoli February 2016

PHP: shell_exec a shell script vs. shell_exec a program on linux (permissions)

I have a shell script, which when executed, makes some folders and copies files into them. I also have a node.js "program", which does exactly what the shell script does.

When I call the shell script using PHP's shell_exec, I get a permission error, which basically says that the shell script does not have privileges to make folders. I did some googling and turns out I'd have to setup passwordless sudo and who knows what else trickery to fix this.

However, I made an exact same thing with node and when I run the node "program" by calling it with shell_exec just like before and it seems that this node "program" has the rights to do whatever.

What is the reason behind this? Why do "programs" have privileges when ran, but shell scripts do not? Is this a legitimate way to do, for example, file system operations (calling something made with node.js from PHP)? Because it is annoying to fiddle with the permission if I try to do the same operations with PHP.


Gavriel February 2016

Php was run by apache or something similar and is run under user www-data or http or something like that. Your node server probably runs under piwwoli user. piwwoli can create a directory under /home/piwwoli, but www-data can't.

Both programs and shellscripts have privileges when they run, the question is who is running them.

You can create a directory that is writeable both by www-data and piwwoli:

mkdir /path/to/dir
chmod o+w,g+w /path/to/dir
chown piwwoli.www-data /path/to/dir # maybe you'll need sudo for this to work

Post Status

Asked in February 2016
Viewed 2,834 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life