TrueOctopus
2020-08-23

Apache2部署flask

环境:
系统:Ubuntu18.04
版本:Apache2.4 python3.6

  1. 安装apache2和wsgi模块

    1
    2
    $ sudo apt-get install apache2
    $ sudo apt-get install libapache2-mod-wsgi-py3

    安装完成后访问80端口,出现初始页面代表apache2安装成功

  2. 在项目根目录下新建wsgi配置文件,范例如下

    1
    2
    3
    4
    5
    import site
    site.addsitedir('/home/zzy/xgkxflask/venv/lib/python3.6/site-packages') # 项目的环境配置
    import sys
    sys.path.insert(0, '/home/zzy/xgkxflask') # 项目根目录
    from flasky import app as application # 启动文件
  3. 配置apache2配置文件,默认位于 /etc/apache2/sites-available/,默认配置为000-default.conf,可自行配置,软链接到sites-enabled文件夹才会生效,范例如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
      <VirtualHost *:80> # 应用的端口
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName trueoctopus.com # 绑定的域名

    ServerAdmin xgkx@hgu.edu.cn # 管理员联系方式,出错时会显示在报错内容中
    DocumentRoot /home/zzy/xgkxflask # 网站根目录

    LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so # 跨域用mod
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
    Header always set Access-Control-Allow-Headers "authorization,content-type"
    # 上面为跨域配置

    WSGIDaemonProcess xgkxflask user=zzy group=zzy processes=4 threads=4
    # 进程配置 用户不得为root用户,processes为进程 threads为线程
    WSGIScriptAlias / /home/zzy/xgkxflask/xgkxflask.wsgi
    # wsgi配置文件地址,将IP:端口/下的请求转发至项目位置
    WSGIPassAuthorization On # 请求头中允许authorization发送

    <Directory /home/zzy/xgkxflask> # 项目地址
    WSGIProcessGroup xgkxflask #项目名
    WSGIApplicationGroup %{GLOBAL}

    #权限设置
    Order allow,deny
    Allow from all
    Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    # 日志位置
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
    </VirtualHost>
  4. 在ports.conf中配置端口,位于/etc/apache2下,服务器安全组需开放

    1
    2
    Listen 80
    Listen 8080
  5. 重启服务

    1
    $ sudo /etc/init.d/apache2 restart