Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8374

Python • Can not call Python scripts with PHP in Bookworm or Trixie

$
0
0
Issue / Problem: I can not use PHP to run Python scripts that DO work from command line.

Setup: Raspberry Pi 5 (8gb) running updated Bookworm or Trixie, and all the latest Apache, PHP, MariaSB, Python3, Pi-Plates libraries
Additional Hardware: Pi-Plates THERMOplate, RELAYplate2
Objective: I am porting an industrial heating system control I wrote several years for a Pi 3b to the newest stuff.

BACKGROUND

We have been using a Pi 3b and the Raspbian OS from several years ago to serve a simple web page on our LAN that would control some industrial heating units as a programmable thermostat but it's time to update it to the latest Pi and OS.

We were using a relay module from Pi-Plates and their I/O board. Now we are using their THERMOplate and RELAYplate2, but I don't think these hardware components are the issue because I can't even run simple test Python scripts from PHP calls.

Again, all the scripts I am trying to run from PHP work from command line.

Yes, I have done all the obvious with respect to permissions and ownership, particularly ensuring scripts are in the www-data group and / or that www-data owns the files.

I have tried fresh installs of both Bookworm and Trixie.

Here's some of the code I have tried:

PHP

Code:

<?php$command = escapeshellcmd('sudo /home/pi/python_venv/venv/bin/python /var/www/html/cgi-bin/test.py');echo $command;$output = shell_exec($command);echo "<br>Relays state: ";echo $output;echo"<br>";?>
NOTE: I have tried the command both with and without "sudo". I have tried both shell_exec() and exec(). And I have tried bypassing escapeshellcmd().

Python test.py

Code:

import sysif len(sys.argv) > 1:    data_from_php = sys.argv[1]    result_string = f"Python processed this data: {data_from_php}"else:    result_string = "Hello from Python!"print(result_string)
After many hours of trying, I am left wondering if something has changed in Apache, PHP or something else that does not allow PHP to call Python scripts.

Any help would be greatly appreciated!

Statistics: Posted by CZPi — Sun Mar 01, 2026 11:34 pm



Viewing all articles
Browse latest Browse all 8374

Trending Articles