V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
teannnn
V2EX  ›  Flask

[求教高人]在 Digital Ocean 上部署了第一个 flask 应用,但浏览器访问后报 500 错误

  •  
  •   teannnn · 2015-01-02 12:29:29 +08:00 · 5872 次点击
    这是一个创建于 3602 天前的主题,其中的信息可能已经有所发展或是发生改变。

    部署在Digital Ocean的ubuntu 12.X主机上。这个是DO的部署说明如何在ubuntu上部署Flask应用

    Problem:
    浏览器访问地址后,报500错误。接着我检查了apache日志,报错如下

    Error log

    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131] mod_wsgi (pid=6887): Target WSGI script '/var/www/qianshan/qianshan.wsgi' cannot be loaded as Python module.
    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131] mod_wsgi (pid=6887): Exception occurred processing WSGI script '/var/www/qianshan/qianshan.wsgi'.
    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131] Traceback (most recent call last):
    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131]   File "/var/www/qianshan/qianshan.wsgi", line 7, in <module>
    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131]     from qianshan import app as application
    [Thu Jan 01 23:13:29 2015] [error] [client 112.64.71.131] ImportError: cannot import name app
    

    树形结构

    qianshan
    ├── qianshan
    │   ├── config.ini
    │   ├── __init__.py
    │   ├── static
    │   ├── templates
    │   └── venv
    └── qianshan.wsgi
    

    Virtual Host configuration

    <VirtualHost *:80>
                ServerName qianshan.co
                ServerAdmin [email protected]
                WSGIScriptAlias / /var/www/qianshan/qianshan.wsgi
                <Directory /var/www/qianshan/qianshan/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/qianshan/qianshan/static
                <Directory /var/www/qianshan/qianshan/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    wsgi

    #!/usr/bin/python
    import sys
    import logging
    logging.basicConfig(stream=sys.stderr)
    sys.path.insert(0,"/var/www/qianshan")
    
    from qianshan import app as application
    application.secret_key = 'Add your secret key'
    

    init.py file

    # Filename: __init__.py
    # encoding: utf-8
    
    import ConfigParser
    import codecs
    from flask import Flask
    from flask import render_template
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        block_list = getBlockList()
        website_dict = getWebsiteDict()
        return render_template('index.html', block_list=block_list, website_dict=website_dict)
    ...
    ...
    if __name__ == '__main__':
    app.run()
    

    祝大家新年快乐

    11 条回复    2015-01-03 22:42:28 +08:00
    imlonghao
        1
    imlonghao  
       2015-01-02 12:35:30 +08:00 via Android
    ImportError: cannot import name app
    imlonghao
        2
    imlonghao  
       2015-01-02 12:36:31 +08:00 via Android
    from qianshan import app

    qianshan目录下哪里有app?
    Livid
        3
    Livid  
    MOD
       2015-01-02 13:06:15 +08:00 via iPhone   ❤️ 2
    看起来像是 working directory 问题。
    crazyxin1988
        4
    crazyxin1988  
       2015-01-02 13:11:49 +08:00   ❤️ 2
    @imlonghao
    app = Flask(__name__)
    @Livid 对的
    问题出在这里 sys.path.insert(0,"/var/www/qianshan")
    imlonghao
        5
    imlonghao  
       2015-01-02 13:12:50 +08:00 via Android
    收回2L说的话 自己对Flask不熟造成
    HowardMei
        6
    HowardMei  
       2015-01-02 13:18:46 +08:00
    你第一条 Allow from all 会把 config.ini 也暴露了吧?
    teannnn
        7
    teannnn  
    OP
       2015-01-02 20:16:12 +08:00
    @crazyxin1988 请问这里应该怎么写呢?
    sys.path.insert(0,"/var/www/qianshan")

    我打算把项目从机器删除,重新按照配置走一遍
    crazyxin1988
        8
    crazyxin1988  
       2015-01-02 20:32:44 +08:00   ❤️ 1
    @teannnn
    还没搞定? 我昨晚在sf上回答你了~
    我觉得 你有些的东西没搞清楚~
    推荐你仔细看一下flask官方文档 http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/
    搞清楚大体的原理再去弄~很快的
    文档里说的很清楚
    sys.path.insert(0,"path") 的用途是:项目使用virtualenv,然后项目的所有的所以依赖库都是安装在了项目下,在你的项目里就是venv 这里~
    teannnn
        9
    teannnn  
    OP
       2015-01-03 18:20:56 +08:00
    @crazyxin1988 好,多谢
    teannnn
        10
    teannnn  
    OP
       2015-01-03 22:41:59 +08:00
    重新部署了项目,发现是init.py内部的代码问题。非常感谢上面的大家啦~
    我用教程的init.py代码片段,不修改配置也不修改sys.path这些,网页可以正常访问。用自己写的init.py就报错了。

    from flask import Flask
    app = Flask(__name__)
    @app.route("/")
    def hello():
    return "Hello, I love Digital Ocean!"
    if __name__ == "__main__":
    app.run()
    teannnn
        11
    teannnn  
    OP
       2015-01-03 22:42:28 +08:00
    我在检查代码中,谢谢大家。上面的python代码是教程中的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5430 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 01:27 · PVG 09:27 · LAX 17:27 · JFK 20:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.