问题
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')
回答1:
On thing that eats my eyes is that '__ file __' is in quotes. It is a special variable, not a string. It should not be quoted.
You may want to print you basePath variable first to see the actual path you get back.
Additionally, for cross platform compatibility, you may want to use os.path.sep instead of '/' as well as for the path concatenation use os.path.join instead of simple string concat.
回答2:
Probably related to this question, solution for the edit part is to convert the relative path to an absolute path. I do not know how programs are started on EC2 but it appears that the working directory is within some Python / library directory where the actual program is not allowed to write. A fix might look like so:
@ImgResizeApi.route('/test')
def test():
f = open('/tmp/test.txt', 'w')
f.write('Hi there')
f.close()
return render_template('index.html')
来源:https://stackoverflow.com/questions/33965370/flask-apache-on-aws-ec2-read-write-failing