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

2015-01-02 12:29:29 +08:00
 teannnn

部署在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 spark@qianshan.co
            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()

祝大家新年快乐

5876 次点击
所在节点    Flask
11 条回复
imlonghao
2015-01-02 12:35:30 +08:00
ImportError: cannot import name app
imlonghao
2015-01-02 12:36:31 +08:00
from qianshan import app

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

我打算把项目从机器删除,重新按照配置走一遍
crazyxin1988
2015-01-02 20:32:44 +08:00
@teannnn
还没搞定? 我昨晚在sf上回答你了~
我觉得 你有些的东西没搞清楚~
推荐你仔细看一下flask官方文档 http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/
搞清楚大体的原理再去弄~很快的
文档里说的很清楚
sys.path.insert(0,"path") 的用途是:项目使用virtualenv,然后项目的所有的所以依赖库都是安装在了项目下,在你的项目里就是venv 这里~
teannnn
2015-01-03 18:20:56 +08:00
@crazyxin1988 好,多谢
teannnn
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
2015-01-03 22:42:28 +08:00
我在检查代码中,谢谢大家。上面的python代码是教程中的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/158586

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX