Why Does a PHP Program Call to a Python Program Work from the Back-End but Not from the Front-End?

Problem scenario
In your PHP program, the Python binary is executing regardless of how the PHP program is run (e.g., from a command line with the php command or when downloaded from a web browser). The PHP program will launch Python programs if the PHP program is invoked from the back-end with PHP. But Python programs are not being interpreted by Python when a web browser loads the PHP file.

What could be wrong with calling it from the front-end?

Possible solution
This assumes that PHP and Apache2 have been installed on your server. If you need assistance, run this if you have Ubuntu/Debian Linux: sudo apt -y install php apache2

If you are using Debian/Ubuntu Linux, make sure that the .py program is owned by www-data and associated with the www-data group. This is a security concern. Only do this if you know what you are doing. If regular website users can invoke a Python program, there could be serious consequences to this.

In some instances different users or groups may be in play (besides www-data). To find the relevant user or group, use this PHP program and view it from a web browser:


   $output = shell_exec('/usr/bin/python3 --version');
   echo $output;

   $output1 = shell_exec('/usr/bin/env');
   echo $output1;




The output will give you the Linux back-end environment variables. Look for the APACHE_RUN_USER and APACHE_RUN_GROUP variable settings. This should help you determine what user and group to associate your .py file.

We are not sure about the solution for CentOS/RHEL/Fedora. If you need to start all over, see How Do You Get a PHP Program to Invoke a Python Program via Browsing a Website?

Leave a comment

Your email address will not be published. Required fields are marked *