我在mod_wsgi 3.4上使用Python 2.7.6运行Flask应用程序。操作系统是Ubuntu 14.04。我无法写入文件。以下是我使用的代码:
if __name__ == "__main__":
f = open('/var/www/jcapp/foobar', 'r+')
f.write('hello world')
f.close()
jcapp.run()
Apache站点配置文件:
<VirtualHost *:80>
ServerName mysite.co
WSGIDaemonProcess jcapp
WSGIScriptAlias / /var/www/jcapp/jcapp.wsgi
<Directory /var/www/jcapp/>
WSGIProcessGroup jcapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /notify /var/www/jcapp/jcapp.wsgi
<Directory /var/www/jcapp/>
WSGIProcessGroup jcapp
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
有人可以告诉我哪里可能出错了吗?
答案 0 :(得分:1)
您只能在__name__ == '__main__'
写入该文件,而当您使用mod_wsgi而不是python app.py
时,该文件不会写入。
将代码移到__name__
后卫之外。
如果您需要在运行应用程序之前进行一些设置,请使用单独的入口点。而是在此入口点指向mod_wsgi。
wsgi_app.py
:
from myapp import jcapp as application
with open('/var/www/jcapp/foobar', 'w') as f:
f.write('hello world')