辽宁球迷 发表于 2023-3-7 16:19:28

windows下使用nginx + waitress 部署django

虽然不喜欢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_processes2;

error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

pidlogs/nginx.pid;

events {
    worker_connections1024;
}

http {
    include         mime.types;
    default_type      application/octet-stream;
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log      logs/access.logmain;
    sendfile          on;
    keepalive_timeout65;

    #gzipon;

    server {
      listen       80;
      server_namelocalhost 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 waitress2、使用
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

来源:https://www.cnblogs.com/mooremok/p/17188510.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: windows下使用nginx + waitress 部署django