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:

sudo apt-get install apache2-suexec-common

Enable suexec:

sudo a2enmod suexec

Edit the config file to match your site:

sudo -e /etc/apache2/suexec/www-data

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:

sudo visudo

Add to the contents of the sudoers file:

User_Alias WWW_USER = www-data
Cmnd_Alias WWW_COMMANDS = /sbin/program1, /sbin/program2


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:

//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/var/www/my_script.sh');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.