• windows下使用nginx + waitress 部署django


    虽然不喜欢IIS,不过有些项目又必须部署在windows上,windows下部署django的方案有IIS + wfastcgiapache + mod_wsgi,也有超简单的部署方式如:nginx + waitress,本文主要讲的是最后一种部署方式。

    程序文件

    随便找个目录放置好程序文件

    下载安装nginx和配置文件

    1、下载
    下载链接:http://nginx.org/en/download.html
    一直都只在linux中使用nginx,还从未在windows中使用,感觉在windows中使用nginx更为简单
    2、安装
    下载的是一个压缩包,找个目录解压即可,无需安装,解压出来的内容为:
    image

    其中nginx.exe是入口程序,不考虑系统命令的情况下,cd到当前目录,即可使用nginx的命令了:

    Options:
      -?,-h         : this help
      -v            : show version and exit
      -V            : show version and configure options then exit
      -t            : test configuration and exit
      -T            : test configuration, dump it and exit
      -q            : suppress non-error messages during configuration testing
      -s signal     : send signal to a master process: stop, quit, reopen, reload
      -p prefix     : set prefix path (default: NONE)
      -e filename   : set error log file (default: logs/error.log)
      -c filename   : set configuration file (default: conf/nginx.conf)
      -g directives : set global directives out of configuration file
    

    如果把nginx设置到环境变量中,即可在全局使用nginx命令。

    3、配置文件
    和linux环境下配置一样,这里贴一份基础配置,主要是修改nginx目录下的conf/nginx.conf

    worker_processes  2;
    
    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    pid  logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include           mime.types;
        default_type      application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log        logs/access.log  main;
        sendfile          on;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost 121.199.1.144;
    
            location / {
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8000;
            }
    
            location /static {
                alias C:\inetpub\wwwroot\sanxue\static;
            }
    
            location /media {
                alias C:\inetpub\wwwroot\sanxue\media;
            }
    

    下载waitress和使用它

    1、下载

    pip install waitress
    

    2、使用
    waitress的使用太简单了,国内使用的人也非常少,在django项目的根目录创建run.py(文件名随意),内容如下:

    from waitress import serve
    from sanxue.wsgi import application
    
    serve(
        app=application,
        host='127.0.0.1',
        port=8000
    )
    

    然后使用命令行python run.py即可启动django的服务了,比IISapache的简单太多了,跑个中小项目都不成问题。

    如果想把以上的命令加到windwos服务中,可参考下面的第3点。

    参考

    1、waitress官方文档https://docs.pylonsproject.org/projects/waitress/en/stable/index.html
    2、如何在python web项目中使用waitress https://www.devdungeon.com/content/run-python-wsgi-web-app-waitress
    3、如何把python项目构建成windows服务 https://www.devdungeon.com/content/run-python-script-windows-service

  • 相关阅读:
    华为数通方向HCIP-DataCom H12-831题库(单选题:41-60)
    Java程序设计入门教程--Java语言概述
    【学习笔记】指向常量的指针和常量指针
    SOME/IP TTL 在各种Entry 中各是什么意思?有什么限制?
    SpringBoot:Web开发之Filter实践
    Android13版本新特性介绍
    力扣第797题 所有可能的路径 C++ 深度优先搜索 +java
    Javaweb三大组件知识点记录
    不建议升级windows11的理由
    代码随想录二叉树——对称二叉树
  • 原文地址:https://www.cnblogs.com/mooremok/p/17188510.html