2011-06-17 00:34:12 +08:00
|
|
|
============================
|
|
|
|
How to use Django with uWSGI
|
|
|
|
============================
|
|
|
|
|
|
|
|
.. highlight:: bash
|
|
|
|
|
|
|
|
uWSGI_ is a fast, self-healing and developer/sysadmin-friendly application
|
|
|
|
container server coded in pure C.
|
|
|
|
|
|
|
|
.. _uWSGI: http://projects.unbit.it/uwsgi/
|
|
|
|
|
|
|
|
Prerequisite: uWSGI
|
|
|
|
===================
|
|
|
|
|
|
|
|
The wiki describes several `installation procedures`_. Using pip, the python
|
|
|
|
package manager, installing any uWSGI version can be done with one command
|
|
|
|
line. For example::
|
|
|
|
|
|
|
|
# install current stable version
|
|
|
|
pip install uwsgi
|
|
|
|
|
|
|
|
# or install LTS (long term support)
|
|
|
|
pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz
|
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
.. _installation procedures: http://projects.unbit.it/uwsgi/wiki/Install
|
2011-06-17 00:34:12 +08:00
|
|
|
|
|
|
|
uWSGI model
|
|
|
|
-----------
|
|
|
|
|
|
|
|
uWSGI operates on a client-server model. Your Web server (ie. nginx, Apache)
|
2011-10-22 12:30:10 +08:00
|
|
|
communicates with a django-uwsgi "worker" process to serve dynamic content.
|
2012-02-25 07:24:30 +08:00
|
|
|
See uWSGI's `background documentation`_ for more detail.
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
.. _background documentation: http://projects.unbit.it/uwsgi/wiki/Background
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
Configuring and starting the uWSGI server for Django
|
|
|
|
----------------------------------------------------
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
uWSGI supports multiple ways to configure the process, see uWSGI's
|
|
|
|
`configuration documentation`_ and `examples`_
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
.. _configuration documentation: http://projects.unbit.it/uwsgi/wiki/Doc
|
|
|
|
.. _examples: http://projects.unbit.it/uwsgi/wiki/Example
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
An example command to start a uWSGI server::
|
2011-06-17 00:34:12 +08:00
|
|
|
|
|
|
|
uwsgi --chdir=/path/to/your/project
|
2011-10-22 12:30:10 +08:00
|
|
|
--module='mysite.wsgi:application' \
|
|
|
|
--env DJANGO_SETTINGS_MODULE=mysite.settings \
|
2011-06-17 00:34:12 +08:00
|
|
|
--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
|
|
|
|
--limit-as=128 \ # limit the project to 128 Megabytes
|
|
|
|
--max-requests=5000 \ # respawn processes after serving 5000 requests
|
|
|
|
--vacuum \ # clear environment on exit
|
|
|
|
--home=/path/to/virtual/env \ # optionnal path to a virtualenv
|
|
|
|
--daemonize=/var/log/uwsgi/yourproject.log # background the process
|
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
This assumes that you have a top-level project package named ``mysite``, and
|
|
|
|
within it a module :file:`mysite/wsgi.py` that contains a WSGI ``application``
|
|
|
|
object. This is the layout you will have if you ran ``django-admin.py
|
|
|
|
startproject mysite`` (using your own project name in place of ``mysite``) with
|
|
|
|
a recent version of Django. If this file does not exist, you'll need to create
|
|
|
|
it. See the :doc:`/howto/deployment/wsgi/index` documentation for the default
|
|
|
|
contents you should put in this file, and what else you can add to it.
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
The Django-specific options here are:
|
|
|
|
|
|
|
|
* ``chdir``: the path to the directory that needs to be on Python's import path; i.e. the directory containing the ``mysite`` package.
|
|
|
|
* ``module``: The WSGI module to use, probably the ``mysite.wsgi`` module which
|
|
|
|
:djadmin:`startproject` creates.
|
|
|
|
* ``env``: should probably contain at least ``DJANGO_SETTINGS_MODULE``
|
|
|
|
* ``home``: optional path to your project virtualenv
|
2011-06-17 00:34:12 +08:00
|
|
|
|
|
|
|
Example ini configuration file::
|
|
|
|
|
|
|
|
[uwsgi]
|
|
|
|
chdir=/path/to/your/project
|
2011-10-22 12:30:10 +08:00
|
|
|
module='mysite.wsgi:application'
|
2011-06-17 00:34:12 +08:00
|
|
|
master=True
|
|
|
|
pidfile=/tmp/project-master.pid
|
|
|
|
vacuum=True
|
|
|
|
max-requests=5000
|
|
|
|
deamonize=/var/log/uwsgi/yourproject.log
|
|
|
|
|
|
|
|
Example ini configuration file usage::
|
|
|
|
|
|
|
|
uwsgi --ini uwsgi.ini
|
|
|
|
|
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
See the uWSGI docs on `managing the uWSGI process`_ for information on
|
|
|
|
starting, stoping, and reloading the uWSGI workers.
|
2011-06-17 00:34:12 +08:00
|
|
|
|
2012-02-25 07:24:30 +08:00
|
|
|
.. _managing the uWSGI process: http://projects.unbit.it/uwsgi/wiki/Management
|