0

On my website, I have a javascript function that has a POST ajax call to a PHP file on the server (a GoDaddy VPS running CentOS 6), which executes a python script via shell_exec() and returns the output.

Javascript function:

function submitdivsheet()
{
    $.post("test.php", {}, function(data,status){
        console.log(data);
    }); 
}

PHP script: test.php

<?php
    $command = escapeshellcmd('python3.6 test.py');
    $output = shell_exec($command);
    echo $output;
?> 

Python script: test.py

import sys
print("This is the output")

When I try to run the php file from the bash terminal on CPanel, there are no issues, as shown here. However, upon running the javascript function, only an empty string is output to the console. I'm not sure why it would only run on the CPanel terminal, but I suspect it could be a permissions issue somewhere. How can I fix this issue?

NOTE: This works fine on both the server and from the POST call if shell_exec() is not present. It seems that shell_exec is causing the problem.


EDIT: Upon changing the PHP script to:

<?php print shell_exec('echo $PATH'); ?>

This is the output.

4
  • My first guess is that it's a PATH issue. Can you please edit your post to include the output of this test.php: <?php print shell_exec('echo $PATH'); ?>' Does it show a PATH at all? Is your python3.6` in that path? If either answer is no, you'll need to specify the full path to /where/ever/you/have/python3.6
    – Jim L.
    Commented Aug 20, 2019 at 17:51
  • @JimL. added! It shows a path, but python3.6 is not in that path. Commented Aug 20, 2019 at 18:04
  • As my previous comment says then, you'll need to update your test.php file to include the full path to python3.6 in your PHP code. Possibly the full path to test.py also.
    – Jim L.
    Commented Aug 20, 2019 at 18:40
  • @JimL. Thank you! This worked. Commented Aug 20, 2019 at 21:13

1 Answer 1

0

You are missing environment values when invoking python from the web server that are present in your shell. Candidates are PATH and the variables mentioned in the python man page.

Try this:

env -i python3.6 test.py

You will likely get some error messages that will hopefully point to the problem. Then try variables that are present in your shell environment, like:

env -i PYTHONHOME=$PYTHONHOME python3.6 test.py

Add all necessary environment variables. When this works, add them to the call to the script. When calling the script, you have supply the actual values for the variables, you can't refer to them as $PYTHONHOME.

2
  • when I run "env -i python3.6 test.py", the terminal returns "python3.6: No such file or directory" Commented Aug 20, 2019 at 18:29
  • Then one of the missing things is an entry in the PATH. Find out where it is (type python3.6) and add it to the PATH.
    – RalfFriedl
    Commented Aug 20, 2019 at 21:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .