skip to Main Content

here some difficulties with running python script through apache mod-php

fname='/tmp/catalog.xml'
http=urllib3.PoolManager()
response=http.request('GET','https://xxxx/xxxxx.xml')
doc=response.data.decode('UTF-8')
print('Saving xml...')
with open(fname,'w') as catalog:
    catalog.write(doc)
print('Parsing...')

this code executed from php script and perfectly works when i execute it from cli like that : sudo -u www-data php -f test.php , in the tmp directory appears file catalog.xml with data in it. but when i execute same php script though web browser, the result from executing just string "Saving xml…", and empty file catalog.xml in the tmp directory. somehow data did not written in the file, but it creates

all permissoins are correct, file creates but it is empty, there is no errors at all in the php log file

2

Answers


  1. When you run a Python script through Apache’s mod-php from a web browser, there are several factors that could be causing the issue you described. Here are some steps you can take to diagnose and potentially resolve the problem:

    Check the Web Server Environment:

    Ensure that the web server (e.g., Apache) has permission to write to the /tmp directory. You may need to specifically grant write permissions to the user that Apache runs as (commonly www-data on many Linux distributions). You can do this using the chmod command.

    sudo chmod 777 /tmp
    

    Verify that the PHP script is running as the same user when accessed via the web browser as it is when you execute it from the command line (www-data in your case). This can be important because file permissions and environment variables might differ between the two execution methods.

    Check PHP Configuration:

    Ensure that you are not encountering any PHP execution limits or timeouts. Long-running scripts may be terminated by PHP if they exceed configured limits. You can check your PHP configuration for these limits, such as max_execution_time, memory_limit, and post_max_size.
    Check for Errors:

    Since you mentioned that there are no errors in the PHP log file, consider adding error handling in your PHP script to catch any potential issues that might not be logged. For example, use try-catch blocks to catch exceptions and log them to a file.

    try {
        // Your Python script here
    } catch (Exception $e) {
        error_log('Error: ' . $e->getMessage(), 0);
    }
    

    Additionally, you can check for errors in the Python script itself by adding error handling around the Python code, such as checking the return value of with open() and logging any errors.

    Debugging:

    Add debugging statements to your PHP script to help narrow down the issue. For example, print out the value of the doc variable after decoding the response to ensure it contains the expected data.

    echo 'Response Data: ' . $doc;
    

    You can also use the error_log function in PHP to log custom messages that can help you trace the script’s execution path.

    Environment Variables:

    Verify that the environment variables necessary for your Python script are properly set when it runs through the web server. Environment variables can differ between CLI and web server environments. You can use getenv() in PHP to check the values of environment variables.

    echo 'Python Path: ' . getenv('PATH'); // Check the PATH variable
    

    SELinux or AppArmor:

    If you are using SELinux or AppArmor, make sure that these security modules are not preventing the Apache process from writing to the /tmp directory.
    By systematically checking these factors, you should be able to pinpoint the issue and resolve it. Be cautious about changing permissions and make sure to secure your web server properly after troubleshooting.

    Login or Signup to reply.
  2. Could we try to add some error handling there? To see if there is anything that could be happening in desperation?

    Like:

    fname='/tmp/catalog.xml'
    http=urllib3.PoolManager()
    response=http.request('GET','https://xxxx/xxxxx.xml')
    doc=response.data.decode('UTF-8')
    print('Saving xml...')
    try:
       with open(fname,'w') as catalog:
          catalog.write(doc)
    except Exception as e: 
        print(e)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search