0

When I call a simple python-script that blinks LED once, from a PHP-script via browser, nothing happens.

and I add these lines in the sudoers file.

apache ALL=(ALL) NOPASSWD: ALL
apache2 ALL=(ALL) NOPASSWD: ALL
www-data ALL=(ALL) NOPASSWD: ALL
adm ALL=(ALL) NOPASSWD: ALL

/var/www/html owner&Group is www-data and python script owner&Group is www-data also

what is the problem?

<html>
<head>
<?php
if (isset($_POST['RedON']))
{
exec('sudo python /var/www/html/xbee.py');
}
if (isset($_POST['RedOFF']))
{
exec('sudo python /home/pi/xbee2.py');
}
if (isset($_POST['YellowON']))
{
exec('sudo python /var/www/html/xbee2.py');
}
if (isset($_POST['YellowOFF']))
{
exec('sudo python /var/www/gpio/yellow_off.py');
}
if (isset($_POST['GreenON']))
{
exec('sudo python /var/www/gpio/green_on.py');
}
if (isset($_POST['GreenOFF']))
{
exec('sudo python /var/www/gpio/green_off.py');
}
?>

  <title></title>
</head>
<body>
<form method="post">
  <table
 style="width: 75%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
    <tbody>
      <tr>
        <td style="text-align: center;">Turn LED on</td>
        <td style="text-align: center;">Turn LED off</td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="RedON">Red On</button></td>
    <td style="text-align: center;"><button name="RedOFF">Red Off</button></td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="YellowON">Yellow On</button></td>
    <td style="text-align: center;"><button name="YellowOFF">Yellow Off</button></td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="GreenON">Green On</button></td>
    <td style="text-align: center;"><button name="GreenOFF">Green Off</button></td>
  </tr>
</tbody>
  </table>
</form>
</body>
</html>

Not: python script works in SSH Normally.

12
  • I'm not sure what you're problem is right off the bat, but there's a trick that might help us debug this a little easier. Wrap your exec statements like this: echo(exec('sudo python /var/www/html/xbee.py 2>&1'));.
    – hobenkr
    Commented Dec 1, 2016 at 4:13
  • I added two things there. First, echo the output of your exec statements. That will print anything that your python call returns to the webpage. That only gets you part of the way there though because whatever errors occur are printed to stderr instead of stdout. By adding 2>&1 following your python script, that will redirect the output that's meant for stderr to stdout so that echo can print it. Give that a try and let me know what errors show up.
    – hobenkr
    Commented Dec 1, 2016 at 4:18
  • You're also probably aware, but this is not a good practice to do on public facing pages :) It's a bit revealing about your system.
    – hobenkr
    Commented Dec 1, 2016 at 4:33
  • I triad before the same PHP and python script in the same Raspberry pi, and it is worked, because there was an error in Raspberry OS, I was format it, and when I trying to install the same script again don't work. Commented Dec 1, 2016 at 7:36
  • 1
    Actually, it could be as simple as the fact you've called your file index.html not index.php so it's not running through the interpreter.
    – calcinai
    Commented Dec 1, 2016 at 16:50

3 Answers 3

1

This is a problem a lot of people face, and it's not always just due to a lack of sudo permission.

A couple of possible causes could be:

  • Permission/sudo (as you are already aware)
  • python might not be available in the www-data user's environment

Since you've been quite aggressive with your sudo permissions, it's probably not that. There are two main ways (that I can think of) to resolve the second possibility.

  • Use the complete path to python (e.g. /usr/bin/python) in your PHP script. You can make sure you're using the same one as when you SSH by typing which python
  • Make your scripts directly executable. This would require 3 steps:

    1. Specify the interpreter (Python) with a shebang at the start of your python scripts. A common way to do this is #!/usr/bin/env python
    2. Add the execute permission chmod +x /var/www/gpio/x.py
    3. Change your PHP code to reflect this exec('sudo /var/www/gpio/x.py');

Alternatively

I've been working on a php library to overcome this exact issue. It's native PHP and provides an asynchronous api for GPIO manipulation. It doesn't require sudo for basic operations provided you add the www-data user to the gpio group.

0

I had a similar issue trying to run Python scripts through PHP in a browser.

I believe the problem relies on the code in these Python files. Probabily in your python code you import libraries that access some devices that need permission.

So you need to set the permission to the device itself, not to Phyton or the Python script.

For example, if your Python code reads data from the USB, you need to set the right permission to the right ttyUSB file in the device folder: sudo chmod 777 /dev/ttyUSB0

0

You can use phpy - library for php.

PHP file:

<?php
  require_once "vendor/autoload.php";

  use app\core\App;

  $app = new App();
  $python = $app->python;
  $output = $python->gen(your python path,data....);
  $python->dump($output);

Python file:

import include.library.phpy as phpy
print(phpy.get_data(number of data , first = 1 , two =2 ...))

You can see also example in github page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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