Windows环境下flask+Apache+mod_wsgi部署及爬坑

 

 

安装python

下载相应的安装包python-3.6.5-amd64.exe,直接双击打开即可一步步安装,非常简单

Windows 环境使用virtualenv和virtualenvwrapper

  1. 环境搭建
    • 安装virtualenv:pip install virtualenv
    • 安装virtualenvwrapper:pip install virtualenvwrapper-win
    • 配置环境变量:打开系统环境变量,添加:WORKON_HOME=C:\virtualenvs  注意这个目录是虚拟环境存放的目录
    • 配置完环境变量,一定要重启cmd窗口,要不然环境变量不生效。
  2. 常用命令:
    • 新建虚拟环境:mkvirtualenv BoyPy36
    • 查看所有虚拟环境:workon
    • 进入虚拟环境:workon BoyPy36
    • 退出虚拟环境:进入到虚拟环境的目录,例如:C:\virtualenvs\LibraFlaskPy36\Scripts, 输入:deactivate
    • 激活虚拟环境:进入到虚拟环境的目录,例如:C:\virtualenvs\LibraFlaskPy36\Scripts  输入:activate.bat

安装mod_wsgi

在这个网站  https://www.lfd.uci.edu/~gohlke/pythonlibs/  上找到编译好的包,进入到安装包的路径,输入如下命令,进行安装

pip install mod_wsgi-4.5.24+ap24vc14-cp36-cp36m-win_amd64.whl

 

安装nginx

本来想安装nginx来着,后来大量查阅资料,发现windows下还是Apache用着顺手,以下安装nginx步骤仅供参考,后面会详细介绍Apache的安装和配置

  1. 下载相应的安装包:http://nginx.org/en/download.html
  2. 输入如下命令:
    cd c:\
    unzip nginx-1.15.5.zip
    cd nginx-1.15.5
    start nginx
  1. 运行tasklist查看运行进程:
    C:\nginx-1.15.5>tasklist /fi "imagename eq nginx.exe"
    
    Image Name           PID Session Name     Session#    Mem Usage
    =============== ======== ============== ========== ============
    nginx.exe            652 Console                 0      2 780 K
    nginx.exe           1332 Console                 0      3 112 K
  2. 在浏览器输入:http://localhost,显示欢迎页,表示启动成功
  3. 常用相关命令:
    nginx -s stop    fast shutdown
    nginx -s quit    graceful shutdown
    nginx -s reload    changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
    nginx -s reopen    re-opening log files

     

安装Apache

  1. 去官网  http://httpd.apache.org/download.cgi  下载相应的安装包,解压,打开cmd终端,进入到Apache/bin目录,输入如下命令,不报错表明安装成功
    httpd -k install
  2. 双击ApacheMonitor.exe,打开服务管理UI界面,可以对Apache服务进行管理。
  3. 也可以用命令对Apache服务进行管理
    httpd -k [start|stop|restart]    # 用来 启动|停止|重启 服务
  4. 如果有报错,可以到Apache/logs目录下查看日志:error.log,看相关的报错信息,再进行具体问题具体解决。

遇到的坑

安装Apache遇到的坑

  1. 配置apache,在Apache24/conf/httpd.conf的最后添加上如下的配置,使用mod_wsgi-express module-config > myconfig.txt,可以得到 mod_uwsgi 配置:
    ################################################################################
    # mod_wsgi 配置
    LoadFile "c:/python36/python36.dll"
    LoadModule wsgi_module "c:/virtualenvs/libraflaskpy36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
    WSGIPythonHome "c:/virtualenvs/libraflaskpy36"
    
    # 参考:https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html
    <VirtualHost *>
        ServerName 192.168.6.27:80    # 这里我少写了80端口,坑死了
        WSGIScriptAlias / C:\tools\ZLflask\Libra.wsgi
        <Directory C:\tools\ZLflask>
                # Order deny,allow
                # Allow from all
                Require all granted
        </Directory>
    </VirtualHost>

中间有一段配置也要改,这里坑死,搞了好久:

<Directory />
    #AllowOverride none
    #Require all denied

    AllowOverride All    # 改成这样的配置,为了让别人访问到这个IP地址
    Require all granted
</Directory>
...
ServerName 192.168.6.27:80    # 修改ip地址

修改证书的配置,去掉ssl认证,因为是公司内部使用,不需要绑定域名和认证,将下面这句话注释掉:

LoadModule ssl_module modules/mod_ssl.so
  1. 新建app.wsgi文件,写上如下代码:
    # 添加虚拟环境的路径
    activate_this = 'C:\\virtualenvs\\LibraFlaskPy36\\Scripts\\activate_this.py'
    with open(activate_this) as file_:
        exec(file_.read(), dict(__file__=activate_this))

在这里重点说明下,配置的时候,一直报错:

[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286] Traceback (most recent call last):\r, referer: http://192.168.6.27/index
[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286]   File "C:/tools/ZLflask/Libra.wsgi", line 5, in <module>\r, referer: http://192.168.6.27/index
[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286]     from app import app as application\r, referer: http://192.168.6.27/index
[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286] ModuleNotFoundError: No module named 'app'\r, referer: http://192.168.6.27/index

后来找了好久,加上下面的2行代码,成功了:

import sys
sys.path.insert(0, "C:\\tools\\ZLflask")

app.wsgi 文件的全部代码:

# 添加虚拟环境的路径
activate_this = 'C:\\virtualenvs\\LibraFlaskPy36\\Scripts\\activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# 将项目的路径添加到系统中,不然就会报错
import sys
sys.path.insert(0, "C:\\tools\\ZLflask")

from app import app as application

安装mod_uwsgi的坑

安装的时候一直报这个错:

Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Dell\AppData\Local\Temp\pip-install-pnicet59\mod-wsgi\setup.py", line 158, in <module>
        raise RuntimeError('No Apache installation can be found. Set the '
    RuntimeError: No Apache installation can be found. Set the MOD_WSGI_APACHE_ROOTDIR environment to its location.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\Dell\AppData\Local\Temp\pip-install-pnicet59\mod-wsgi\

解决办法:

在这个网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/上找到编译好的包进行安装,重启Nginx好了。

洛水之风的公众号

在这里插入图片描述

转载于:https://www.cnblogs.com/DeaconOne/p/10787527.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值