2011-06-16 16:34:12 +00: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.
|
|
|
|
|
2015-11-29 16:29:46 +00:00
|
|
|
.. _uWSGI: https://projects.unbit.it/uwsgi/
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2013-03-30 20:21:59 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
The uWSGI docs offer a `tutorial`_ covering Django, nginx, and uWSGI (one
|
|
|
|
possible deployment setup of many). The docs below are focused on how to
|
|
|
|
integrate Django with uWSGI.
|
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
.. _tutorial: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
|
2013-03-30 20:21:59 +00:00
|
|
|
|
2011-06-16 16:34:12 +00:00
|
|
|
Prerequisite: uWSGI
|
|
|
|
===================
|
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
The uWSGI wiki describes several `installation procedures`_. Using pip, the
|
|
|
|
Python package manager, you can install any uWSGI version with a single
|
2012-03-03 09:11:54 +00:00
|
|
|
command. For example:
|
|
|
|
|
2015-02-19 03:19:21 +00:00
|
|
|
.. code-block:: console
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
# Install current stable version.
|
2014-09-20 05:58:49 +00:00
|
|
|
$ pip install uwsgi
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
# Or install LTS (long term support).
|
2015-11-29 16:29:46 +00:00
|
|
|
$ pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
.. _installation procedures: https://uwsgi-docs.readthedocs.io/en/latest/Install.html
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2013-06-03 13:53:04 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Some distributions, including Debian and Ubuntu, ship an outdated version
|
|
|
|
of uWSGI that does not conform to the WSGI specification. Versions prior to
|
|
|
|
1.2.6 do not call ``close`` on the response object after handling a
|
|
|
|
request. In those cases the :data:`~django.core.signals.request_finished`
|
|
|
|
signal isn't sent. This can result in idle connections to database and
|
|
|
|
memcache servers.
|
|
|
|
|
2011-06-16 16:34:12 +00:00
|
|
|
uWSGI model
|
|
|
|
-----------
|
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache)
|
2016-11-15 22:00:50 +00:00
|
|
|
communicates with a `django-uwsgi` "worker" process to serve dynamic content.
|
2012-02-24 23:24:30 +00:00
|
|
|
See uWSGI's `background documentation`_ for more detail.
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2015-11-29 16:29:46 +00:00
|
|
|
.. _background documentation: https://projects.unbit.it/uwsgi/wiki/Background
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-02-24 23:24:30 +00:00
|
|
|
Configuring and starting the uWSGI server for Django
|
|
|
|
----------------------------------------------------
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
uWSGI supports multiple ways to configure the process. See uWSGI's
|
2014-12-06 21:49:59 +00:00
|
|
|
`configuration documentation`_ and `examples`_.
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
.. _configuration documentation: https://uwsgi.readthedocs.io/en/latest/Configuration.html
|
2015-11-29 16:29:46 +00:00
|
|
|
.. _examples: https://projects.unbit.it/uwsgi/wiki/Example
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
Here's an example command to start a uWSGI server::
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2012-07-06 09:10:27 +00:00
|
|
|
uwsgi --chdir=/path/to/your/project \
|
2012-03-28 20:28:13 +00:00
|
|
|
--module=mysite.wsgi:application \
|
2011-10-22 04:30:10 +00:00
|
|
|
--env DJANGO_SETTINGS_MODULE=mysite.settings \
|
2011-06-16 16:34:12 +00: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
|
|
|
|
--max-requests=5000 \ # respawn processes after serving 5000 requests
|
|
|
|
--vacuum \ # clear environment on exit
|
2012-03-02 17:16:52 +00:00
|
|
|
--home=/path/to/virtual/env \ # optional path to a virtualenv
|
2011-06-16 16:34:12 +00:00
|
|
|
--daemonize=/var/log/uwsgi/yourproject.log # background the process
|
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
This assumes you have a top-level project package named ``mysite``, and
|
2011-10-22 04:30:10 +00:00
|
|
|
within it a module :file:`mysite/wsgi.py` that contains a WSGI ``application``
|
2014-07-26 11:21:52 +00:00
|
|
|
object. This is the layout you'll have if you ran ``django-admin
|
2011-10-22 04:30:10 +00:00
|
|
|
startproject mysite`` (using your own project name in place of ``mysite``) with
|
2012-03-02 17:16:52 +00:00
|
|
|
a recent version of Django. If this file doesn't exist, you'll need to create
|
2011-10-22 04:30:10 +00:00
|
|
|
it. See the :doc:`/howto/deployment/wsgi/index` documentation for the default
|
2012-03-02 17:16:52 +00:00
|
|
|
contents you should put in this file and what else you can add to it.
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2011-10-22 04:30:10 +00:00
|
|
|
The Django-specific options here are:
|
|
|
|
|
2012-03-02 17:16:52 +00:00
|
|
|
* ``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
|
|
|
|
that :djadmin:`startproject` creates.
|
|
|
|
* ``env``: Should probably contain at least ``DJANGO_SETTINGS_MODULE``.
|
|
|
|
* ``home``: Optional path to your project virtualenv.
|
2011-06-16 16:34:12 +00:00
|
|
|
|
|
|
|
Example ini configuration file::
|
|
|
|
|
|
|
|
[uwsgi]
|
|
|
|
chdir=/path/to/your/project
|
2012-03-28 20:28:13 +00:00
|
|
|
module=mysite.wsgi:application
|
2011-06-16 16:34:12 +00:00
|
|
|
master=True
|
|
|
|
pidfile=/tmp/project-master.pid
|
|
|
|
vacuum=True
|
|
|
|
max-requests=5000
|
2012-03-10 10:03:33 +00:00
|
|
|
daemonize=/var/log/uwsgi/yourproject.log
|
2011-06-16 16:34:12 +00:00
|
|
|
|
|
|
|
Example ini configuration file usage::
|
|
|
|
|
|
|
|
uwsgi --ini uwsgi.ini
|
|
|
|
|
2015-11-07 14:35:07 +00:00
|
|
|
.. admonition:: Fixing ``UnicodeEncodeError`` for file uploads
|
|
|
|
|
|
|
|
If you get a ``UnicodeEncodeError`` when uploading files with file names
|
|
|
|
that contain non-ASCII characters, make sure uWSGI is configured to accept
|
|
|
|
non-ASCII file names by adding this to your ``uwsgi.ini``::
|
|
|
|
|
2016-07-25 11:01:04 +00:00
|
|
|
env = LANG=en_US.UTF-8
|
2015-11-07 14:35:07 +00:00
|
|
|
|
|
|
|
See the :ref:`unicode-files` section of the Unicode reference guide for
|
|
|
|
details.
|
|
|
|
|
2012-02-24 23:24:30 +00:00
|
|
|
See the uWSGI docs on `managing the uWSGI process`_ for information on
|
2013-07-17 10:50:40 +00:00
|
|
|
starting, stopping and reloading the uWSGI workers.
|
2011-06-16 16:34:12 +00:00
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
.. _managing the uWSGI process: https://uwsgi-docs.readthedocs.io/en/latest/Management.html
|