phpbashshell-exec

Impossible to call a bash file with shell_exec with PHP


I am managing a Rocky Linux 8 server. I would like to be able to execute bash files from a webpage.

Here is the information about the structure:

Here is the content of test.sh file:

#!/bin/bash echo 'hello';

1/ From my console as root user, when I execute "/root/test.sh", it's working fine!

2/ From my PHP file, if I run the following code, it's working too.

$test = shell_exec('date'); echo $test;

3/ From my PHP file, if I run the following code, it's not working :

$test = shell_exec('/root/test.sh'); or $test = shell_exec('sh /root/test.sh'); or $test = shell_exec('/usr/bin/sh /root/test.sh'); echo $test;

It's not working and it is so frustrating...

I have been searching for hours and I do not find any solution. Would you please help me solve this issue. Maybe is the Apache Configuration that is wrong...

Thank you so much!


Solution

  • I found a solution by adding she bash file in the web folder with the apache:apache permission. Using shell_exe and calling a bash file outside de web project doesn't work.

    The structure that I use is the following:

    This will protect my bash files as the folder "app" is not accessible from the web. Also the good syntax that works for me in my index.php is:

    $test = shell_exec('sh ../app/test.sh');
    echo $test;
    

    I hope it will help some other people.

    Thanks for you help!