总的概念

常用命令

httpd -t 检测配置语法
httpd -k start 启动服务器
httpd -k restart 重启启服务器
httpd -k stop 停止服务器
httpd -k install -n “Apache2.4” 安装Apache到Windows服务中
注意:需添加httpd的环境变量path 或者直接在httpd目录运行cmd

python + wsgi +flask + apache + windows 10

注意:

  1. apache python wsgi模块均需要位数一致,比如32位还是64位
  2. python wsgi python版本要一致

第三方库下载链接 wheel
https://www.lfd.uci.edu/~gohlke/pythonlibs/

apache下载链接
https://www.apachehaus.com/cgi-bin/download.plx

http.conf 参考配置

  1. KeepAlive Off 关闭长连接
  2. ServerTokens Prod 最低服务器信息
  3. LoadFile "C:\Python37-32\python36.dll"
  4. LoadModule wsgi_module "C:\Python37-32\site-packages\mod_wsgi\server\mod_wsgi.cp36-win_amd64.pyd"
  5. # PythonHome寻找第三方库路径
  6. WSGIPythonHome "C:\Python37-32"
  7. # 严格模式关闭
  8. WSGIRestrictStdout Off
  9. #防止路径查找不到,WSGI和平时python运行环境不一样
  10. WSGIPythonPath C:\H\Git\xieyinote\自动化笔记\QQ
  11. # wsgi脚本自动加载,但wsgi脚本加载的其他脚本无法自动加载更新,还是旧的
  12. WSGIScriptReloading On
  13. <VirtualHost *:80>
  14. LogFormat "%h %{%Y-%m-%d %T}t \"%r\" %>s %B" mylog
  15. CustomLog logs/mylog.txt mylog
  16. ErrorLog logs/mylog_error.log
  17. WSGIScriptAlias / C:\H\Git\test-wsgi.py
  18. DocumentRoot C:\H\Git
  19. <Directory C:\H\Git>
  20. Require all granted
  21. </Directory>
  22. </VirtualHost>

windows 下mpm 配置

MPM CONFIG

# WinNT MPM
# ThreadsPerChild: constant number of worker threads in the server process
# MaxRequestsPerChild: maximum  number of requests a server process serves
<IfModule mpm_winnt_module>
    ThreadsPerChild      150
    MaxRequestsPerChild    1000 
</IfModule>

//MaxRequestsPerChild 这个默认是0,可能内存泄漏
MaxRequestsPerChild 在windows下处理N个请求后会新开进程,进程ID会变化,原守护进程不变
ThreadsPerChild为并发处理能力,能同时处理多少个请求,不然要排队等待
httpd在windows下一般会有2个进程,但也可能3个甚至更多进程,是由于原进程任务还未执行完毕.

新开进程和原进程全局变量不通用

遇到的问题

直接停止,无任何提示

请记住 mod_wsgi 不允许使用 sys.stdout 和 sys.stderr

import sys
sys.stdout = sys.stderr

一些常见问题
https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html

selenium +chrome 无任何界面

Chrome.headless 在apache服务模式下无效,默认无头
如果想有界面,请在cmd命令下直接运行httpd命令

多开selenium + chrome
平均每个使用内存大约 120m,基本为chrome,其占用内存110M左右

apache中error log

wsgi:error是代码中的print语句引起的,注释掉所有的print语句后问题解决