大家好,做了一个flask的小应用,配置在digital ocean上,按照digital ocean的配置说明配置后链接描述,访问站点时报500错误。看了下apache的日志,错误原因如下,还请帮忙看看。
日志报错:
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] Traceback (most recent call last):
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] File "/var/www/qianshan/qianshan.wsgi", line 7, in <module>
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] from qianshan import app as application
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] ImportError: cannot import name app
项目结构:
.
├── qianshan
│ ├── config.ini
│ ├── __init__.py
│ ├── static
│ ├── templates
│ └── venv
└── qianshan.wsgi
虚拟主机配置
<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()
黄舟2017-04-17 14:01:49
我之前在do上部署Flask應用 用的是gunicorn。詳情可以看我的部落格:http://defshine.github.io/deploy-flask-app-on-do.html
我試著在本地機器上,按照樓主的方式部署了一下,恰巧也遇到這個錯誤。
最後發現問題時在wsgi的那個檔案
推薦 樓主檢查
sys.path.insert(0,"/var/www/qianshan/")
這個路徑/var/www/qianshan/ 是專案的路徑嗎?
例如的專案放在 /home/xin/workspace/python/flaskapp這裡那我的設定就是:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/xin/workspace/python/flaskapp/")
from app import app as application
application.secret_key = 'Add your secret key'
大家讲道理2017-04-17 14:01:49
把init.py中
if name == 'main':
app.run()
這兩句刪除.
另新建一個檔案runsvr,py, 內容如下:
from init import app
if name == 'main':
app.run()
平常調試的時候
用python runsvr.py來運行