在Ubuntu 14.04 VPS(数字海洋)上部署Flask应用程序的问题

时间:2014-05-04 02:45:56

标签: apache ubuntu flask wsgi

我在当地开发了一款非常棒的Flask应用......现在似乎无法为我的生活部署。

我收到内部服务器错误,其中包含以下错误日志:

[Sat May 03 22:24:11.355281 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221] mod_wsgi (pid=17560): Target WSGI script '/var/www/dhh/dhh.wsgi' cannot be loaded as Python module.
[Sat May 03 22:24:11.355380 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221] mod_wsgi (pid=17560): Exception occurred processing WSGI script '/var/www/dhh/dhh.wsgi'.
[Sat May 03 22:24:11.355467 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221] Traceback (most recent call last):
[Sat May 03 22:24:11.355541 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221]   File "/var/www/dhh/dhh.wsgi", line 7, in 
[Sat May 03 22:24:11.355754 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221]     from dhh.app import app as application
[Sat May 03 22:24:11.355815 2014] [:error] [pid 17560:tid 139814473037568] [client 68.40.196.121:52221] ImportError: No module named dhh.app

当前文件结构树:

/dhh
|--/app
|------/static
|------/templates
|------__init__.py
|------views.py
|--/flask
|------/bin
|------/include
|------/lib
|------/local
|--/tmp

相关文件

  1. __init__.py
  2. from flask import Flask
    
    app = Flask(__name__)
    from app import views
    
    if __name__ == "__main__":
        app.run()
    
    1. views.py
    2. from app import app
      
      @app.route('/')
      @app.route('/index')
      def index():
          return "Hello, World! This better get working soon."
      
      1. /etc/apache2/sites-available/dhh.conf
      2. ServerName davidhagan.me ServerAdmin david@davidhhagan.com WSGIScriptAlias / /var/www/dhh/dhh.wsgi Order allow,deny Allow from all Alias /static /var/www/dhh/dhh/app/static Order allow,deny Allow from all ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined

        1. dhh.wsgi
        2. #!/usr/bin/python
          import sys
          import logging
          logging.basicConfig(stream=sys.stderr)
          sys.path.insert(0,"/var/www/dhh/")
          
          from dhh.app import app as application
          application.secret_key = 'super secret key'
          
          

          虽然这是应用程序的非常空的版本,但它似乎导致了错误。我确定apache服务器是如何设置的,这是一个简单的错误,但无法用其他SO Q / A来解决它。我很想知道如何解决这个问题,但为什么会这样呢!

          更新/解决方案

          我最终需要改变路径以及我如何调用模块。

          sys.path.insert(0, "/var/www/dhh/")

          成为

          sys.path.insert(0, "/var/www/dhh/dhh/")

          from dhh.app import app as application

          成为

          from app import app as application

1 个答案:

答案 0 :(得分:2)

而不是:

from dhh.app import app as application

使用:

from app import app as application