Score:1

Unable to run python script from PHP

br flag

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 and shell_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.

in flag
Your code works for me. Check your httpd settings. increase the log verbosity if needed.
in flag
You don't need a sudo configuration; you don't need to set the .py file to be executable, since your provide it as a parameter to the python3 binary; python3 can be executed by every user.
cn flag
`/etc/bin/python3`? are you sure of that filename?
Rocky avatar
br flag
@DanilaVershinin Good catch. `whereis python3' gives 'python3: /usr/bin/python3` Still didn't work
Rocky avatar
br flag
@GeraldSchneider Thank you for editing the question, looks perfect now. What should I be looking at in apache config file to run a python from shell? I checked apache error_log file, nothing there. How to increase log verbosity? I thought I already have the max.
Rocky avatar
br flag
@GeraldSchneider The issue seems to have persisted but in a different way. PHP is able to run scripts from my server, except for the directory which is running a Wordpress site. I looked everywhere to see what could be overriding default php.ini directives but couldn't. Any ideas?
Score:0
br flag

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)

ws flag
Something VERY wrong if enabling execute permissions on the script changed the behaviour, and there are lots of other reasons why this can fail (in case anyone else is confused by your fix not resolving their issue). Use the full path to the python interpreter, check your PHP is not subject to chroot or MAC (apparmor, selinux) constraints, has execute permissions on the interpreter and can read the directory tree and script file.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.