【问题标题】:Hosting flask with mod_wsgi, Apache使用 mod_wsgi、Apache 托管烧瓶
【发布时间】:2014-03-03 12:24:19
【问题描述】:

我在 Flask 中编写了一个小应用程序,我正试图让它在 Linode 服务器上运行。 但是目前,如果我在浏览器中输入服务器 IP 地址,我会得到一个文件列表。

我没有在 virtualenv 中安装 python(当前),检查过,它正在工作。

我一直在关注这个教程:https://www.digitalocean.com/community/articles/how-to-deploy-a-flask-application-on-an-ubuntu-vps

我的应用结构是这样的(顶层文件夹位于/var/www/):

|--mythmuff/
|----mythmuff/
|------__init__.py
|------app.db
|------config.py
|------models.py
|------templates/
|--------index.html
|------views.py
|--mythmuff.wsgi

这是我的 init.py:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail
import os
basedir = os.path.abspath(os.path.dirname(__file__))


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.db')
# app email settings go here
mail = Mail(app)
db = SQLAlchemy(app)

import views
import models

这是我的 mythmuff.wsgi:

import sys
sys.path.insert(0, '/var/www/mythmuff')
from mythmuff import app as application

这是我的 /etc/apache2/sites-available/mythmuff.ru.conf(我将 Apache 指向该文件)。

<VirtualHost *:80>
    ServerName mythmuff.ru

    WSGIDaemonProcess mythmuff user=www-data group=www-data threads=5
    WSGIScriptAlias / /var/www/mythmuff/mythmuff.wsgi

    <Directory var/www/mythmuff/mythmuff>
        Order allow,deny
        Allow from all
    </Directory>
    Alias /static /var/www/mythmuff/mythmuff/static
    <Directory /var/www/mythmuff/mythmuff/static>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
</VirtualHost>

我在设置 Apache/wsgi 方面还是个新手,但我认为当我转到 IP 地址并仅获取 /var/www/ 目录中的文件和文件夹列表时,这意味着我在某处缺少app.run()。 我尝试将此添加到__init__.py

if __name__ == "__main__":
    app.run()

但我在家用电脑上运行 __init__.py 时遇到错误:

  File "/Users/georgeoblapenko/Dropbox/University/Python/flask/MythMuff/mythmuff/mythmuff/__init__.py", line 18, in <module>
    import views
  File "/Users/georgeoblapenko/Dropbox/University/Python/flask/MythMuff/mythmuff/mythmuff/views.py", line 5, in <module>
    from models import Product
  File "/Users/georgeoblapenko/Dropbox/University/Python/flask/MythMuff/mythmuff/mythmuff/models.py", line 6, in <module>
    class Product(db.Model):
  File "/Users/georgeoblapenko/anaconda/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 510, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "/Users/georgeoblapenko/anaconda/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 53, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/Users/georgeoblapenko/anaconda/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 246, in _as_declarative
    **table_kw)
  File "/Users/georgeoblapenko/anaconda/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 342, in __new__
    "existing Table object." % key)
sqlalchemy.exc.InvalidRequestError: Table 'product' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

在家里,我从 mythmuff 顶部文件夹中运行了一个 runserver.py 脚本进行测试(如此处所述:http://flask.pocoo.org/docs/patterns/packages/)。

那么,我需要添加什么才能真正使整个工作正常运行?

更新: 我决定运行一个简单的 wsgi hello world 测试。所以我用

替换了mythmuff.wsgi中的所有内容
import os
import sys
sys.path.append('/var/www/mythmuff/mythmuff')

def application(environ, start_response):
        status = '200 OK'
        output = 'Hello World!'

        response_headers = [('Content-type', 'text/plain'),
                                        ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
        return [output]

但是,我仍然得到文件/文件夹列表,而不是“Hello World!”页。 因此,由于某种原因,该请求似乎没有传递给 WSGI。

【问题讨论】:

    标签: apache flask mod-wsgi virtualhost


    【解决方案1】:

    好的,我设法弄明白了。原来,我删除了默认的 apache index.html 文件,并没有禁用 000-default.conf(通过dissite),这就是它显示文件夹的原因。 现在一切正常。

    【讨论】:

      【解决方案2】:

      是的。如果您想通过外部IP地址访问服务器,您必须先禁用默认站点配置。

      sudo a2dissite 000-default.conf
      service apache2 restart
      

      【讨论】:

        猜你喜欢
        • 2020-04-02
        • 1970-01-01
        • 2014-05-23
        • 2013-12-16
        • 2021-06-17
        • 2018-10-06
        • 2014-03-07
        • 1970-01-01
        • 2012-06-20
        相关资源
        最近更新 更多