问题描述
所以我被这个问题困扰了一整天。我对AWS EC2比较陌生,所以一直在尝试使用Python Flask应用程序。我有一个Ubuntu实例,可以得到一个烧瓶应用程序运行良好它使用Apache2和WSGI。唯一的问题是每当我把一行到我的应用程序,要求读或写在文件系统上的服务器错误,但错误日志不会产生错误。
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)
当我加载调用该函数的页面时,它只是给我一个内部服务器错误,并检查 /var/log/apache2/error.log
出现的唯一行是调试打印1。
奇怪的是,如果我在本地运行该服务器,它工作正常。即使我通过python shell在EC2实例上手动运行该函数,它仍然可以正常工作。所以我认为这可能与Apache或WSGI权限有关?
我已经将整个烧瓶站点更改为 chmod -R 777
,并将用户:组从root:root更改为ubuntu:ubuntu,但非这似乎有所作为?
现在说实话,我不知道还有什么可以尝试吗?
好的方法是我的 / 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}
允许,拒绝
允许所有
< / Directory>
< / VirtualHost>
编辑:我相当确定现在是做权限的事情,因为我做了一个更简单的测试,添加下面的函数,一旦3个文件的IO行加错了服务器!
$ pre $ @ code @ @ ImgResizeApi.route ('/ test')
def test():
f = open('test.txt','w')
f.write('Hi there')
f .close()
return render_template('index.html')
在吃东西时我的眼睛是'__ file __'在引号中。
这是一个特殊的变量,而不是一个字符串。不应该引用它。
您可能需要首先打印您的basePath变量才能看到您返回的实际路径。
另外,为了跨平台的兼容性,你可能想使用os.path.sep而不是'/'以及路径连接,而使用os.path.join而不是简单的字符串连接。
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')
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.
这篇关于在AWS EC2上烧毁Apache - 读取/写入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!