This particular question has been asked so many times with great suggestions but the sudden abandonment by Askers doesn't really help as they never bothered to return to tell what worked (assuming it must have worked eventually)
I'm trying to run a python script from PHP but haven't been able to. Environment: PHP 7.3, Python 3.8, CentOS 7, Apache 2.4, Bluehost VPS.
-
Tried both
exec
andshell_exec
-
apachectl -S
reveals apache server is running as nobody:nobody -
Added nobody in sudoer using
nobody ALL=(ALL) NOPASSWD: /etc/bin/python3
-
Added Execute permissions to the Python script and changed the owner
to nobody (same as apache)ls -l testing.py
gives-rwxr-xr-x 1 nobody nobody
-
The Python script on its own runs fine from the shell.
Here's my code:
<?php
error_reporting(E_ALL);
$command = 'python3 /home/uploads/testing.py';
$command = escapeshellcmd($command);
$shelloutput = exec($command,$output, $ret_code);
echo "<h1>";
echo $shelloutput;
echo $output;
echo $ret_code;
echo "</h1>";
?>
The python script:
#!/usr/bin/env python3
import sys
print("Hello")
sys.exit(8)
When I run the PHP file from the browser, I get absolutely nothing.
I would appreciate any help in getting this resolved.
Best Answer
Found that the issue was with php.ini file, removed
disable_functions=""
directive and the Python script worked. As Gerald mentioned in the comments, I did not need to add anything to the sudoers file or change file permissions/users other than the standard+x
permission attribute (or 0755)