1

PHP can't find the python on my computer. When I tried to access python through php using the code below, it returned nothing.

The server is built on xampp

The php code:

<?php
$output=shell_exec('/usr/bin/python3 -V');
echo $output;
?>

What might be wrong?

  1. I have tried python3 -V on the terminal, and it displayed the version of python installed.
  2. I saw that the reason might cause this problem is that the folder holding python does not have permission, so I sudo chmod 777 all the folders on the path, but it doesn't change anything
  3. using sudo -u www-data /usr/bin/python3 -V shows that the user is not allowed to access python.
henry Chan
  • 59
  • 7
  • So the output is falsy, but is it null or false? (check https://www.php.net/manual/en/function.shell-exec.php), as this is a important debugging factor. and add 2>&1 at the end, it may help. – Dxg125 Jun 12 '23 at 11:09
  • Php is executed with user `www-data` in most cases unless you are running it in terminal with your own user permission and `www-data` might not have execution rights to python. You can try to see what happens when you do `sudo -u www-data /usr/bin/python3 -V` – Bijay Regmi Jun 12 '23 at 11:24
  • @Dxg125 After adding ```2>&1``` it shows permission denied. But I have chomod all the folders in the path, is it something wrong with the user or something else? – henry Chan Jun 12 '23 at 11:32
  • @BijayRegmi using ```www-data``` and ```daemon``` the terminal shows that the user is not allowed to execute python through ```www-data``` (or ```daemon```) on the master, I think this is the problem, but the user is in the sudo group on the master, so is there something that I can change? – henry Chan Jun 12 '23 at 11:59
  • did you try out the steps from: https://stackoverflow.com/questions/32066800/php-shell-exec-permission-denied. what is your WhoAmI result? and check the comment below the answer, as you may have the exact same problem. – Dxg125 Jun 12 '23 at 14:16
  • @Dxg125 The result is ```daemon```, after changing the owner of the folder, It showed ```Python3: XML_SetHashSalt```, I think it has something to do with the python environment, but after searching the net, nothing helped, do you know how to solve this problem? – henry Chan Jun 13 '23 at 01:17
  • I'd say if you are logged onto as daemon, you can either go ahead and give the permission to this exact user (maybe it is www-data/daemon), but im no linux expert, far from it. Or you login onto the system with another user (which probably is best in terms of security) and only give permission(s) for the required tasks (shell_exec can be unsafe). if youre on a test enviroment just give 777 to all and see if it works, if not, youre in a hell of trouble as then its probably the py env that blocks something. – Dxg125 Jun 13 '23 at 07:08
  • @Dxg125 Thanks for the advice, I will try it later!!! – henry Chan Jun 15 '23 at 02:13

0 Answers0