win10编译mod_wsgi,基于Python3.5和Apache2.4.39

文章目录

    • 摘要
    • 环境
    • 注意 - Python >= 3.7会导致编译失败
    • 编译方法


摘要

本文记述如何在windows 10上编译mod_wsgi ( Python3.5 + Apache-2.4.39 )


环境

  • win 10
  • Apache-2.4.39 - https://www.apachelounge.com/download/
  • Python-3.5
  • Visual Studio 2019 (含c++开发组件)
  • mod_wsgi - https://github.com/GrahamDumpleton/mod_wsgi [ 时效性: 本文用到的源码下载于 2019-06-15 ]

注意 - Python >= 3.7会导致编译失败

mod_wsgi的源码中存在如下内容, 而python37.lib文件中不存在函数PyOS_AfterFork_Child(),这就会导致使用的Python版本大于等于3.7都会导致编译失败,并在链接时提示错误 :无法解析的外部符号。
因此,本文使用Python 3.5 进行mod_wsgi的编译

    if (wsgi_python_initialized && !wsgi_python_after_fork) {
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7)
        PyOS_AfterFork_Child();
#else
        PyOS_AfterFork();
#endif
    }


编译方法

  1. 找到编译器 cl.exe, 将其位置写入用户环境变量PATH中。注意,请保证所有组件的位数一致,如均为64位,或均为32位。例如,在64位机器上编译64位程序,需要使用 …MSVC编译器目录\bin\Hostx64\x64\cl.exe

  2. 在 https://github.com/GrahamDumpleton/mod_wsgi 下载mod_wsgi源码,解压到当前文件夹

  3. 进入目录mod_wsgi/win32/,此目录存放编译mod_wsgi需要的Makefile文件,但到2019/06/15为止,里面没有使用VS 2019和Python 3.5的Makefile,因此,接下来需要做三件事

    • 复制"ap24py34-win64-VC10.mk",重命名为"ap24py35-win64-VC16.mk"
    • 复制"common-VC10.mk",重命名为"common-VC16.mk"
    • 复制"build-win64-VC10.bat",重命名为"build-win64-VC16.bat"
  4. 修改ap24py35-win64-VC16.mk为如下内容

    APACHE_ROOTDIR = Apache2.4的文件夹路径
    
    PYTHON_ROOTDIR = Python3.5的文件夹路径
    PYTHON_VERSION = 35
    
    MSVC_INCLUDE = MSVC编译器的头文件路径
    MSVC_LIB = MSVC编译器的库文件路径
    
    WINSDK_INCLUDE_UM = windows sdk的um头文件路径
    WINSDK_INCLUDE_UCRT = windows sdk的ucrt头文件路径
    WINSDK_INCLUDE_SHARE = windows sdk的shared头文件路径
    
    WINSDK_LIB_UCRT = windows sdk的ucrt库文件路径
    WINSDK_LIB_UM = windows sdk的um库文件路径
    
    include common-VC16.mk
    
    

    示例:

    APACHE_ROOTDIR = D:\App-Server\Apache24.39-Win64-VS16\Apache24
    
    PYTHON_ROOTDIR = D:\Dev\Python\Python35
    PYTHON_VERSION = 35
    
    MSVC_INCLUDE = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\include
    MSVC_LIB = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\lib\x64
    
    WINSDK_INCLUDE_UM = C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um
    WINSDK_INCLUDE_UCRT = C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt
    WINSDK_INCLUDE_SHARE = C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared
    
    WINSDK_LIB_UCRT = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64
    WINSDK_LIB_UM = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64
    
    include common-VC16.mk
    
    
  5. 修改 common-VC16.mk为如下内容

    CPPFLAGS = \
    /DWIN32 \
    /DNDEBUG \
    /I"$(MSVC_INCLUDE)" \
    /I"$(WINSDK_INCLUDE_UM)" \
    /I"$(WINSDK_INCLUDE_UCRT)" \
    /I"$(WINSDK_INCLUDE_SHARE)" \
    /I"$(PYTHON_ROOTDIR)\include" \
    /I"$(APACHE_ROOTDIR)\include"
    
    CFLAGS = \
    /MD \
    /GF \
    /Gy \
    /O2 \
    /Wall \
    /Zc:wchar_t \
    /Zc:forScope
    
    LDFLAGS = \
    /link \
    /LIBPATH:"$(APACHE_ROOTDIR)\lib" \
    /LIBPATH:"$(PYTHON_ROOTDIR)\libs" \
    /LIBPATH:"$(MSVC_LIB)" \
    /LIBPATH:"$(WINSDK_LIB_UM)" \
    /LIBPATH:"$(WINSDK_LIB_UCRT)" \
    /OPT:REF \
    /OPT:ICF=2 \
    /RELEASE \
    /SUBSYSTEM:WINDOWS
    
    LDLIBS = \
    python$(PYTHON_VERSION).lib \
    libhttpd.lib \
    libapr-1.lib \
    libaprutil-1.lib
    
    SRCFILES = ..\src\server\*.c
    
    mod_wsgi.so : $(SRCFILES)
        cl $(CPPFLAGS) $(CFLAGS) $(SRCFILES) /LD $(LDFLAGS) $(LDLIBS) /OUT:$@
    
    VARIANT = py$(PYTHON_VERSION)-VC16
    
    install : mod_wsgi.so
        copy $? $(APACHE_ROOTDIR)\modules\mod_wsgi-$(VARIANT).so
        :
        :
        :
        :
        : You now need to edit $(APACHE_ROOTDIR)\conf\httpd.conf and add:
        :
        :   LoadModule wsgi_module modules/mod_wsgi-$(VARIANT).so
        :
        :
        :
        :
    
    clean :
        del *.obj *.so *.so.manifest *.lib *.exp
    
    
  6. 修改build-win64-VC16.bat为如下内容

    nmake -f common-VC16.mk clean
    nmake -f ap24py35-win64-VC16.mk install
    nmake -f common-VC16.mk clean
    
  7. 最后,在命令行中执行此文件build-win64-VC16.bat,即可编译成功, 编译成功后会自动将mod_wsgi-py??-VC??.so (本文为mod_wsgi-py35-VC16.so)复制到Apache安装目录的modules文件夹内。加载此模块仍需要自行修改Apache的配置文件。

你可能感兴趣的:(WEB)