通过PHP的shell_exec()从网页执行bash脚本,这需要另一个用户的权限?

I have a couple of bash scripts on a Centos box which I use to do basic server admin stuff like restart services, etc. I run these as a standard user who is also the scripts' owner.

I tried to run these using shell_exec() in PHP, with the apache user, but it simply doesn't work - I'm guessing it doesn't have enough permissions (even with 775 and being in the correct group!) to run everything I want it to.

I've tried editing the sudoers file giving apache permission to run the script calls but it still doesn't work and has no error messages that I can see.

Any thoughts? How can one trigger a script from a web page which requires a different user to run?

  1. check under which user is running apache ( for debian it is www-data)
  2. add www-data in sudoers list with permission to execute files that you like
  3. check which shell has www-data user in /etc/passwd (you will need to give valid shell)
  4. run script with /bin/bash -x (it will output for sure)

Make sure safe mode is off. Also verify the user is the one you expect:

<?php echo exec('whoami'); ?>