uwsgi.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ============================
  2. How to use Django with uWSGI
  3. ============================
  4. .. highlight:: bash
  5. uWSGI_ is a fast, self-healing and developer/sysadmin-friendly application
  6. container server coded in pure C.
  7. .. _uWSGI: http://projects.unbit.it/uwsgi/
  8. .. seealso::
  9. The uWSGI docs offer a `tutorial`_ covering Django, nginx, and uWSGI (one
  10. possible deployment setup of many). The docs below are focused on how to
  11. integrate Django with uWSGI.
  12. .. _tutorial: https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
  13. Prerequisite: uWSGI
  14. ===================
  15. The uWSGI wiki describes several `installation procedures`_. Using pip, the
  16. Python package manager, you can install any uWSGI version with a single
  17. command. For example:
  18. .. code-block:: bash
  19. # Install current stable version.
  20. $ sudo pip install uwsgi
  21. # Or install LTS (long term support).
  22. $ sudo pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz
  23. .. _installation procedures: http://projects.unbit.it/uwsgi/wiki/Install
  24. .. warning::
  25. Some distributions, including Debian and Ubuntu, ship an outdated version
  26. of uWSGI that does not conform to the WSGI specification. Versions prior to
  27. 1.2.6 do not call ``close`` on the response object after handling a
  28. request. In those cases the :data:`~django.core.signals.request_finished`
  29. signal isn't sent. This can result in idle connections to database and
  30. memcache servers.
  31. uWSGI model
  32. -----------
  33. uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache)
  34. communicates with a django-uwsgi "worker" process to serve dynamic content.
  35. See uWSGI's `background documentation`_ for more detail.
  36. .. _background documentation: http://projects.unbit.it/uwsgi/wiki/Background
  37. Configuring and starting the uWSGI server for Django
  38. ----------------------------------------------------
  39. uWSGI supports multiple ways to configure the process. See uWSGI's
  40. `configuration documentation`_ and `examples`_
  41. .. _configuration documentation: http://projects.unbit.it/uwsgi/wiki/Doc
  42. .. _examples: http://projects.unbit.it/uwsgi/wiki/Example
  43. Here's an example command to start a uWSGI server::
  44. uwsgi --chdir=/path/to/your/project \
  45. --module=mysite.wsgi:application \
  46. --env DJANGO_SETTINGS_MODULE=mysite.settings \
  47. --master --pidfile=/tmp/project-master.pid \
  48. --socket=127.0.0.1:49152 \ # can also be a file
  49. --processes=5 \ # number of worker processes
  50. --uid=1000 --gid=2000 \ # if root, uwsgi can drop privileges
  51. --harakiri=20 \ # respawn processes taking more than 20 seconds
  52. --max-requests=5000 \ # respawn processes after serving 5000 requests
  53. --vacuum \ # clear environment on exit
  54. --home=/path/to/virtual/env \ # optional path to a virtualenv
  55. --daemonize=/var/log/uwsgi/yourproject.log # background the process
  56. This assumes you have a top-level project package named ``mysite``, and
  57. within it a module :file:`mysite/wsgi.py` that contains a WSGI ``application``
  58. object. This is the layout you'll have if you ran ``django-admin.py
  59. startproject mysite`` (using your own project name in place of ``mysite``) with
  60. a recent version of Django. If this file doesn't exist, you'll need to create
  61. it. See the :doc:`/howto/deployment/wsgi/index` documentation for the default
  62. contents you should put in this file and what else you can add to it.
  63. The Django-specific options here are:
  64. * ``chdir``: The path to the directory that needs to be on Python's import
  65. path -- i.e., the directory containing the ``mysite`` package.
  66. * ``module``: The WSGI module to use -- probably the ``mysite.wsgi`` module
  67. that :djadmin:`startproject` creates.
  68. * ``env``: Should probably contain at least ``DJANGO_SETTINGS_MODULE``.
  69. * ``home``: Optional path to your project virtualenv.
  70. Example ini configuration file::
  71. [uwsgi]
  72. chdir=/path/to/your/project
  73. module=mysite.wsgi:application
  74. master=True
  75. pidfile=/tmp/project-master.pid
  76. vacuum=True
  77. max-requests=5000
  78. daemonize=/var/log/uwsgi/yourproject.log
  79. Example ini configuration file usage::
  80. uwsgi --ini uwsgi.ini
  81. See the uWSGI docs on `managing the uWSGI process`_ for information on
  82. starting, stoping and reloading the uWSGI workers.
  83. .. _managing the uWSGI process: http://projects.unbit.it/uwsgi/wiki/Management