我正在尝试使用mod_wsgi使Flask与Apache一起工作,而我已经为此花了七个多小时来猛烈抨击。是时候寻求帮助了。
我正在使用httpd泊坞映像运行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
现在,我尝试使我的烧瓶应用程序正常工作。这是出问题的地方。我的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
从这些日志中,我知道apache知道这是mod_wsgi的事情。似乎它从正确的位置加载了脚本,但是随后我得到了404。我对所有烧瓶的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
我的问题:
我该怎么进一步调试呢?有什么地方喷出我看不见的暗瓶错误吗?
我正在尝试使用mod_wsgi使Flask与Apache一起工作,而我已经为此花了七个多小时来猛烈抨击。现在该寻求帮助了。我正在使用httpd docker运行apache ...
好吧,我已经弄清楚了-秘密的调试技巧是走一个小时然后再回来。我希望我在六个小时前尝试过。
烧瓶应用程序具有这样的路由: