python - Flask/wsgi - 写入日志文件或接触随机文件时权限被拒绝

标签 python apache logging flask mod-wsgi

我正在编写一个非常简单的 Flask 程序来记录 POST 有效负载。它在调试中运行良好,所以我将它连接到 apache。现在我得到
“IOError: [Errno 13] Permission denied:”当试图在除/tmp 之外的任何地方登录时。

我在 RHEL 7.x 上运行 apache v2.4.6 和 wsgi v3.4。
这是我的东西:

虚拟主机文件:

Listen *:8080
<VirtualHost *:8080>
    ServerName www.test.com
    ErrorLog /var/log/httpd/error2.log
    CustomLog /var/log/httpd/access2.log combined

    # didn't work
    #<Directory />
    #    Options Indexes FollowSymLinks Includes ExecCGI
    #    AllowOverride All
    #    Require all granted
    #</Directory>

    WSGIDaemonProcess csplogger user=apache group=apache threads=5
    WSGIProcessGroup csplogger
    WSGIScriptAlias /report-to /var/www/FLASKAPPS/csplogger/csplogger.wsgi

</VirtualHost>


csplogger.wsgi 文件:

#!/bin/python
import sys

sys.path.insert(0,"/var/www/FLASKAPPS/")

from csplogger import app as application


和 python :

from flask import Flask, request
import json
import os
import logging
from logging import Formatter, FileHandler
from logging.handlers import RotatingFileHandler

app = Flask(__name__)

if not app.debug:

    logdir = '/opt/logs/'

    file_handler = FileHandler('/tmp/access.log')
    #file_handler = FileHandler(logdir + 'access.log')
    #file_handler = RotatingFileHandler(logdir + 'access.log', maxBytes=20000000, backupCount=10)
    file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s'))

    logging.getLogger('werkzeug').setLevel(logging.INFO)
    logging.getLogger('werkzeug').addHandler(file_handler)

    app.logger.addHandler(file_handler)
    app.logger.setLevel(logging.INFO)
    
    # adding some debug
    app.logger.info(os.environ)
    app.logger.info(os.path.dirname(logdir).format())
    app.logger.info(os.listdir(logdir))

@app.route('/', methods=['GET', 'POST'])
def home():

    if request.method != 'POST':
        app.logger.info('GET testing'.format())
        return ('not a post\n', 200)
    else:
        data = request.get_json()
        app.logger.info(json.dumps(data))
        return ('', 204)

if __name__ =='__main__':

    app.run(host='0.0.0.0', port=5000, debug=False)


/opt/logs 是预期的日志目的地。它是 chown'd apache:apache(这是我的 apache 用户/组)并且完全打开。我知道 python 应用程序可以找到这些目录,因为我正在列出内容并将其记录(登录到/tmp/access.log 时)作为调试的一部分。我提到这是 RHEL,但我没有提到 SELinux 已禁用。

长话短说,我可以在一个目录中看到文件,这两个文件都是 777 并且由 apache 用户拥有,但我无法写入这些日志文件。

谢谢你的时间。任何帮助将不胜感激。

最佳答案

我遇到了这个问题,我解决它的方式可能不是解决这个问题的直接方法,但它对我有用。

而不是配置 app.logger ,我直接从 logging 包中创建了记录器:

if not app.debug:

    logdir = '/opt/logs/'

    file_handler = FileHandler('/tmp/access.log')
    #file_handler = FileHandler(logdir + 'access.log')
    #file_handler = RotatingFileHandler(logdir + 'access.log', maxBytes=20000000, backupCount=10)
    file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s'))

    logging.getLogger('werkzeug').setLevel(logging.INFO)
    logging.getLogger('werkzeug').addHandler(file_handler)

    logger = logging.getLogger(__name__) 

    logger.addHandler(file_handler)
    logger.setLevel(logging.INFO)

    # adding some debug
    logger.info(os.environ)
    logger.info(os.path.dirname(logdir).format())
    logger.info(os.listdir(logdir))

保留 app.logger 的实际解决方案到位也可能与此有关。类似:app.logger = logging.getLogger(__name__)但我自己没试过。

关于python - Flask/wsgi - 写入日志文件或接触随机文件时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49663420/

相关文章:

python - Django vs Python2.7 单元测试测试用例?

apache - 如何验证 Apache 缓存是否正常工作?

python - 将日志记录配置包含在 configparser 读取的文件中

c++ - 如何在 C++ 中读取不断增长的文本文件?

Python:迭代一个列表,但我想在循环中添加到该列表并继续迭代添加的内容。

python - 谷歌应用引擎: Failing to Upload Data

python - 元组到字符串,每个元素都有一个函数

java - 如何为无法使用 xssf 方法打开的巨大 xlsx 文件着色

apache - mod_rewrite 和双斜杠问题

java - 记录操作的优雅方式?