翼度科技»论坛 编程开发 python 查看内容

windows下使用nginx + waitress 部署django

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
虽然不喜欢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的命令了:
  1. Options:
  2.   -?,-h         : this help
  3.   -v            : show version and exit
  4.   -V            : show version and configure options then exit
  5.   -t            : test configuration and exit
  6.   -T            : test configuration, dump it and exit
  7.   -q            : suppress non-error messages during configuration testing
  8.   -s signal     : send signal to a master process: stop, quit, reopen, reload
  9.   -p prefix     : set prefix path (default: NONE)
  10.   -e filename   : set error log file (default: logs/error.log)
  11.   -c filename   : set configuration file (default: conf/nginx.conf)
  12.   -g directives : set global directives out of configuration file
复制代码
如果把nginx设置到环境变量中,即可在全局使用nginx命令。
3、配置文件
和linux环境下配置一样,这里贴一份基础配置,主要是修改nginx目录下的conf/nginx.conf:
  1. worker_processes  2;
  2. error_log  logs/error.log;
  3. #error_log  logs/error.log  notice;
  4. #error_log  logs/error.log  info;
  5. pid  logs/nginx.pid;
  6. events {
  7.     worker_connections  1024;
  8. }
  9. http {
  10.     include           mime.types;
  11.     default_type      application/octet-stream;
  12.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  13.                       '$status $body_bytes_sent "$http_referer" '
  14.                       '"$http_user_agent" "$http_x_forwarded_for"';
  15.     access_log        logs/access.log  main;
  16.     sendfile          on;
  17.     keepalive_timeout  65;
  18.     #gzip  on;
  19.     server {
  20.         listen       80;
  21.         server_name  localhost 121.199.1.144;
  22.         location / {
  23.             proxy_set_header Host $host;
  24.             proxy_pass http://127.0.0.1:8000;
  25.         }
  26.         location /static {
  27.             alias C:\inetpub\wwwroot\sanxue\static;
  28.         }
  29.         location /media {
  30.             alias C:\inetpub\wwwroot\sanxue\media;
  31.         }
复制代码
下载waitress和使用它

1、下载
  1. pip install waitress
复制代码
2、使用
waitress的使用太简单了,国内使用的人也非常少,在django项目的根目录创建run.py(文件名随意),内容如下:
  1. from waitress import serve
  2. from sanxue.wsgi import application
  3. serve(
  4.     app=application,
  5.     host='127.0.0.1',
  6.     port=8000
  7. )
复制代码
然后使用命令行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】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具