Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

python - Flask Apache on AWS EC2 - Read/Write Failing

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...