uwsgi.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. It also provides a fast `caching framework`_ but its documentation is not the
  8. purpose of this document.
  9. .. _uWSGI: http://projects.unbit.it/uwsgi/
  10. .. _caching framework: http://projects.unbit.it/uwsgi/wiki/CachingFramework
  11. Prerequisite: uWSGI
  12. ===================
  13. The wiki describes several `installation procedures`_. Using pip, the python
  14. package manager, installing any uWSGI version can be done with one command
  15. line. For example::
  16. # install current stable version
  17. pip install uwsgi
  18. # or install LTS (long term support)
  19. pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz
  20. .. _installation procedures: http://projects0.unbit.it/uwsgi/wiki/Install
  21. Prerequisite: general concept
  22. =============================
  23. uWSGI model
  24. -----------
  25. uWSGI operates on a client-server model. Your Web server (ie. nginx, Apache)
  26. communicates with a django-uwsgi "worker" process to serve dynamic contents.
  27. The Web server can communicate with the uWSGI process either:
  28. * directly by the uWSGI protocol through a socket created by uWSGI,
  29. * or by proxying HTTP requests to the minimalist HTTP server built in uWSGI.
  30. In the first case: the Web server can do uWSGI protocol (often with a
  31. module). It can then use either a Unix domain socket (a "named pipe" on Win32
  32. systems), or it can use a TCP socket. What you choose is a matterr of
  33. preference. Usually, a TCP socket is easier because connecting to a port
  34. doesn't require special permissions.
  35. In the second case, the Web server doesn't need to do uWSGI protocol. It just
  36. needs to be able to proxy HTTP requests to the HTTP server built-in uWSGI.
  37. The procedure is the same than proxying any HTTP server. Note that the Web
  38. server is a "reverse proxy" in this case.
  39. Configuring the uWSGI server
  40. ----------------------------
  41. In any case, when you set up your Web server, you'll just need to point its
  42. uwsgi or proxy module to the host/port or socket you specified when starting
  43. the uWSGI server.
  44. .. admonition:: Choosing the socket
  45. The easiest is to set the socket to a high level (>49152) local port like
  46. 127.0.0.1:49152. If the socket is a file, the system administrator must
  47. ensure that the Web server process has read, write and execute privileges
  48. on that file.
  49. uWSGI is highly configurable and thus there are many ways to start the
  50. process. For example, uwsgi version 0.9.6.8 provides a hundred switches.
  51. This guide demonstrates the most important of them, but does not intent to
  52. substitute the official manual and online documentation.
  53. uWSGI supports configuration through:
  54. * environment variables
  55. * command line switches
  56. * ldap
  57. * ini files
  58. * xml files
  59. * yaml files
  60. Managing the uWSGI server
  61. -------------------------
  62. The system administrator controls the worker process pool by sending signals
  63. to the master process. For example, the unix kill command sends such signals.
  64. uWSGI can write the master process id to a "pidfile". A "pidfile" is a plain
  65. text file containing just a process id.
  66. Starting the server
  67. -------------------
  68. Starting an uWSGI server is the role of the system administrator, like
  69. starting the Web server. It is *not* the role of the Web server to start the
  70. uWSGI server. This means:
  71. * the uWSGI server can be restarted or reloaded independently from the Web
  72. server,
  73. * (except with Cheerokee), it is the role of the system administrator to make
  74. uWSGI to start on boot or reboot: either through tools like supervisor or
  75. daemontools, either directly at init level in a file like /etc/rc.local or
  76. /etc/conf.d/local
  77. Managing uWSGI
  78. ==============
  79. Starting the server
  80. -------------------
  81. Example command line for a Web server that understand the uWSGI protocol::
  82. uwsgi --chdir=/path/to/your/project
  83. --module='django.core.handlers.wsgi:WSGIHandler()' \
  84. --env DJANGO_SETTINGS_MODULE=settings \
  85. --master --pidfile=/tmp/project-master.pid \
  86. --socket=127.0.0.1:49152 \ # can also be a file
  87. --processes=5 \ # number of worker processes
  88. --uid=1000 --gid=2000 \ # if root, uwsgi can drop privileges
  89. --harakiri=20 \ # respawn processes taking more than 20 seconds
  90. --limit-as=128 \ # limit the project to 128 Megabytes
  91. --max-requests=5000 \ # respawn processes after serving 5000 requests
  92. --vacuum \ # clear environment on exit
  93. --home=/path/to/virtual/env \ # optionnal path to a virtualenv
  94. --daemonize=/var/log/uwsgi/yourproject.log # background the process
  95. Django specific options are:
  96. * ``chdir``: should be the path to your project
  97. * ``module``: uwsgi module to use
  98. * ``pythonpath``: optional path to your project virtualenv
  99. * ``env``: should contain at least ``DJANGO_SETTINGS_MODULE``
  100. Example ini configuration file::
  101. [uwsgi]
  102. chdir=/path/to/your/project
  103. master=True
  104. pidfile=/tmp/project-master.pid
  105. vacuum=True
  106. max-requests=5000
  107. deamonize=/var/log/uwsgi/yourproject.log
  108. Example ini configuration file usage::
  109. uwsgi --ini uwsgi.ini
  110. Read more `uWSGI configuration examples
  111. <http://projects.unbit.it/uwsgi/wiki/Example>`_.
  112. .. admonition:: Massive application hosting
  113. `uWSGI emperor <http://projects.unbit.it/uwsgi/wiki/Emperor>`_ is a special
  114. uWSGI process that can manage many master processes at once.
  115. Reloading the daemon
  116. --------------------
  117. As mentioned above, the uWSGI master process is one of the core component of
  118. the uWSGI stack. The signal to brutally reload all the workers and the master
  119. process is SIGTERM. Example command to brutally reload the uWSGI processes::
  120. kill -TERM `cat /tmp/project-master.pid`
  121. Patching the daemon
  122. -------------------
  123. One of the great advantages of uWSGI is its ability to gradually restart each
  124. worker without loosing any request.
  125. For example, uWSGI can be signaled that worker should reload the code after
  126. handling their current request (if any) from bash::
  127. # using kill to send the signal
  128. kill -HUP `cat /tmp/project-master.pid`
  129. # if uwsgi was started with --touch-reload=/tmp/somefile
  130. touch /tmp/somefile
  131. Or from Python::
  132. uwsgi.reload()
  133. Stopping the daemon
  134. -------------------
  135. If you have the process running in the foreground, it's easy enough to stop it:
  136. Simply hitting ``Ctrl-C`` will stop and quit the uWSGI server. However, when
  137. you're dealing with background processes, you'll need to resort to the Unix
  138. ``kill`` command.
  139. The ``kill`` is used to send a signal to the uWSGI master process. The
  140. `uWSGI signals are documented online
  141. <http://projects.unbit.it/uwsgi/wiki/uWSGISignals>`_. Example command to
  142. completely stop the uWSGI stack::
  143. kill -INT `cat /tmp/project-master.pid`
  144. HTTP server configuration
  145. =========================
  146. Nginx setup
  147. -----------
  148. Nginx provides the `uwsgi module <http://wiki.nginx.org/HttpUwsgiModule>`_ by
  149. default since nginx 0.8.40. Configuring Nginx to use an uWSGI server is as
  150. simple as setting it up to proxy requests::
  151. location / {
  152. uwsgi_pass 127.0.0.1:49152;
  153. # in case of a socket file:
  154. # uwsgi_pass unix:/tmp/yourproject.sock;
  155. }
  156. Note that default uwsgi parameters should be included somewhere in your Nginx
  157. configuration. For example::
  158. http {
  159. include uwsgi_params;
  160. # [...] normal nginx configuration here
  161. }
  162. Cherokee setup
  163. --------------
  164. Cherokee setup is documented in the `official Cherokee uWSGI documentation
  165. <http://www.cherokee-project.com/doc/cookbook_uwsgi.html>`_.
  166. Lighttpd setup
  167. --------------
  168. `Lighttpd uwsgi module <http://projects.unbit.it/uwsgi/wiki/RunOnLighttpd>`_ is
  169. still experimental.
  170. Troubleshooting
  171. ===============
  172. As usual, the first things to do is to check the logs. This implies:
  173. * the web server log, which will indicate if it couldn't connect to the uWSGI
  174. process,
  175. * the uWSGI log, which will indicate if an exception was thrown.
  176. Typical gotchas:
  177. * If the socket is a file, the Web server process should have read, write and
  178. execute permissions on the socket file. The ``--chmod-socket`` option can do
  179. it.
  180. * In some cases, for instance if uWSGI was started without ``--vacuum`` or
  181. killed with ``SIGKILL``, it won't remove the socket and pidfile when it is
  182. interrupted. It is safe to remove them manually and to start uWSGI again in
  183. that case.
  184. * uWSGI can start the process on the foreground, this will make errors easily
  185. visible to the system administrator.