Django + gunicorn + nginx


1.安装gunicorn

pip install gunicorn

2.保证django的runserver测试环境能运行起来

3.使用gunicorn启动django环境

  1. Gunicorn是一个开源Python WSGI UNIX的HTTP服务器,Github仓库地址在这,传说速度快(配置快、运行快)、简单,默认是同步工作,支持Gevent、Eventlet异步,支持Tornado,官方有很详细的文档可以参阅。
  2. 需要在你的Django项目的settings.py中的INSTALLED_APPS加入:gunicorn
nohup gunicorn --worker-class=gevent isaced.wsgi:application -b 0.0.0.0:1601 &
  • --worker-class
    指定工作方式,这里我用的gevent
    如果提示You need gevent installed to use this worker则表示你还没有安装 gevent。
  • isaced.wsgi:application
    这里是指你的项目名,在Django创建项目的时候会自动生成对应名字文件夹中的wsgi.py,这里就是指的它。(Python_20160906.wsgi:app)Python_20160905的django项目,里面的app应用
  • 我的项目启动文件变成了start.py,但是不担心,因为wsgi返回的是个wsgihandler,我的启动文件,最后返回的也是wsgihandler,所以支持异步,支持websocket,完全没有问题

4.更改nginx配置文件

upstream python_villagers.web {
    server 127.0.0.1:1601 weight=10 max_fails=2 fail_timeout=30s ; } server { listen 80; server_name www.x666.com; access_log /var/log/nginx/x666.log; location / { proxy_pass http://python_villagers.web; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /static/ { root /data/isaced; #Django项目所在目录 } } 
 





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM