So I've been stumped by this problem for a day now. I'm relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.
I have an Ubuntu instance, and can get a flask app to run fine on it using Apache2 and WSGI. Only problem is whenever I put a line into my app that requests either a read or a write on the file system the server errors, but the error logs don't produce an error.
Here's the function with some debug prints that errors the site:
def cleanup_temps():
basePath = os.path.dirname(os.path.realpath('__file__')) + '/static/images/temp/'
deleteDelay = 20
print '1'
for f in os.listdir(basePath):
print '2'
if os.path.getctime(basePath + f) < (time.time() - deleteDelay):
print '3'
os.remove(basePath + f)
When I load the page that calls that function it just gives me an Internal Server Error, and checking /var/log/apache2/error.log
the only line that appears is the debug print '1'.
What is strange is that if I run that server locally it works fine. And even if I run that function manually on the EC2 instance via a python shell it still works fine. So I thought it might have something to do with the Apache or WSGI permissions?
I've changed the whole flask site to chmod -R 777
and changed the user:group from root:root to ubuntu:ubuntu, yet non of that seems to have made a difference?
I'm at a bit of a blank now to be honest so not sure what else I can try?
For good measure here's my /etc/apache2/sites-available/ImgResizeApi.conf
file:
<VirtualHost *:80>
WSGIDaemonProcess ImgResizeApi
WSGIScriptAlias / /var/www/ImgResizeApi/ImgResizeApi.wsgi
<Directory /var/www/ImgResizeApi>
WSGIProcessGroup ImgResizeApi
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EDIT: I am pretty sure it's now something to do with permissions as I've made an even simpler test, adding the function below, which as soon as the 3 file IO lines are added errors the server!
@ImgResizeApi.route('/test')
def test():
f = open('test.txt', 'w')
f.write('Hi there')
f.close()
return render_template('index.html')
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…