我试图让Flask运行时看到了什么问题? (mod_wsgi + virtualenv)

时间:2010-12-12 03:01:28

标签: python apache2 mod-wsgi virtualenv flask

我有一个运行全新安装的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

这些错误暗示这是非常明显的,但我很遗憾。

2 个答案:

答案 0 :(得分:8)

显然,它无法找到您的“myapp”包。您应该将其添加到myapp.wsgi文件中的路径,如下所示:

import sys
sys.path.append(DIRECTORY_WHERE_YOUR_PACKAGE_IS_LOCATED)
from myapp import app

此外,如果myapp模块是一个包,则应将__init__.py文件放入其目录中。

答案 1 :(得分:0)

修改专栏sys.path.append,它必须是一个字符串。

import sys
sys.path.append('directory/where/package/is/located')

注意单引号。