zoukankan      html  css  js  c++  java
  • Django用Apache和mod_wsgi部署

    背景

    最近写了一个Djangowindows上运行,但是用的vbs命令,导致在后台运行的时候,必须用户登录才可以运行,还有就是登录之后,有其他进程可能被干掉。

    Set ws = CreateObject("Wscript.Shell") 
    ws.run "cmd /c D:\xxd\run.bat",vbhide
    
    ` bat 内容
    C:\Python37\python3.exe manage.py runserver 0.0.0.0:8000 >> console.log
    

    第二种创建task的方式,也是只能是用户登录后才可以,不是service级别的,不能开机就运行。
    在这里插入图片描述

    第三种就是用sc创建服务来运行,不过这个需要文件是程序必须是二进制的可执行程序,这就排除了脚本语言和虚拟机语言;其次,程序必须按系统服务的格式编写,编写过程繁琐,可以参考下面的连接:

    https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948。

    不过这个有现成的轮子可以拿来用,就用脚本语言、python/bat等可以直接用,他的名字就是EasyService。但是今天要讲的不是这个,是用apache和mod_wsgi来部署django。

    后面可以从windows迁移到linux也可以运行的话,用Apache httpd来运行,是非常不错的

    准备

    1. 环境准备
    python3.7
    mysql5~8
    还有apache -httpd
    mod_wsgi
    

    保证python和apache都是统一的,都是32位或者都是64位。

    下面是我的pip requirement list

    asn1crypto==0.24.0
    cffi==1.11.5
    cryptography==2.3
    Django==2.0.7
    idna==2.7
    mod-wsgi==4.5.24+ap24vc14
    pip==19.3.1
    pycparser==2.18
    PyMySQL==0.9.2
    pytz==2018.5
    setuptools==41.6.0
    six==1.11.0
    wheel==0.31.1
    

    配置

    创建工程,搭建mysql环境,以及怎么启动。可以参考

    https://www.djangoproject.com/start/

    重点是讲下标题的用 apache和mod_wsgi来部署django。
    什么是mod_wsgi

    mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5.3, mod_wsgi supports Python 2 and 3 (starting from 2.6 and 3.2).

    1. 安装apahce, 并设置一个环境变量
    key: MOD_WSGI_APACHE_ROOTDIR
    value: C:\wamp64\bin\apache\apache<version>\
    
    1. 创建一个virtualHost启动文件,根据自己的修改工程名字和路径
    import os
    import sys
    import site
    
    # Add the site-packages of the chosen virtualenv to work with
    site.addsitedir('C:/Python37/Lib/site-packages')
    
    # Add the app's directory to the PYTHONPATH
    sys.path.append('D:/xxd/Assetlog')
    sys.path.append('D:/xxd/Assetlog/AssetLog')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'AssetLog.settings'
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "AssetLog.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
    1. 安装mod_wdgi
    pip3 install mod_wsgi
    或者直接用pip3 install whl文件
    pip3 install mod_wsgi-4.5.24+ap24vc14-cp37-cp37m-win_amd64.whl 
    # https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 这里下载
    
    1. 配置apache和mod_wsgi
      查看apache配置信息mod_wsgi-express module-config, 如果执行mod_wsgi-express失败,在python安装目录里面找C:\Python37\Scripts,我的在这个目录下。
    LoadFile "c:/python37/python37.dll"
    LoadModule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd"
    WSGIPythonHome "c:/python37"
    

    把这个信息追加到apache配置文件httpd.conf的最后

    最后在配置文件

    下面配置apache虚拟主机,如果有多个django工程,可以添加多个。配置文件在conf\extra\httpd_vhosts.conf里面

    <VirtualHost *:8000>
        ServerName localhost 
        WSGIPassAuthorization On
        ErrorLog "logs/asset.error.log"
        CustomLog "logs/asset.access.log" combined
        WSGIScriptAlias /  "D:/xxd/Assetlog/AssetLog/wsgi_win.py"
        <Directory "D:/xxd/Assetlog/AssetLog">
            <Files wsgi_win.py>
                Require all granted
            </Files>
        </Directory>
    
        Alias /static "D:/xxd/Assetlog/static"
        <Directory "D:/xxd/Assetlog/static">
            Require all granted
        </Directory>  
    </VirtualHost>
    

    参考

    • Apache 虚拟机配置 https://blog.csdn.net/leoly612200/article/details/74913049
    • How to use Django with Apache and mod_wsgi https://django.readthedocs.io/en/2.2.x/howto/deployment/wsgi/modwsgi.html
    • Deploying a Django application in Windows with Apache and mod_wsgi
      https://www.codementor.io/aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
    • http://terokarvinen.com/2017/django-on-apache-with-python-3-on-ubuntu-16-04
  • 相关阅读:
    用例图会不会
    存储过程进阶(vb.net+SQL Server2008环境)
    众说纷纭,我也说“云”
    三层架构之抽象工厂加反射实现数据库转换
    三层架构之抽象工厂加反射&mdash;&mdash;实现数据库转换
    存储过程懂不懂
    8个对于Web设计和开发人员非常有用的在线工具
    TexturePacker的使用(图片打包再一起)
    cocos2dx游戏摇杆的实现方法
    cocos2dx 矩形碰撞检测
  • 原文地址:https://www.cnblogs.com/ievjai/p/14382627.html
Copyright © 2011-2022 走看看