How to use Django with uWSGI

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/uwsgi/

https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html


uWSGI is a fast, self-healing and developer/sysadmin-friendly applicationcontainer server coded in pure C.

See also

The uWSGI docs offer a tutorial covering Django, nginx, and uWSGI (onepossible deployment setup of many). The docs below are focused on how tointegrate Django with uWSGI.

Prerequisite: uWSGI

The uWSGI wiki describes several installation procedures. Using pip, thePython package manager, you can install any uWSGI version with a singlecommand. For example:

# Install current stable version.
$ sudo pip install uwsgi

# Or install LTS (long term support).
$ sudo pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz

uWSGI model

uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache)communicates with a django-uwsgi “worker” process to serve dynamic content.See uWSGI’sbackground documentation for more detail.

Configuring and starting the uWSGI server for Django

uWSGI supports multiple ways to configure the process. See uWSGI’sconfiguration documentation andexamples

Here’s an example command to start a uWSGI server:

uwsgi --chdir=/path/to/your/project \
    --module=mysite.wsgi:application \
    --env DJANGO_SETTINGS_MODULE=mysite.settings \
    --master --pidfile=/tmp/project-master.pid \
    --socket=127.0.0.1:49152 \      # can also be a file
    --processes=5 \                 # number of worker processes
    --uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges
    --harakiri=20 \                 # respawn processes taking more than 20 seconds
    --max-requests=5000 \           # respawn processes after serving 5000 requests
    --vacuum \                      # clear environment on exit
    --home=/path/to/virtual/env \   # optional path to a virtualenv
    --daemonize=/var/log/uwsgi/yourproject.log      # background the process

This assumes you have a top-level project package named mysite, andwithin it a module mysite/wsgi.py that contains a WSGI applicationobject. This is the layout you’ll have if you randjango-admin.pystartprojectmysite (using your own project name in place of mysite) witha recent version of Django. If this file doesn’t exist, you’ll need to createit. See theHow to deploy with WSGI documentation for the defaultcontents you should put in this file and what else you can add to it.

The Django-specific options here are:

  • chdir: The path to the directory that needs to be on Python’s importpath – i.e., the directory containing themysite package.
  • module: The WSGI module to use – probably themysite.wsgi modulethatstartproject creates.
  • env: Should probably contain at leastDJANGO_SETTINGS_MODULE.
  • home: Optional path to your project virtualenv.

Example ini configuration file:

[uwsgi]
chdir=/path/to/your/project
module=mysite.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/yourproject.log

Example ini configuration file usage:

uwsgi --ini uwsgi.ini

See the uWSGI docs on managing the uWSGI process for information onstarting, stoping and reloading the uWSGI workers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值