flask 部署在apache24.* + wsgi 填坑总结

  1. 下载安装apachepython

  •  打开apache官网http://www.apache.org/   
  •  点击右上角Download,出现如下界面 ,这里是各镜像服务器,随便找一个,这里用的是推荐版。
  •  出现目录列表,这些是apache的项目列表,我也不明白apahce为何用这种方式浏览。
  • 点“httpd",出现如下界面 
  • 点红框部分,出现以下界面
  •    进入以下界面后,选择第一项ApacheHaus,这是个第三方下载平台,在它的网站下载独立的Apache会是一个压缩包。另外四个中,第二个也是独立的Apache下载地址,另外三个是集成开发环境。
  • 在新的界面中,会发现VC9和VC11字样,经过阅读相关内容得知,VC9是指用VS2008编译的代码,而VC11是用VS2012编译的,而用VS2012编译的没法在Windows XP和Server 2003中使用。

2.安装apacheweb

把下载文件解压到你要安装目录,如:d:express

安装服务:打开cmd 进入apache\bin 文件 执行httpd.exe -k install -n Apache2.4apache

该命令的意思是,安装apache服务,并将该服务名称命名为Apache2.4(你也能够改为别的)服务器

若是报一下错误app

sock: could not bind to address [::]:443
(OS 10013)以一种访问权限不容许的方式作了一个访问套接字的尝试。  : AH00072: make_
sock: could not bind to address 0.0.0.0:443dom

说明你的443端口已经呗占用,则须要修改 httpd-ssl.conf或httpd-ahssl.conf中的443端口改为其它端口号,若是用不到443端口(用于https)能够直接在httpd.config中,直接屏蔽掉对应的配置,以下:ide

<IfModule ssl_module>
#Include conf/extra/httpd-ssl.conf
#Include conf/extra/httpd-ahssl.conf
#SSLRandomSeed startup builtin
#SSLRandomSeed connect builtin
</IfModule>网站

启动apache 服务能正常启动说明安装成功。ui

3.安装mod_wsgi

设置环境变量“MOD_WSGI_APACHE_ROOTDIR”为Apache安装目录 如D:\Apache24

而后运行pip install mod_wsgi 进行安装,下载过程当中若是遇到以下错误:

    building 'mod_wsgi.server.mod_wsgi' extension
    error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat).
Get it from http://aka.ms/vcpython27

按指定目录下载vcpython27并安装就能够了。

不知为何用pip自动安装的匹配的是mod_wsgi 4.5.23 但安装一直失败,报以下错误

D:\Heatnetwork\Apache24/include\httpd.h(44) : fatal error C1083: Cannot open inc
lude file: 'ap_config.h': No such file or directory

因而我下载了“mod_wsgi-4.5.22+ap24vc9-cp27-cp27m-win_amd64”的版本再进行安装成功。下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

4.配置mod_wsgi

网上有的说解压mod_wsgi对应的whl文件找到mod_wsgi.so文件放到apache安装目录里面 modules 目录下,但我解压后怎么也找不到mod_wsgi.so文件。找到其它方法

进行安装在安装成功后在python的安装目录的\scripts文件夹下运行 mod_wsgi-express module-config  

输出以下:

把输出中的"mod_wsgiNone"改为“mod_wsgi”, 而后拷贝到httpd.conf文件的中,以下图:

5.新建wsgi文件,即下面要配置的weatherSpiderV3.wsgi文件,文件名本身按本身状况定义

    import sys 
    #Expand Python classes path with your app's path  
    sys.path.insert(0, "E:/PythonWeatherServer") 
    from weatherSpiderV3 import app
    #Put logging code (and imports) here ... 
    #Initialize WSGI app object  
    application = app 

6.配置Virtual hosts

    去掉“# Include conf/extra/httpd-vhosts.conf” 前的#号 以下:

    # Virtual hosts
    Include conf/extra/httpd-vhosts.conf

    打开httpd-vhosts.conf文件,注释掉DocumentRoot "${SRVROOT}/htdocs" 以下:

    <VirtualHost _default_:80>
    #DocumentRoot "${SRVROOT}/htdocs"
    #ServerName www.example.com:80
    </VirtualHost>

添加一个

<VirtualHost *:2002>
    ServerName localhost
    #ServerAlias www.test.com
    #ServerAdmin root@test.com
    DocumentRoot "E:/PythonWeatherServer"
    #ErrorLog "E:/PythonWeatherServer/error.log"
    WSGIScriptAlias / E:/PythonWeatherServer/weatherSpiderV3.wsgi
    #Alias /static F:/web/static

    <Directory "E:/PythonWeatherServer">
        #Options +ExecCGI
        #AddHandler cgi-script .py
        #Options -Indexes +FollowSymLinks
        Require all granted  
        AllowOverride All
        WSGIScriptReloading On
    </Directory>
</VirtualHost>

按本身的实际状况进行配置“端口号、ServerName、DocumentRoot、WSGIScriptAlias、<Directory”等。

重启服务器便可。

相关文章
相关标签/搜索