LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Running shell script with PHP in Apache (http://www.linuxquestions.org/questions/programming-9/running-shell-script-with-php-in-apache-494191/)

mr_scary 10-20-2006 03:57 PM

Running shell script with PHP in Apache
 
What is the best way to run a simple Bourne shell script from a PHP script in Apache?

I thought sudo would do the trick.

Contents of /etc/sudoers:

apache ALL = (<user>) NOPASSWD: /home/<user>/bin/test.sh

The shell script:

#!/bin/sh

echo
echo "This is a test."
echo


The PHP code:

$r = exec("sudo /home/<user>/bin/test.sh", $arrayOutput, $exeStatus);
if($exeStatus == 1)
{
print_r($arrayOutput);
}
else
{
print("Process Failed!");
}

But I keep getting "Process Failed!". What am I missing?

I originally had nologin as apache user shell so I put /bin/sh.

--
Peter

bulliver 10-20-2006 05:06 PM

I am not sure, but I have seen plenty of problems in these forums of people trying to run sudo scripts through PHP. Perhaps you can have a search and see if any of them have been resolved.

Perhaps it is also a permissions issue. Does apache have permission to access /home/<user>/bin/test.sh? Typically your home directory is chmod 700, so apache will not be able to access the script.

As an aside, it is probably better to rewrite whatever code is in this script into pure PHP if possible.

Quote:

I originally had nologin as apache user shell so I put /bin/sh
Please do change that back. apache does _not_ need a login shell to run a script.

mr_scary 10-20-2006 05:17 PM

I gave apache user a shell because I thought it was necessary for sudo to work.


All times are GMT -5. The time now is 06:30 PM.