what i am trying to acomplish is to have a php page execute a shell command and display the output on the web page.

the code for the page is something along the lines of

<?php
echo shell_exec('ls -l /code');
?>

when i load up this page all i see is blank. yet when i execute it from the command line i see the command being executed and the output printed.

I am sure its something to do with the permissions and i am hoping someone can point me the right way.

this is what I see in the httpd log file

ls: cannot access /gateway_code: Permission denied
ls: cannot access /gateway_code: Permission denied

BTW the directory /code is set to "Read only" for the user "apache"

thanks! -ankit

link|improve this question

75% accept rate
The error log says...? – Ignacio Vazquez-Abrams Jan 25 '11 at 3:16
Can you execute the command using: sudo su apache-user -c "ls -l /code"? You need to replace apache-user with the actual user defined. – Khaled Jan 25 '11 at 6:13
disabling SELINUX solved the problem. thanks for all he help, much appreciated ! – ankit Feb 24 '11 at 16:11
feedback

3 Answers

Check your base directory restriction. Php doesn't have permission to access /bin/ls. Instead of adding /bin directory to the base directory restriction list copy /bin/ls over to the folder /code.

If you really have to do this.

link|improve this answer
hmm .. but looking at the error log it seems the restriction is on accessing the directory "gateway_code" and not the ls command itself. in any case i went ahead tried it. also changed the permission on the copied ls file to Read and write for all but got the same results – ankit Jan 25 '11 at 4:04
feedback

BTW the directory /code is set to "Read only" for the user "apache"

That's your interpretation of the way the permissions have been set - it would have been a lot more useful if you'd told us what the permissions actually are.

I suspect the directory is not executable by the apache uid. Or there maybe MAC/ACL issues in play here - but you've provided no details of which Linux distribution this is, nor how the permissions are set up.

link|improve this answer
hi, the permission for the directory "gateway_code" are 1704705 drwxr-xr-x. 7 ankit apache 4096 Jan 24 12:52 gateway_code – ankit Jan 25 '11 at 15:06
feedback

The first thing to do is make PHP give you a better answer than just a blank page. You can redirect stderr to stdout using 2>&1 such that the error message will be printed with echo.

<?php
echo shell_exec('ls -l /code 2>&1');
?>

Next, make sure that whatever process is running your php script (ie. apache) has execute permissions on the directory you're trying to list. If you don't mind being heavy-handed, you can easily determine whether permissions are the problem by just giving rwx access to everybody.

> chmod 777 code

And finally, make sure that you're specifying the right path. In your question you say that you're trying to list the contents of /code -- is it really your intention to be listing a root level directory? If the code folder is in the same directory as your script, you might have better luck with a relative path.

echo shell_exec('ls -l ./code 2>&1');
link|improve this answer
yes it is my intention to print the listing for a directory at the root level. tried setting the permission to 777 to see what happens but i still get the same error. – ankit Jan 25 '11 at 4:17
'chmod 777' is NEVER the right way to fix permissions. – symcbean Jan 25 '11 at 12:31
@symcbean I agree that it's not a real solution, but in this case it did eliminate permissions as a possible cause because the problem persisted after applying 777. – Nic Jan 25 '11 at 20:37
feedback

Your Answer

 
or
required, but never shown

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