mod_wsgi 中的烧瓶 运行 无法写入 /tmp

flask running in mod_wsgi cannot write to /tmp

Apache v2.4.12-2

Mod_wsgi v4.4.8-1

Python v3.4.2

python-flask v0.10.1-5

Arch linux - 内核 3.12.36

我正在使用 mod_wsgi 和 flask 来托管服务器。我可以使用以下简化代码和通用 .wsgi 脚本重现此问题:

MainServer.py:

import flask

app = flask.Flask(__name__)

@app.before_first_request
def initstuff():
    test_file = '/tmp/test'
    with open(test_file, 'w') as f:
        f.write('test')

@app.route('/', methods=['GET'])
def rootdir():
    return 'Hello world'

MainServer.wsgi:

from MainServer import app as application

预期:内容为'test'的文件写入/tmp

实际结果:没有写入文件。日志中没有报告错误

如果我 运行 相同的代码但指向我的用户有权写入的任何其他目录,它会按预期创建文件。 /tmp 是我遇到此问题的唯一目录。

如果我 运行 直接使用上面的代码并使用 flask 的内置服务器 (app.run),它可以按预期在 /tmp 中创建文件,没有任何问题。

我已确保 mod_wsgi 服务器 运行ning 与 app.run 脚本的用户相同,并且该用户能够写入 /tmp。

--编辑--

运行 直接从命令行使用 httpd 不会导致此问题。将 httpd 作为 systemd 服务启动

尝试将 app 记录器级别设置为 DEBUG(并添加处理程序):

import logging
from logging.handlers import RotatingFileHandler
import flask

app = flask.Flask(__name__)

@app.before_first_request
def initstuff():
    test_file = '/tmp/test'
    with open(test_file, 'w') as f:
        f.write('test')

@app.route('/', methods=['GET'])
def rootdir():
    return 'Hello world'

if __name__ == '__main__':
    handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=1)
    handler.setLevel(logging.DEBUG)
    app.logger.addHandler(handler)
    app.run(host='0.0.0.0', port=8056, debug=True, use_reloader=False)

然后在app.log中查看问题所在。

我认为与这个答案有关: https://unix.stackexchange.com/questions/167835/where-apaches-tmp-located

Apache 可能正在使用 private-tmp,这导致 /tmp 被重定向到 /var/tmp/systemd-private--httpd.service-/