Trying to run a python script via PHP button hosted on raspberry pi but failing
<p>I have a php script that should (I think) run a python script to control the energenie radio controlled plug sockets depending on which button is selected. It seems to work in that it echos back the correct message when the button is pressed but the python scripts don''t appear to run. I have added the line:</p>

<p>www-data ALL=NOPASSWD: /usr/bin/python /home/pi/lampon.py</p>

<p>which should give the apache user privileges to run the python script at least for turning on the power socket but it doesn't work. The script itself does work when run via the pi command line itself. Any suggestions? (the code for the php is below)</p>

<pre><code>&lt;html&gt;
 &lt;head&gt;
 &lt;meta name="viewport" content="width=device-width" /&gt;
 &lt;title&gt;LED Control&lt;/title&gt;
 &lt;/head&gt;
         &lt;body&gt;
         LED Control:
         &lt;form method="get" action="energenie.php"&gt;
                 &lt;input type="submit" value="ON" name="on"&gt;
                 &lt;input type="submit" value="OFF" name="off"&gt;
         &lt;/form&gt;
         &lt;?php
          if(isset($_GET['on'])){
                 shell_exec("python /home/pi/lampon.py");
                 echo "LED is on";
         }
         else if(isset($_GET['off'])){
                 shell_exec("python /home/pi/lampoff.py");
                 echo "LED is off";
         }
         ?&gt;
         &lt;/body&gt;
 &lt;/html&gt;
</code></pre>

<p>Have you tried setting the permissions of the <code>lampoff.py</code> and <code>lampon.py</code> to 777?</p>

<p><code>chmod 777 /home/pi/lampoff.py &amp;&amp; chmod 777 /home/py/lampon.py</code></p>

php python raspberry-pi