我试图让flask使用mod_wsgi与Apache一起工作,我已经用头撞了七个多小时了。是时候寻求帮助了。
我正在使用httpd docker镜像运行apache。
我首先按照here的说明让wsgi 'hello world‘应用程序正常工作,这非常好用。如果我打开浏览器并导航到172.17.0.2,我会看到/usr/local/apache2/htdocs/上的页面
在我的httpd.conf中,我有以下内容来显示这些页面:
DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiView
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>这都是docker图像的默认设置。
然后,我在/usr/local/apache2/wsgi-scripts/myapp.wsgi的mod_wsgi说明页面上得到了hello world示例。在我的httpd.conf中,我有以下内容
WSGIScriptAlias /myapp /usr/local/apache2/wsgi-scripts/myapp.wsgi
<Directory /usr/local/apache2/wsgi-scripts>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>当我使用浏览器导航到172.17.0.2/myapp时,一切都很好。我看到'hello world‘,apache输出以下日志:
[Thu Apr 16 02:48:14.678027 2020] [wsgi:info] [pid 1275:tid 140646030583552] mod_wsgi (pid=1275): Create interpreter '172.17.0.2|/myapp'.
[Thu Apr 16 02:48:14.713667 2020] [wsgi:info] [pid 1275:tid 140646030583552] [client 172.17.0.1:38046] mod_wsgi (pid=1275, process='', application='172.17.0.2|/myapp'): Loading Python script file '/usr/local/apache2/wsgi-scripts/myapp.wsgi'.
172.17.0.1 - - [16/Apr/2020:02:48:14 +0000] "GET /myapp HTTP/1.1" 200 12现在我试着让我的flask应用程序正常工作。这就是问题所在。我的wsgi脚本就在上面的myapp.wsgi脚本旁边:
root@423828dd037e:/# ls -l /usr/local/apache2/wsgi-scripts/
total 8
-rw-r--r-- 1 root root 278 Apr 16 02:16 myapp.wsgi
-rw-r--r-- 1 root root 74 Apr 16 02:22 website_api.wsgi脚本非常简单:
root@423828dd037e:/# cat /usr/local/apache2/wsgi-scripts/website_api.wsgi
from website_rest_api import app as application我可以在python解释器中运行完全相同的命令,所以我知道导入工作正常。
我有过
WSGIScriptAlias /api /usr/local/apache2/wsgi-scripts/website_api.wsgi在httpd.conf文件中
当我试图导航到172.17.0.2/api时,我遇到了一个完全令人沮丧的练习。apache日志如下所示:
[Thu Apr 16 02:52:41.678999 2020] [wsgi:info] [pid 1395:tid 140174394627840] mod_wsgi (pid=1395): Create interpreter '172.17.0.2|/api'.
[Thu Apr 16 02:52:41.683555 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Python home /usr.
[Thu Apr 16 02:52:41.683649 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Initializing Python.
[Thu Apr 16 02:52:41.710027 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Attach interpreter ''.
[Thu Apr 16 02:52:41.714671 2020] [wsgi:info] [pid 1395:tid 140174394627840] [client 172.17.0.1:40228] mod_wsgi (pid=1395, process='', application='172.17.0.2|/api'): Loading Python script file '
/usr/local/apache2/wsgi-scripts/website_api.wsgi'.
[Thu Apr 16 02:52:41.736762 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Imported 'mod_wsgi'.
172.17.0.1 - - [16/Apr/2020:02:52:41 +0000] "GET /api HTTP/1.1" 404 232从这些日志中,我知道阿帕奇知道这是mod_wsgi的事情。它似乎是从正确的位置加载脚本,但是我得到了一个404。对于我所有的flask URL,我都得到了这个。我应该能够访问/api、/api/login、/api/logout、/api/projects、/api/tests,但是什么都没有。所有的东西都是404,我看不到有其他的错误可以帮助调试。
我知道from website_rest_api import app as application导入了正确的东西,因为我可以进入python shell并自己查看URL映射:
>>> from website_rest_api import app as application
>>> application.url_map
Map([<Rule '/api/projects' (OPTIONS, HEAD, GET) -> projects>,
<Rule '/api/logout' (POST, OPTIONS) -> logout>,
<Rule '/api/login' (POST, OPTIONS) -> login>,
<Rule '/api/tests' (OPTIONS, HEAD, GET) -> tests>,
<Rule '/api' (OPTIONS, HEAD, GET) -> hello>,
<Rule '/static/<filename>' (OPTIONS, HEAD, GET) -> static>])我也可以打印这些,这样它们就会显示在apache日志中:
[Thu Apr 16 04:38:57.162667 2020] [wsgi:info] [pid 2953:tid 139925743146112] mod_wsgi (pid=2953): Imported 'mod_wsgi'.
[Thu Apr 16 04:38:57.396592 2020] [wsgi:error] [pid 2864:tid 139925713024768] Map([<Rule '/api/projects' (GET, OPTIONS, HEAD) -> projects>,
[Thu Apr 16 04:38:57.396634 2020] [wsgi:error] [pid 2864:tid 139925713024768] <Rule '/api/logout' (OPTIONS, POST) -> logout>,
[Thu Apr 16 04:38:57.396642 2020] [wsgi:error] [pid 2864:tid 139925713024768] <Rule '/api/login' (OPTIONS, POST) -> login>,
[Thu Apr 16 04:38:57.396649 2020] [wsgi:error] [pid 2864:tid 139925713024768] <Rule '/api/tests' (GET, OPTIONS, HEAD) -> tests>,
[Thu Apr 16 04:38:57.396657 2020] [wsgi:error] [pid 2864:tid 139925713024768] <Rule '/api' (GET, OPTIONS, HEAD) -> hello>,
[Thu Apr 16 04:38:57.396666 2020] [wsgi:error] [pid 2864:tid 139925713024768] <Rule '/static/<filename>' (GET, OPTIONS, HEAD) -> static>])我也可以故意搞乱website_api.wsgi中的导入,我看到Apache会发出一条错误消息,所以我有理由相信,如果导入错误,它会告诉我:
[Thu Apr 16 04:34:27.634313 2020] [wsgi:info] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743, process='website_api', application=''): Loading Python script file '/usr
/local/apache2/wsgi-scripts/website_api.wsgi'.
[Thu Apr 16 04:34:27.634956 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743): Failed to exec Python script file '/usr/local/apache2/wsgi-scripts/web
site_api.wsgi'.
[Thu Apr 16 04:34:27.635044 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743): Exception occurred processing WSGI script '/usr/local/apache2/wsgi-scr
ipts/website_api.wsgi'.
[Thu Apr 16 04:34:27.644848 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] Traceback (most recent call last):
[Thu Apr 16 04:34:27.644905 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] File "/usr/local/apache2/wsgi-scripts/website_api.wsgi", line 1, in <module>
[Thu Apr 16 04:34:27.644917 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] import zarzlefrz
[Thu Apr 16 04:34:27.644943 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] ModuleNotFoundError: No module named 'zarzlefrz'
172.17.0.1 - - [16/Apr/2020:04:34:27 +0000] "GET /api HTTP/1.1" 500 528即使是调试级别的日志似乎也不能告诉我发生了什么:
[Thu Apr 16 03:53:52.766115 2020] [core:debug] [pid 2075:tid 140389386400896] log.c(1571): AH02639: Using SO_REUSEPORT: yes (1)
[Thu Apr 16 03:53:52.766422 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Python home /usr.
[Thu Apr 16 03:53:52.766546 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Initializing Python.
[Thu Apr 16 03:53:52.769139 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Python home /usr.
[Thu Apr 16 03:53:52.769300 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Initializing Python.
[Thu Apr 16 03:53:52.805797 2020] [wsgi:info] [pid 2077:tid 140389386400896] mod_wsgi (pid=2077): Attach interpreter ''.
[Thu Apr 16 03:53:52.806676 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Attach interpreter ''.
[Thu Apr 16 03:53:52.813314 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Attach interpreter ''.
[Thu Apr 16 03:53:52.845456 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.847250 2020] [mpm_event:debug] [pid 2079:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:53:52.859225 2020] [wsgi:info] [pid 2077:tid 140389386400896] mod_wsgi (pid=2077): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.860438 2020] [mpm_event:debug] [pid 2077:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:53:52.862762 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.863600 2020] [mpm_event:debug] [pid 2078:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:54:03.057717 2020] [authz_core:debug] [pid 2079:tid 140389235279616] mod_authz_core.c(818): [client 172.17.0.1:41350] AH01626: authorization result of Require all granted: granted
[Thu Apr 16 03:54:03.057771 2020] [authz_core:debug] [pid 2079:tid 140389235279616] mod_authz_core.c(818): [client 172.17.0.1:41350] AH01626: authorization result of <RequireAny>: granted
[Thu Apr 16 03:54:03.086984 2020] [wsgi:info] [pid 2079:tid 140389235279616] mod_wsgi (pid=2079): Create interpreter '172.17.0.2|/api'.
[Thu Apr 16 03:54:03.128584 2020] [wsgi:info] [pid 2079:tid 140389235279616] [client 172.17.0.1:41350] mod_wsgi (pid=2079, process='', application='172.17.0.2|/api'): Loading Python script file '
/usr/local/apache2/wsgi-scripts/website_api.wsgi'.
172.17.0.1 - - [16/Apr/2020:03:54:03 +0000] "GET /api HTTP/1.1" 404 232我的问题是:
我到底该如何进一步调试它呢?有没有什么地方会喷出我看不到的秘密烧瓶错误?
发布于 2020-04-16 12:47:39
好了,我已经弄明白了--秘密的调试技巧是离开一个小时,然后再回来。我希望我六个小时前就试过了。
flask应用程序的路由如下:
@app.route('/api', methods=['GET'])
def hello():
return "Hello World" apache配置如下所示:
WSGIScriptAlias /api /usr/local/apache2/wsgi-scripts/website_api.wsgi这意味着要看到flask打印"Hello World",我需要访问172.17.0.2/api/api,而不是像以前那样访问172.17.0.2/api。
在使用flask的内置服务器与生产环境进行调试时,我需要做一些额外的工作来使路由匹配,但这是一个不同的堆栈溢出问题
https://stackoverflow.com/questions/61242022
复制相似问题