apache2.4 + windows 多站点部署 Django

     阅读:37

apache2.4 + windows 部署 Django

apache2.4 多站点部署

虚拟站点, 多站点 部署 django

文件名称文件路径文件备注
httpd.confC:\Apache24\conf\httpd.confapache 主配置文件 加载模块 监听端口 配置serverName(必须)
httpd-vhosts.confC:\Apache24\conf\extra\httpd-vhosts.conf多站点的配置文件
httpd.exeC:\Apache24\bin\httpd.exeapache 启动服务 httpd -k start | stop | restart
  1. 打开vhosts配置加载

    # httpd.conf
    # Virtual hosts
    Include conf/extra/httpd-vhosts.conf
    
  2. 打开 mod_proxymod_proxy_http 模块加载 ( 反向代理要用到的模块)

    # httpd.conf
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    
  3. 编辑 httpd-vhosts.conf 配置

    # # for further details before you try to setup virtual hosts.
    # You may use the command line option '-S' to verify your virtual host
    # configuration.
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for all requests that do not
    # match a ServerName or ServerAlias in any <VirtualHost> block.
    #
    # first VirtualHost
    <VirtualHost *:80>
    	Define PROJECT_DIR "E:/www/project_root/"  # 建议定义一个 项目主目录 | 网站根目录 方便后面使用
        ServerAdmin server@server.com
        ServerName www.test.com
        ServerAlias www.test.com test.com # 网站别名 可以是旧网站的域名
        ProxyPass "/foo/" "http://192.168.1.xxx:8080/"  # 将 www.test.com/foo/a 的 请求转发 到 192.168.1.xxx:8080/a
        ProxyPassReverse "/foo/"  "http://192.168.1.xxx:3080"  # 将 www.test.com/foo/a 代理到 192.168.1.xxx:8080/a
        
        Alias /static "${PROJECT_DIR}/static"  # 静态文件目录访问
        # 权限 设置
        <Directory "${PROJECT_DIR}/static">
            <RequireAll>
                Require all granted
            </RequireAll>
        </Directory>
        # 日志设置 一天生成一个文件
        SetEnvIf Request_URI "\.(gif|jpg|png|css|js)$" static_request
        ErrorLog "|bin/rotatelogs.exe logs/error-qitay.com-%Y%m%d.log 86400 480" # 错误日志 apaache 有默认的日志格式
        CustomLog "|bin/rotatelogs.exe logs/access-demo.com-%Y%m%d.log 86400 480" common env=!static_request  # env=!static_request 不记静态文件的访问日志
        CustomLog "|bin/rotatelogs.exe logs/details-demo.com-%Y%m%d.log 86400 480" combined  # combined 是在 httpd 文件里面定义的 日志格式
    </VirtualHost>
    
    # second VirtualHost
    
    <VirtualHost *:80>
        ServerAdmin server@qitai.com
        ServerName test.test.com
        ServerAlias test.test.com
        ProxyPass "/foo/" "http://192.168.1.xxx:8080/"
    </VirtualHost>
    

django 项目配置

# httpd-vhosts.conf

# 在本机的python 环境下安装 pip install mod_swgi -r https://pypi.douban.com/simple
# 运行 `mod_wsgi-express module-config`  输出下面三行 
LoadFile "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python37.dll"
LoadModule wsgi_module "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "C:/Users/Administrator/AppData/Local/Programs/Python/Python37"  
<VirtualHost *:80> 
    Define DJANGO_PROJECT "E:/development/online/server/dj_demo"
    ServerAdmin admin@email.com
    ServerName www.demo.com
    ServerAlias www.demo.com
    DocumentRoot "${DJANGO_PROJECT}" # 网站根目录

    # log
    SetEnvIf Request_URI "\.(gif|jpg|png|css|js)$" static_request
    ErrorLog "|bin/rotatelogs.exe logs/error-qitay.com-%Y%m%d.log 86400 480"
    CustomLog "|bin/rotatelogs.exe logs/access-demo.com-%Y%m%d.log 86400 480" common env=!static_request 
    CustomLog "|bin/rotatelogs.exe logs/details-demo.com-%Y%m%d.log 86400 480" combined
    WSGIScriptAlias / "${DJANGO_PROJECT}/dj_demo/wsgi.py"
    # dj_demo wsgi.py 的文件权限
   <Directory "${DJANGO_PROJECT}/dj_demo">
        <Files wsgi.py>
            <RequireAll>
                Require all granted
            </RequireAll>
        </Files>
   </Directory>

    # 设置静态文件路径
    Alias /static "${DJANGO_PROJECT}/static"
    # 静态文件权限
   <Directory "${DJANGO_PROJECT}/static">
        <RequireAll>
            Require all granted
        </RequireAll>
   </Directory>
# dj_demo/wsgi.py 
# 虚拟环境部署 不是虚拟环境的话 就不需要更改
# 这是参考一篇博客的,博客链接 找不到了.... 

import os
import sys
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PROJECT_DIR)  # 项目加入导包路径
virtualenv_dir = os.path.join(PROJECT_DIR, 'env', 'Lib', 'site-packages')  # 虚拟环境python包文件夹
sys.path.insert(0, virtualenv_dir)  # 加入导包路径

from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Qitai.settings')

application = get_wsgi_application()

部署流程大概分三步:

  1. 本机 python 下载 mod_swgi

    pip install mod_swgi -i https://pypi.douban.com/simple

    运行 mod_wsgi-express module-config

    将输出的三行文本 复制 等待使用

  2. 建立项目的虚拟环境

    cd {PROJECT_DIR} && python -m venv env

    pip install -r requirement.txt -i https://pypi.douban.com/simple

    更改 项目的 wsgi.py 文件

  3. 修改 vhosts.conf

    将 复制的三行文本 复制到 vhosts.conf 头部

    然后 复制粘贴上面的配置

    修改 DJANGO_PROJECT ServerName ServerAlias

    有必要的 话 修改 端口

到此为止 django 项目 部署完成

注意 第一步 下载 mod_swgi 只需要进行一次 如果需要发布多个django 网站, 只需要从第二步开始