看到我运行 Flask 的尝试有什么问题吗? (mod_wsgi + virtualenv)

发布于 2024-10-07 04:49:29 字数 2363 浏览 7 评论 0原文

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

这些错误暗示这是非常明显的事情,但我很迷失。

I have a VPS running a fresh install of Ubuntu 10.04 LTS. I'm trying to set up a live application using the Flask microframework, but it's giving me trouble. I took notes while I tried to get it running and here's my play-by-play in an effort to pinpoint exactly where I went wrong.

INSTALLATION

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

Creating WSGI file

$ 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/

Configuring 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>

Enable the virtual host file I just created

$ cd /etc/apache2/sites-enabled
$ ln -s ../sites-available/myapp.com

Restart Apache

$ /etc/init.d/apache2 restart

Servers me a 500 server error page. Here's the latest error log:

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

The errors allude that it's something strikingly obvious, but I'm quite lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2024-10-14 04:49:29

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

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

此外,如果 myapp 模块是一个包,您应该放置并清空 __init__.py 文件放入其目录中。

Obviously, it cannot find your "myapp" package. You should add it to the path in your myapp.wsgi file like this:

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

Also, if myapp module is a package, you should put and empty __init__.py file into its directory.

尴尬癌患者 2024-10-14 04:49:29

编辑sys.path.append行,它需要是一个字符串。

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

注意单引号。

Edit line sys.path.append, it needs to be a string.

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

Notice the single quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文