【问题标题】:Running Flask on Apache - CentOS在 Apache 上运行 Flask - CentOS
【发布时间】:2014-06-12 09:47:15
【问题描述】:

我设置了一个非常简单的烧瓶应用程序来测试在 CentOS 6 上的 Apache 中的部署,但我收到以下错误:

[Sat Apr 26 12:44:20 2014] [error] mod_wsgi (pid=29782): Target WSGI script '/var/www/html/sites/rtdsllc/rtdsllc.wsgi' cannot be loaded as Python module.
[Sat Apr 26 12:44:20 2014] [error] mod_wsgi (pid=29782): Exception occurred processing WSGI script '/var/www/html/sites/rtdsllc/rtdsllc.wsgi'.
[Sat Apr 26 12:44:20 2014] [error] Traceback (most recent call last):
[Sat Apr 26 12:44:20 2014] [error] File "/var/www/html/sites/rtdsllc/rtdsllc.wsgi", line 10, in <module>
[Sat Apr 26 12:44:20 2014] [error]     from rtdsllc import app as application
[Sat Apr 26 12:44:20 2014] [error] ImportError: cannot import name app

这是我的 wsgi 文件:

activate_this = '/var/www/virtualenvs/default/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

path = '/var/www/html/sites/rtdsllc'

import sys
if path not in sys.path:
    sys.path.append(path)

from rtdsllc import app as application

这是我的虚拟主机配置:

WSGISocketPrefix run/wsgi

<VirtualHost *:8086>

    WSGIDaemonProcess rtdsllc user=apache group=apache threads=5
    WSGIScriptAlias /test /var/www/html/sites/rtdsllc/rtdsllc.wsgi

    ErrorLog logs/rtdsllc-error_log
    CustomLog logs/rtdsllc-access_log common

    <Directory /var/www/html/sites/rtdsllc/rtdsllc>
        WSGIProcessGroup rtdsllc
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

这是我的烧瓶应用程序:

from flask import Flask, render_template

app = Flask(__name__)      

@app.route('/')
def home():
  return render_template('index.html')

if __name__ == '__main__':
  app.run(debug=False)

最后是我的文件夹结构:

rtdsllc/
├── rtdsllc
│   ├── __init__.py
│   ├── routes.py
│   ├── static
│   │   ├── css
│   │   ├── img
│   │   └── js
│   └── templates
│       ├── base.html
│       └── index.html
└── rtdsllc.wsgi

什么给了?我尝试了各种设置组合均无济于事。在 apache 中设置 Flask 应用程序的正确方法是什么?

编辑

ls -las rtdsllc/rtdsllc:

4 drwxr-xr-x. 4 apache apache 4096 Apr 26 13:07 .
4 drwxr-xr-x. 3 apache apache 4096 Apr 25 22:54 ..
0 -rw-r--r--. 1 apache apache    0 Apr 25 20:57 __init__.py
4 -rw-r--r--. 1 apache apache  134 Apr 25 22:55 __init__.pyc
4 -rw-r--r--. 1 apache apache  190 Apr 26 12:48 routes.py
4 -rw-r--r--. 1 apache apache  521 Apr 26 13:07 routes.pyc
4 drwxr-xr-x. 5 apache apache 4096 Apr 25 20:14 static
4 drwxr-xr-x. 2 apache apache 4096 Apr 25 20:19 templates

【问题讨论】:

  • Apache 用户是否可以读取目录/文件?
  • @GrahamDumpleton 是的,先生。所有文件归apache:apache所有。
  • 不仅仅是文件,还有目录。 'ls -las rtdsllc/rtdsllc' 的输出是什么?
  • @GrahamDumpleton 查看我的编辑。

标签: python apache flask centos mod-wsgi


【解决方案1】:

在做:

from rtdsllc import app as application

它将被导入:

rtdsllc/rtdsllc/__init__.py

根据您的目录列表:

0 -rw-r--r--. 1 apache apache    0 Apr 25 20:57 __init__.py

该文件是空的,并且其中没有要通过导入拖入的“app”对象。

以下是哪个文件?

app = Flask(__name__)  

现在它不在您尝试从中导入它的位置。


更新 1

用途:

from rtdsllc.routes import app as application

如果你的 'app' 对象在 'rtdsllc.routes' 中。

【讨论】:

  • 我在 routes.py 中有这个。正确的做法是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 2016-06-07
  • 2020-06-08
  • 2014-10-25
  • 2021-12-08
  • 2021-10-10
相关资源
最近更新 更多