window2008下,apache+mod_msgi+flask出现问题,请大侠帮忙瞧瞧

czbuyi 2018-10-19 05:05:39
环境:
windows2008
apache 2.4.35 x64(ApacheHaus)
python 3.7
mod_wsgi‑4.6.4+ap24vc15‑cp37‑cp37m‑win_amd64.whl
项目路径:d:\pyproject\test\
用pycharm开发,使用venv。

步骤:
1、下载并解压ApacheHaus至:d:\Apache24\。进入d:\Apache24\bin,执行httpd.exe,使用浏览器打开localhost:80,成功。
2、已安装python37,默认安装目录。
3、在pycharm创建新项目test,并进入虚拟环境:
1、pip install flask
2、pip install mod_wsgi‑4.6.4+ap24vc15‑cp37‑cp37m‑win_amd64.whl
3、执行mod_wsgi-express.exe module-config,将出现的结果拷贝下来,如下:
LoadModule wsgi_module "d:/pyproject/test/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "d:/pyproject/test/venv"
4、修改d:\Apache24\conf\httpd.conf
1、将以下加入到最后:
LoadFile "D:/pyproject/test/venv/Scripts/python37.dll"
LoadModule wsgi_module "d:/pyproject/test/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "d:/pyproject/test/venv"
2、修改端口号为:Listen 5002

** 重启httpd.exe,出现错误:
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
解决:出现这个问题,可能是读不到python库之类的东西吧。添加两条环境变量后重启机子,就OK了。
PYTHONHOME="C:\Users\Administrator\AppData\Local\Programs\Python\Python37"
PYTHONPATH="C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib"

5、在pycharm中,加入wsgi.py到test项目中。wsgi.py:
def application(environ,start_response):
     status = "200 Ok"
     output = b"Hello wsgi"
     response_headers=[('Content-type','text/plain'),('Content-Length',str(len(output)))]
     start_response(status,response_headers)
   return[output]

6、将以下代码加到d:\Apache24\conf\httpd.conf最下面:
<VirtualHost *:5002>
DocumentRoot D:\pyproject\test
WSGIScriptAlias / D:\pyproject\test\wsgi.py
<Directory 'D:\pyproject\test'>
AllowOverride AuthConfig FileInfo
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

7、重启httpd.exe,浏览器中打开localhost:5002,成功出现"Hello wsgi"

8、pycharm 新建test.py。test.py:
from flask import Flask
app = Flask(__name__)

@app.route("/test")
def index():
return "hello flask"

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

9、将wsgi.py修改为如下:
from test import app as application

** 重启httpd.exe,浏览器中打开localhost:5002,出错。apache error.log中出现:
mod_wsgi (pid=6068): Failed to exec Python script file 'D:/pyproject/test/wsgi.py'.
mod_wsgi (pid=6068): Exception occurred processing WSGI script 'D:/pyproject/test/wsgi.py'.
Traceback (most recent call last):\r
File "D:/pyproject/test/wsgi.py", line 3, in <module>\r
from test import app as application\r
ImportError: cannot import name 'app' from 'test' (C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\test\\__init__.py)\r
mod_wsgi (pid=6068): Failed to exec Python script file 'D:/pyproject/test/wsgi.py'., referer: http://localhost:5002/
mod_wsgi (pid=6068): Exception occurred processing WSGI script 'D:/pyproject/test/wsgi.py'., referer: http://localhost:5002/
Traceback (most recent call last):\r, referer: http://localhost:5002/
File "D:/pyproject/test/wsgi.py", line 3, in <module>\r, referer: http://localhost:5002/
from test import app as application\r, referer: http://localhost:5002/
ImportError: cannot import name 'app' from 'test' (C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\test\\__init__.py)\r, referer: http://localhost:5002/

看提示是在C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\Lib\\test\\__init__.py导入app出错,我的app不是在这个目录下,而是在d:\pyproject\test\中,所以把修改一下wsgi.py:
from .test import app as application

又出现错误:
mod_wsgi (pid=6068): Failed to exec Python script file 'D:/pyproject/test/wsgi.py'.
mod_wsgi (pid=6068): Exception occurred processing WSGI script 'D:/pyproject/test/wsgi.py'.
Traceback (most recent call last):\r
File "D:/pyproject/test/wsgi.py", line 3, in <module>\r
from .test import app as application\r
ImportError: attempted relative import with no known parent package\r
mod_wsgi (pid=6068): Failed to exec Python script file 'D:/pyproject/test/wsgi.py'., referer: http://localhost:5002/
mod_wsgi (pid=6068): Exception occurred processing WSGI script 'D:/pyproject/test/wsgi.py'., referer: http://localhost:5002/
Traceback (most recent call last):\r, referer: http://localhost:5002/
File "D:/pyproject/test/wsgi.py", line 3, in <module>\r, referer: http://localhost:5002/
from .test import app as application\r, referer: http://localhost:5002/
ImportError: attempted relative import with no known parent package\r, referer: http://localhost:5002/
提示相对导入错误,因为我把wsgi.py和test.py是放在同一目录下的,用相对form .text import应该是没问题的啊。至此无解。
...全文
599 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_42583401 2019-04-03
  • 打赏
  • 举报
回复
这是我的配置信息,大佬我也是这个问题能帮我看看么
LoadFile "c:/users/administrator/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/administrator/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/administrator/appdata/local/programs/python/python37"
WSGIPythonPath "c:\users\administrator\demos\form"

<VirtualHost *:8081>

ServerName localhost:8081
WSGIScriptAlias / C:\Users\Administrator\demos\form\test.wsgi

<Directory C:\Users\Administrator\demos\form>

Require all granted
Require host ip
</Directory>

</VirtualHost>
czbuyi 2018-10-22
  • 打赏
  • 举报
回复
我自己搞定了,在博客中:https://blog.csdn.net/qq_41090453/article/details/83269088

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧