23k 23k - 1 year ago 71
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.

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

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>


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?


23k 23k
Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download