Apache 2.4 部署 Flask 应用


Apache 2.4 部署 Flask 应用

Windows环境

Windows 10 (x64)

安装Apache 2.4

  1. 下载安装包
    • 链接 https://www.apachehaus.com/cgi-bin/download.plx
    • 本文中使用的版本为 2.4.41
    • 注意,务必将服务器安装在磁盘的顶级目录内,如C:\Apache24
  2. 系统服务的安装与卸载
    • 以管理员身份打开命令行,进入安装目录下的bin目录
    • 目录结构如下
      D:\APP-SERVER\APACHE24
      ├─bin
      │  └─iconv
      ├─cgi-bin
      ├─conf
      │  ├─extra
      │  ├─original
      │  │  └─extra
      │  └─ssl
      ├─error
      │  └─include
      ├─htdocs
      ├─icons
      │  └─small
      ├─include
      ├─lib
      ├─logs
      └─modules
      
    • 将Apache安装为系统服务
      .\httpd.exe -k install
      
      自行制定服务名称时,使用 -n 选项
      .\httpd.exe -k install -n "Apache-2.4.41"
      
    • 将系统服务中的Apache卸载
      .\httpd.exe -k uninstall
      
      如果你在安装时指定了名字,卸载时也要用-n选项指定名字
      .\httpd.exe -k uninstall -n "Apahce-2.4.41"
      
  3. 环境变量的设置 - 可选
    • 进入 控制面板 -> 系统和安全 -> 系统
    • 打开Windows的高级系统设置
    • 打开环境变量设置
    • 修改用户变量中的Path变量,为其新增一项,内容为Apache2.4的安装目录下的bin目录
      在这里插入图片描述

在启动前的修改必要的配置

设置SRVROOT和ServerRoot

修改httpd.conf,设置ServerRoot( 服务器所在目录 )与SRVROOT(如果配置文件存在此关键词则设置,否则不必理会,有的配置文件可能没有) 【Apache安装目录/conf/httpd.conf】

Define SRVROOT "D:\Apache24"
ServerRoot "D:\Apache24"

启动与停止Apache2.4

  1. 使用windows的服务进行启动和停止
    在这里插入图片描述
  2. 在命令行启动/停止服务
    启动服务
    httpd.exe -k start
    
    停止服务
    httpd.exe -k stop
    

新增监听地址和端口

若要监听多个端口,则需要修改httpd.conf,新增Listen选项【Apache安装目录/conf/httpd.conf】
httpd.conf截选,如下所示的配置将监听本机全部IP的80端口和换回测试地址的9000端口

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen 127.0.0.1:9000

配置虚拟主机

  1. 修改httpd.conf,加载虚拟主机模块 【Apache安装目录/conf/httpd.conf】
    找到此行内容,把行首的#号去掉
    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    
  2. 修改httpd.conf, 包含子配置文件httpd-vhosts.conf 【Apache安装目录/conf/extra/httpd-vhosts.conf】
    找到此行内容,把行首的#号去掉
    Include conf/extra/httpd-vhosts.conf
    
  3. 修改httpd-vhosts.conf,新增虚拟主机配置,示例如下
    <VirtualHost *:80> #监听本机全部IP的80号端口
      ServerAdmin [email protected]
      DocumentRoot "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend" #指定网站文件位置
      ServerName www.GBlog.com
      ErrorLog "logs/GBlog-error.log" #指定错误日志文件的位置
      TransferLog "logs/GBlog-access.log" #指定访问日志文件的位置
    
       #控制指定文件夹的访问权限和其他权限
      <Directory "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend">
    
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        #
        AllowOverride None
    
        #
        # Controls who can get stuff from this server.
        #
        Require all granted
    </Directory>
    </VirtualHost>
    
  4. 重启Apahce服务
    httpd -k restart
    

配置反向代理 - 跨域的需求

  1. 修改httpd.conf,加载反向代理必备模块 【Apache安装目录/conf/httpd.conf】
    #将一下条目行首的'#'去掉
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    
  2. 修改httpd-vhosts.conf中的虚拟主机配置,示例如下【Apache安装目录/conf/extra/httpd-vhosts.conf】
    <VirtualHost *:80>
      ServerAdmin [email protected]
      DocumentRoot "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend"
      ServerName www.GBlog.com
      ErrorLog "logs/GBlog-error.log"
      TransferLog "logs/GBlog-access.log"
      <Directory "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend">
    
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        #
        AllowOverride None
    
        #
        # Controls who can get stuff from this server.
        #
        Require all granted
    </Directory>
    
    	ProxyPass /api/ http://127.0.0.1:9000/
    	ProxyPassReverse /api/ http://127.0.0.1:9000/
    
    </VirtualHost>
    
    

关于mod_wsgi

自行搜索下载即可
注意
mod_wsgi使用的编译器版本必须与Apache httpd使用的编译器版本保持一致,
如均为VC14,或均为VC9,或均为VC15
若使用Python2.7,那么,你可能只能找到VC9编译的mod_wsgi,此时也请搜索使用VC9编译的Apache,如Apache 2.4.27

Flask应用部署案例 (以一个简易的博客为例)

与下文CentOS 7部署Flask应用方式一致


Linux环境

CentOS 7 (x64)

编译安装Apache 2.4.41

  1. 安装依赖组件, 下载解压安装包
     yum install -y apr apr-util pcre apr-devel apr-util-devel pcre-devel openssl-devel openssl
     yum install -y perl perl-devel
     wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.bz2
     tar -xvf httpd-2.4.41.tar.bz2
    
  2. 配置编译选项
    cd httpd-2.4.41
    ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite -enable-ssl --with-npm=event
    
  3. 编译和安装
    make 
    make install
    

mod_wsgi的编译,安装与加载

首先,在 https://github.com/GrahamDumpleton/mod_wsgi/releases 下载mod_wsgi的源码

Python 2.7.17 + mod_wsgi 4.6.5

注意,建议编译前进入Python 2.7.17的虚拟环境,或者手动指定Python路径
Python2.7.17的动态链接库必须在系统的动态链接库搜索路径中

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz
tar -xvf 4.6.5.tar.gz
cd mod_wsgi-4.6.5

# 指定apxs的路径和Python的路径
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/opt/Python/2.7.17/bin/python2.7
make
make install

在上述流程完成后,即可在apache2安装目录/modules找到mod_wsgi.so
编辑httpd.conf,加载模块mod_wsgi 【Apache安装目录/conf/httpd.conf】

在加载模块处,新增如下行
LoadModule wsgi_module modules/mod_wsgi.so
Python 3.x + mod_wsgi 4.6.5

方式同Python 2.7.x

在启动前的修改必要的配置

测试配置文件是否存语法错误
apachectl -t

配置文件无语法错误则输出
Syntax OK
设置ServerRoot

与windows下配置方式一致

启动与停止Apache2.4

  1. 使用apachectl控制服务
    查询服务的状态
    apachectl status
    
    启动服务
    apachectl start
    
    停止服务
    apachectl stop
    

配置虚拟主机

前置步骤与windows一致,见上文windows配置部分。

httpd-vhost.conf 示例配置如下

# 监听全部IP的80端口
<VirtualHost 0.0.0.0:80>
    ServerAdmin *********@qq.com
    DocumentRoot "/home/web/GBlog/" # 项目所在目录
    <Directory "/home/web/GBlog/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog "logs/GBlog-error_log" # 错误日志
    CustomLog "logs/GBlog-access_log" common # 一般访问日志
</VirtualHost>

配置反向代理 - 跨域的需求

前置步骤与windows一致,见上文windows配置部分。

httpd-vhost.conf 示例配置如下

# 监听全部IP的80端口
<VirtualHost 0.0.0.0:80>
    ServerAdmin *********@qq.com
    DocumentRoot "/home/web/GBlog/" # 项目所在目录
    <Directory "/home/web/GBlog/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog "logs/GBlog-error_log" # 错误日志
    CustomLog "logs/GBlog-access_log" common # 一般访问日志
    
	#将以url以/api/开头的请求转发给本机的9000端口处理
	ProxyPass /api/ http://127.0.0.1:9000/ 
	ProxyPassReverse /api/ http://127.0.0.1:9000/
</VirtualHost>

Flask应用部署案例 (以一个简易的博客为例)

  1. 项目结构如下
    |-- GBlog
    |   |-- blueprints
    |   |   |-- auth.py
    |   |   |-- blog.py
    |   |   |-- __init__.py
    |   |   
    |   |-- __init__.py
    |   |-- models.py
    |   |-- settings.py
    |
    |-- wsgi.py
    
  2. 在httpd-vhost.conf中的相应配置,需要指定wsgi文件的具体位置 【Apache安装目录/conf/extra/httpd-vhost.conf】
    # 监听全部IP的80端口
    <VirtualHost 0.0.0.0:80>
        ServerAdmin *********@qq.com
        DocumentRoot "/home/web/GBlog/" # 项目所在目录
        <Directory "/home/web/GBlog/">
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
        </Directory>
        ErrorLog "logs/GBlog-error_log" # 错误日志
        CustomLog "logs/GBlog-access_log" common # 一般访问日志
        WSGIScriptAlias / "/home/web/GBlog/wsgi.py" # wsgi文件路径
    </VirtualHost>
    
  3. wsgi文件内容 【使用指定的python虚拟环境】
    import os
    import sys
    
    # 获取项目所在的目录的绝对路径,并追加到Python的搜索路径                          
    BASE_DIR = os.path.abspath( os.path.dirname( os.path.dirname(__file__) ) )
    sys.path.append(BASE_DIR)
    
    # 指定要使用的Python虚拟环境的具体路径
    activate_this = os.path.join(BASE_DIR, r'.venv/bin/activate_this.py')
    # 激活Python虚拟环境
    execfile(activate_this, dict(__file__=activate_this)) 
     
    from GBlog import create_app # 此函数创建一个Flask实例
    app = create_app()  
    
    application = app # application务必存在
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章