1

I'm experiencing an odd issue. I have a Python script that I'm calling from a PHP script. This is all running on an Apache server on Ubuntu 18.04. Part of the Python script uses the Google Drive API. EDIT: See the bottom After a lot of testing and replication,I've concluded that simply having the following Google Drive Python libraries and dependencies imported:

from __future__ import print_function
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaIoBaseDownload

Messes up the script when it is called from the web. By that I mean the Python script does not seem to execute, and any shell output that I should be getting through print statements do not make their way back to the PHP script. When calling the script locally (python myscript.py), it works just fine.

The weird part is that when I remove these import statements from the Python script, it executes just fine from both PHP, and from directly launching the script from a browser. In both of those cases I am also able to get the shell output back to the PHP script. I have given the proper permissions for the Python script, and I have configured Apache to be able run CGI scripts. Here is what my Python script looks like:

#!/usr/bin/env python3
import cgitb

from __future__ import print_function
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaIoBaseDownload

cgitb.enable()
print("Hello World")

And here is what my PHP script looks like:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $output = shell_exec("python path_to_my_script/myscript.py");
    echo $output;
?>

EDIT: I ran a couple of tests using another 3rd party Python library that I installed with pip, and this actually seems to be an issue with importing any 3rd party library when executing the script via PHP.

Aerials
  • 4,231
  • 1
  • 16
  • 20

2 Answers2

0

I Figured this out. Hopefully this will help anyone with a similar issue. When you execute a Python or shell script from a PHP script served on an Apache server, the script will be ran as the user, www-data by default.

By running the command sudo -u www-data (running the following command as the www-data user) python myscript.py, I got a traceback error that exclaimed that the 3rd party modules could not be found. What's happening is that by default pip (python package manager) installs its packages on a user level basis. That's fine for most applications, but causes a problem in this case as the www-data user understandably cannot find the installed packages, and the script crashes. This becomes particularly tricky from the perspective of the PHP script, as it doesn't understand this and the output is just null.

I understand that installing pip packages with sudo is not preferred as it can cause conflict issues in some cases; however, this is the easiest way to solve the problem for most packages. For cases where this does not work, check out this answer from infinigrove: How to install Python Package for global use by all users (incl. www-data)

Finally, from what I could see in order to install a pip package as the www-data user (sudo -u www-data pip install package), www-data would have to be granted sudo permissions, which is definitely not a recommended solution.

0

Your issue is a user permissions issue. You should run the Apache server as the user that owns your python environment, or allow the user running the server execute permissions in your python environment.

This answer should help you figure out your best setup for the server.

Also read about Apache VirtualHosts Configuration

Aerials
  • 4,231
  • 1
  • 16
  • 20