Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
You cannot use SUDO or SU in PHP. –  Johnson Nov 7 '11 at 22:15
    
Why can't you ? –  João Pinto Nov 8 '11 at 0:09
1  
You can: askubuntu.com/questions/64465/… –  user8290 Jan 4 '12 at 1:51

2 Answers 2

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.

share|improve this answer

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
WWW_USER ALL = (ALL) NOPASSWD: WWW_COMMANDS

Save.

Yet another is with suPHP

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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