我有运行全新安装的Ubuntu 10.04 LTS的VPS。我正在尝试使用Flask微框架建立一个实时应用程序,但这给我带来了麻烦。在尝试使其运行时,我记下了笔记,这是我的逐场比赛,目的是准确找出我出了什么问题。
安装
http://flask.pocoo.org/docs/installation/#installation $ adduser myapp $ sudo apt-get install python-setuptools $ sudo easy_install pip $ sudo pip install virtualenv /home/myapp/ -- www/ $ sudo pip install virtualenv /home/myapp/ -- www/ -- env/ $ . env/bin/activate $ easy_install Flask
MOD_WSGI
http://flask.pocoo.org/docs/deploying/mod_wsgi/ $ sudo apt-get install apache2 $ sudo apt-get install libapache2-mod-wsgi
创建WSGI文件
$ nano /home/myapp/www/myapp.wsgi --myapp.wsgi contents:-------------------------- activate_this = '/home/myapp/env/bin/activate_this.py' execfile(activate_this, dict(__file__=activate_this)) from myapp import app as application /home/myapp/ -- www/ -- myapp.wsgi -- env/
配置Apache
$ nano /etc/apache2/sites-available/myapp.com -----myapp.com file contents --------------------- <VirtualHost *:80> ServerName myapp.com WSGIDaemonProcess myapp user=myapp group=myapp threads=5 python-path=/home/myapp/env/lib/python2.6/site-packages WSGIScriptAlias / /home/myapp/www/myapp.wsgi <Directory /home/myapp/www> WSGIProcessGroup myapp WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all </Directory> </VirtualHost>
启用我刚刚创建的虚拟主机文件
$ cd /etc/apache2/sites-enabled $ ln -s ../sites-available/myapp.com
重新启动Apache
$ /etc/init.d/apache2 restart
为我提供了500个服务器错误页面。这是最新的错误日志:
mod_wsgi (pid=3514): Target WSGI script '/home/myapp/www/myapp.wsgi' cannot be loaded as Python module. mod_wsgi (pid=3514): Exception occurred processing WSGI script '/home/myapp/www/myapp.wsgi'. Traceback (most recent call last): File "/home/myapp/www/myapp.wsgi", line 4, in <module> from myapp import app as application ImportError: No module named myapp
这些错误暗示这是显而易见的,但是我很迷茫。
显然,它找不到您的“ myapp”软件包。您应该将其添加到myapp.wsgi文件中的路径中,如下所示:
myapp
myapp.wsgi
import sys sys.path.append(DIRECTORY_WHERE_YOUR_PACKAGE_IS_LOCATED) from myapp import app
另外,如果myappmodule是一个包,则应将__init__.py文件清空并将其放入其目录中。
__init__.py