PHP – Run shell scripts from PHP as a different (non-root) user

Run shell scripts from PHP as a different (non-root) user… here is a solution to the problem.

Run shell scripts from PHP as a different (non-root) user

I’m trying to run a shell script owned by an unprivileged user via PHP on an Ubuntu server. Permission issues make it impractical to run it using exec() because it is www-data that runs it. I googled a bit and found various references to suExec (which seems too cumbersome for such a minor issue), setting the suid bit, and an interesting approach on Stackoverflow that involves a Python server listening for commands sent via PHP.

Anyway, of all these solutions, I tend to involve the one that sets the suid bit, tried but didn’t work: chmod u+s, and my Apache logs still show permission errors, so I know the script is not running as its owner, but as www-data.

What do I need to do to get it to work, is there an easier solution to fix this?


Check out the man page for sudo: sudo -u your_other_user will let you execute commands as a different user.

You can also use man sudoers to find options for which commands users can execute with sudo.

Related Problems and Solutions