PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] Apache Permission Error on open file in Python

 December 29, 2021     apache, centos8, lamp, php, python     No comments   

Issue

I am new to web design, and I'm trying to setup an Apache web interface to some python code. I am running a test python script that just opens a new file in /var/www/html/ called output.txt and attempts to write to it. When the web page is loaded, I use shell_exec('/usr/bin/python3 /var/www/html/ptest.py'); to run my python script. The python script does run and outputs some print messages, but trying to open output.txt fails with: Traceback (most recent call last): File "/var/www/html/ptest.py", line 8, in f=open("/var/www/html/output.txt", "w") PermissionError: [Errno 13] Permission denied: '/var/www/html/output.txt'. It will also fail if the file already exists and I open to read.

I have confirmed that the script is being run by apache with getpass.getuser(), and I have tried as many different permission combinations as I can think of for /var/www/html/, including 777. I have set apache as the group for every directory from /var/ to html/. I have tried creating the file ahead of time with 777 permissions. I have checked that /var/ to html/ has group execution permission. I have tried creating and using another folder completely owned by apache.

I have looked through the apache directives to see if there is a directive I need to have, but I haven't found one yet.

I've included the code for my python script and my php page below.

EDIT: I've tried running ptest.py as apache using su -s /bin/bash apache.

ptest.py successfully runs this way, so the problem doesn't seem to be a permission associated with the apache user/group.

ptest.py

#!/usr/bin/python3
import sys
import getpass
import os
sys.stderr = sys.stdout
print(getpass.getuser())
print(os.getgid())
f = open("/var/www/html/output.txt", "w")
f.write("banana")
f.close()
print("I wrote banana, which is a berry")

banana.php

<html>
    <head></head>
    <body>
        <h2>Welcome to the Program Test</h2>
        <?php
            echo "Created Command", "<br>";
            $output = shell_exec('/usr/bin/python3 /var/www/html/ptest.py');
            echo "Executed Command", "<br>";
            echo $output, "<br>";
            echo "End of output", "<br>";
            $output = shell_exec('ls /var/www/html/');
            echo $output;
        ?>
    </body>
</html>

Solution

Credit to domino_pl leaving a comment.

It looks like typical selinux problem. Try selinux setenforce 0 – domino_pl



Answered By - Bill Jobs
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing