N. Simon N. Simon - 1 year ago 107
Python Question

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 - kernel 3.12.36

I'm using mod_wsgi and flask to host a server. I am able to reproduce this issue with the following simplified code and generic .wsgi script:


import flask

app = flask.Flask(__name__)

def initstuff():
test_file = '/tmp/test'
with open(test_file, 'w') as f:

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


from MainServer import app as application

Expected: a file with the contents 'test' is written in /tmp

Actual outcome: No file is written. No errors reported in log

If I run the same code but instead point to any other directory which my user has permission to write, it creates the file as expected. /tmp is the only directory where I am having this issue.

If I run the above code directly and use flask's built in server (app.run), it can create the file in /tmp as expected without any issues.

I've ensured that the mod_wsgi server is running as the same user as the script with app.run is and that this user is able to write to /tmp.


Running httpd directly from the command line does not cause this issue. Starting httpd as a systemd service does

Answer Source

I ended up simply moving the file I was attempting to create out of /tmp. The original issues appears to be related to Systemd and special permissions for /tmp (although it persists when changing permissions from 1777 to just 777), but I was not able to figure it out the cause or a solution for it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download