uwsgi.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ============================
  2. How to use Django with uWSGI
  3. ============================
  4. uWSGI_ is a fast, self-healing and developer/sysadmin-friendly application
  5. container server coded in pure C.
  6. .. _uWSGI: https://uwsgi-docs.readthedocs.io/
  7. .. seealso::
  8. The uWSGI docs offer a `tutorial`_ covering Django, nginx, and uWSGI (one
  9. possible deployment setup of many). The docs below are focused on how to
  10. integrate Django with uWSGI.
  11. .. _tutorial: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
  12. Prerequisite: uWSGI
  13. ===================
  14. The uWSGI wiki describes several `installation procedures`_. Using pip, the
  15. Python package manager, you can install any uWSGI version with a single
  16. command. For example:
  17. .. code-block:: console
  18. # Install current stable version.
  19. $ python -m pip install uwsgi
  20. # Or install LTS (long term support).
  21. $ python -m pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
  22. .. _installation procedures: https://uwsgi-docs.readthedocs.io/en/latest/Install.html
  23. uWSGI model
  24. -----------
  25. uWSGI operates on a client-server model. Your web server (e.g., nginx, Apache)
  26. communicates with a ``django-uwsgi`` "worker" process to serve dynamic content.
  27. Configuring and starting the uWSGI server for Django
  28. ----------------------------------------------------
  29. uWSGI supports multiple ways to configure the process. See uWSGI's
  30. `configuration documentation`_.
  31. .. _configuration documentation: https://uwsgi.readthedocs.io/en/latest/Configuration.html
  32. Here's an example command to start a uWSGI server:
  33. .. code-block:: shell
  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 virtual environment
  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 :envvar:`DJANGO_SETTINGS_MODULE`.
  59. * ``home``: Optional path to your project virtual environment.
  60. Example ini configuration file:
  61. .. code-block:: ini
  62. [uwsgi]
  63. chdir=/path/to/your/project
  64. module=mysite.wsgi:application
  65. master=True
  66. pidfile=/tmp/project-master.pid
  67. vacuum=True
  68. max-requests=5000
  69. daemonize=/var/log/uwsgi/yourproject.log
  70. Example ini configuration file usage:
  71. .. code-block:: shell
  72. uwsgi --ini uwsgi.ini
  73. .. admonition:: Fixing ``UnicodeEncodeError`` for file uploads
  74. If you get a ``UnicodeEncodeError`` when uploading files with file names
  75. that contain non-ASCII characters, make sure uWSGI is configured to accept
  76. non-ASCII file names by adding this to your ``uwsgi.ini``:
  77. .. code-block:: ini
  78. env = LANG=en_US.UTF-8
  79. See the :ref:`unicode-files` section of the Unicode reference guide for
  80. details.
  81. See the uWSGI docs on `managing the uWSGI process`_ for information on
  82. starting, stopping and reloading the uWSGI workers.
  83. .. _managing the uWSGI process: https://uwsgi-docs.readthedocs.io/en/latest/Management.html