23k 23k - 23 days ago 7
Linux Question

Running Python script from Apache server using PHP

I'm working on a project with a raspberry pi to turn on and off an LED strip using a mobile device.

The last issue I'm running into is getting the scripts to run from the apache server being hosted on the raspberry pi.

<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>


<?php
if (isset($_POST['LightON']))
{
exec("sudo python /home/pi/lightOn.py");
}
if (isset($_POST['LightOFF']))
{
exec("sudo python /home/pi/lightOff.py");
}
?>

<form method="post">
<button class="btn" name="LightON">Light ON</button>&nbsp;
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>


</html>


The Python script works perfectly when being run out of the command line on the raspberry pi. But when run on the apache server, nothing happens. What am I doing wrong here? Does it have something to do with permissions? I'm very new to linux and php, please keep that in mind.

Why does the Python work locally but not on the server?

Thanks!

23k 23k
Answer

For anyone that stumbles upon this, the problem was simply fixed by adding sudo in front of the file-path.