I'm running ubuntu 11.10. I want to be able to call a shell script from php and run that script as root. When my php attempts to call the shell script as root --like this:
$output = shell_exec('sudo /var/www/my_script.sh')
I get this error in the apache log:
sudo: no tty present and no askpass program specified
I've changed the owner of the php script to root:root. I've added this line to sudoers:
www-data ALL = NOPASSWD:/var/www/my_script.php
I realize that I'm supposed to comment out this line in sudoers:
Defaults requiretty
However, there's no such line in my sudoers file. What do I need to do to get past this error?
Your sudoers line is granting access to execute "my_script.php" while your shell_exec is invoking "my_script.sh". When a password is not required, the requiretty option should be irrelevant.
Use "suexec"... in Ubuntu 10.04 server:
Enable suexec:
Edit the config file to match your site:
Then see Using suEXEC on Apache.org to configure the Apache config file(s).
Another approach...
Allow the www-data user to run to run program1 and program2 with no password:
Add to the contents of the sudoers file:
Save.
Yet another is with suPHP
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code: