0

I am trying to call a python script from PHP but not having any luck. I have searched for hours but found nothing. The python script is running just fine when I call it from the command line(connected to a relay switch, just runs through them, turning them on and off) and it works just fine. However, I can't seem to figure out how to get it to run from PHP. I am very new to PHP but here is what I am using:

<!doctype html>
<head>
    <meta charset="UTF-8"/>
</head>

<?php
    if(isset($_POST['switch'])){
        exec("sudo python /home/pi/Desktop/test.py");
    }
?>

<form method="post">
    <button name="switch">Switch</button>
</form>

</html>

What am I doing wrong? I can't seem to find an answer anywhere that will make it work. The PHP is displaying the button just fine, but it does nothing when I click it.

Lee Morgan
  • 550
  • 1
  • 6
  • 26

2 Answers2

2

shell_exec — Execute command via shell and return the complete output as a string . reference

<?php
    if(isset($_POST['switch'])){
      $c=escapeshellcmd("sudo python /home/pi/Desktop/test.py");
      $res=shell_exec($c);
      echo $res; // returns result to display
    }
?>

in your script,output is not printed that may seem to not working

    <?php
        if(isset($_POST['switch'])){
               $s=exec("sudo python /home/pi/Desktop/test.py");
           echo "$s";        
}
    ?>

add full path of interpreter in the first line of python script . if you have installed more than one python version

$s=exec("sudo -u /home/pi/Desktop/test.py"); this gives permission to python file

first of all make python file executable with chmod +x /path/to/python-script.py

EDIT:

from this post

You can't use sudo from a PHP script. Apache is running from an user (www-data generaly), so edit this file : /etc/sudoers

Then add this line :

www-data ALL=(ALL) NOPASSWD:ALL

Care ! this will authorize all functions to be called by a PHP script, you can adapt changing "ALL" by your script or Python command.

Then precise your user in your exec command :

<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/script.py')
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
  • Tried this, no change. Still absolutely nothing happens when I click the button. – Lee Morgan Jan 13 '18 at 21:52
  • So I am getting some errors in the log now. I think it is permissions. "sudo: unknown user: /home/pi/Desktop/test.py" and "sudo: unable to initialize policy plugin" What is that and how do I fix it? I ran the chmod command. Not sure how to fix that error. – Lee Morgan Jan 14 '18 at 21:03
  • reason is username is not specified in exec () it should be like this`"sudo -u /home/pi/Desktop/test.py"` – jasinth premkumar Jan 15 '18 at 07:38
  • reason is username is not specified in exec () it should be like this`"sudo -u /home/pi/Desktop/test.py"` check updated answer – jasinth premkumar Jan 15 '18 at 07:42
  • So it turns out it was permissions and I had to edit sudoers. I had previously tried this so I thought it was something else. However, I made a mistake typing it in. Fixed it an everything works great. – Lee Morgan Jan 17 '18 at 01:42
0

Please make sure that the www user has the permission to execute your python script.

and then you should check if the system could find the PATH of the python libraries that you import in your python code.

I have the same experience with you, and I fixed the problem by checking the apache2 error_log, you'd better try, The error_log will tell what the real problem is !

cd /var/log/apache2
sudo more error.log

chmod 777 test.php
chmod 777 test.py

Good luck!

  • Please add more helpful info and/or necessary code-snippets, or in a more formatted manner to help the users. – Muntashir Akon Jan 13 '18 at 13:30
  • I checked the error log and nothing shows up, only two entries from when the server first starts, nothing else there. Then I tried to add permissions to /etc/sudoers. Still nothing. Maybe I am doing it wrong. How do you give permissions to execute the script? – Lee Morgan Jan 13 '18 at 21:51