gpt4 book ai didi

windows下使用nginx+waitress部署django

转载 作者:我是一只小鸟 更新时间:2023-03-07 22:31:43 30 4
gpt4 key购买 nike

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

程序文件

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

下载安装nginx和配置文件

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

其中 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的服务了,比 IIS 或 apache 的简单太多了,跑个中小项目都不成问题.

如果想把以上的命令加到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 。

最后此篇关于windows下使用nginx+waitress部署django的文章就讲到这里了,如果你想了解更多关于windows下使用nginx+waitress部署django的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com