uwsgi.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: https://uwsgi-docs.readthedocs.io/
  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.io/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:: console
  19. # Install current stable version.
  20. $ python -m pip install uwsgi
  21. # Or install LTS (long term support).
  22. $ python -m pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
  23. .. _installation procedures: https://uwsgi-docs.readthedocs.io/en/latest/Install.html
  24. uWSGI model
  25. -----------
  26. uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache)
  27. communicates with a `django-uwsgi` "worker" process to serve dynamic content.
  28. Configuring and starting the uWSGI server for Django
  29. ----------------------------------------------------
  30. uWSGI supports multiple ways to configure the process. See uWSGI's
  31. `configuration documentation`_.
  32. .. _configuration documentation: https://uwsgi.readthedocs.io/en/latest/Configuration.html
  33. Here's an example command to start a uWSGI server::
  34. uwsgi --chdir=/path/to/your/project \
  35. --module=mysite.wsgi:application \
  36. --env DJANGO_SETTINGS_MODULE=mysite.settings \
  37. --master --pidfile=/tmp/project-master.pid \
  38. --socket=127.0.0.1:49152 \ # can also be a file
  39. --processes=5 \ # number of worker processes
  40. --uid=1000 --gid=2000 \ # if root, uwsgi can drop privileges
  41. --harakiri=20 \ # respawn processes taking more than 20 seconds
  42. --max-requests=5000 \ # respawn processes after serving 5000 requests
  43. --vacuum \ # clear environment on exit
  44. --home=/path/to/virtual/env \ # optional path to a virtualenv
  45. --daemonize=/var/log/uwsgi/yourproject.log # background the process
  46. This assumes you have a top-level project package named ``mysite``, and
  47. within it a module :file:`mysite/wsgi.py` that contains a WSGI ``application``
  48. object. This is the layout you'll have if you ran ``django-admin
  49. startproject mysite`` (using your own project name in place of ``mysite``) with
  50. a recent version of Django. If this file doesn't exist, you'll need to create
  51. it. See the :doc:`/howto/deployment/wsgi/index` documentation for the default
  52. contents you should put in this file and what else you can add to it.
  53. The Django-specific options here are:
  54. * ``chdir``: The path to the directory that needs to be on Python's import
  55. path -- i.e., the directory containing the ``mysite`` package.
  56. * ``module``: The WSGI module to use -- probably the ``mysite.wsgi`` module
  57. that :djadmin:`startproject` creates.
  58. * ``env``: Should probably contain at least ``DJANGO_SETTINGS_MODULE``.
  59. * ``home``: Optional path to your project virtualenv.
  60. Example ini configuration file::
  61. [uwsgi]
  62. chdir=/path/to/your/project
  63. module=mysite.wsgi:application
  64. master=True
  65. pidfile=/tmp/project-master.pid
  66. vacuum=True
  67. max-requests=5000
  68. daemonize=/var/log/uwsgi/yourproject.log
  69. Example ini configuration file usage::
  70. uwsgi --ini uwsgi.ini
  71. .. admonition:: Fixing ``UnicodeEncodeError`` for file uploads
  72. If you get a ``UnicodeEncodeError`` when uploading files with file names
  73. that contain non-ASCII characters, make sure uWSGI is configured to accept
  74. non-ASCII file names by adding this to your ``uwsgi.ini``::
  75. env = LANG=en_US.UTF-8
  76. See the :ref:`unicode-files` section of the Unicode reference guide for
  77. details.
  78. See the uWSGI docs on `managing the uWSGI process`_ for information on
  79. starting, stopping and reloading the uWSGI workers.
  80. .. _managing the uWSGI process: https://uwsgi-docs.readthedocs.io/en/latest/Management.html