PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/frequently-asked-questions/index.rst

https://github.com/GrahamDumpleton/mod_wsgi-docs
ReStructuredText | 327 lines | 246 code | 81 blank | 0 comment | 0 complexity | edcb34ac98c80084029b21a2ae8e8bd4 MD5 | raw file
  1. ==========================
  2. Frequently Asked Questions
  3. ==========================
  4. Note: This document is not a substitute for reading the documentation
  5. properly. If you are having problems, you are highly encouraged to at least
  6. read the following documents, which cover basic setup as well as common
  7. problems.
  8. * :doc:`../installation-guides/building-from-source-code`
  9. * :doc:`../configuration-guides/running-a-basic-application`
  10. * :doc:`../installation-issues/index`
  11. * :doc:`../configuration-issues/index`
  12. * :doc:`../application-issues/index`
  13. If you still don't find a solution, then use the
  14. `mod_wsgi user group <http://groups.google.com/group/modwsgi>`_ to post details
  15. of your problem. Although IRC forums related to Python, WSGI and the various
  16. Python web frameworks may give instant answers, more often than not they will
  17. not know enough about mod_wsgi to actually help. Thus it is suggested that
  18. the mod_wsgi user group always be used if possible.
  19. Comparison to ModPython
  20. ------------------------
  21. **Q**: Is mod_wsgi faster than mod_python?
  22. **A**: Yes, the document :doc:`performance estimates
  23. <../performance-estimates>` indicates that both embedded mode
  24. and daemon mode of mod_wsgi can maintain a greater request throughput than
  25. mod_python.
  26. The code is faster as mod_wsgi is written entirely in C code and is purpose
  27. built for hosting WSGI applications, whereas mod_python does quite a bit
  28. itself in Python code before handing a particular request off to a Python
  29. web application.
  30. Do understand though that raw performance is unlikely to be where the
  31. bottleneck will be in your web application. As a result, the performance
  32. gains in using mod_wsgi may not be significantly noticeable when running a
  33. heavy weight Python web application based on Python web frameworks such as
  34. Django, TurboGears or Pylons.
  35. **Q**: Does mod_wsgi use less memory than mod_python?
  36. **A**: Yes, mod_wsgi has a smaller memory footprint than mod_python.
  37. The memory footprint is smaller as mod_wsgi is written entirely in C code
  38. and is purpose built for hosting WSGI applications. The mod_python code in
  39. contrast is a generic programming interface for working with Apache and
  40. thus the code size, plus the data structures it holds for handling requests
  41. is greater.
  42. That mod_python loads a lot of Python code for its own purposes also serves
  43. to increase its base memory usage. There are also known issues with
  44. mod_python whereby it loads various standard Python modules that for normal
  45. use it doesn't need to. These modules are not always required by Python
  46. web applications and so cause additional unwanted memory usage.
  47. When using mod_wsgi, overall memory usage of Apache can also be cut
  48. significantly, especially when Apache is configured for prefork MPM, by
  49. using mod_wsgi daemon mode and delegating the Python web application to run
  50. in a small number of multithreaded daemon processes.
  51. Apache Process Crashes
  52. ----------------------
  53. **Q**: Why when the mod_wsgi module is initially being loaded by Apache, do
  54. the Apache server processes crash with a 'segmentation fault'?
  55. **A**: This is nearly always caused due to mod_python also being loaded by
  56. Apache at the same time as mod_wsgi and the Python installation not
  57. providing a shared library, or mod_python having originally being built
  58. against a static Python library. This is especially a problem with older
  59. Linux distributions before they started shipping with Python as a shared
  60. library.
  61. Further information on these problems can be found in various sections of:
  62. * :doc:`../installation-issues/index`.
  63. **Q**: Why when first request is made against a WSGI application does the
  64. Apache server process handling the request crash with a 'segmentation
  65. fault'?
  66. **A**: This is nearly always caused due to a shared library version conflict.
  67. That is, Apache or some Apache module is linked against a different version
  68. of a library than that which is being used by a particular Python module
  69. that the WSGI application makes use of. The most common culprits are the
  70. expat and MySQL libraries, but it can also occur with other shared
  71. libraries.
  72. Another cause of a process crash only upon the first request can be a third
  73. party C extension module for Python which has not been implemented so as to
  74. work within a secondary Python sub interpreter. The Python bindings for
  75. Subversion are a particular example, with the Python module only working
  76. correctly if the WSGI application is forced to run within the first
  77. interpreter instance created by Python.
  78. Further information on these problems can be found in various sections of:
  79. * :doc:`../application-issues/index`
  80. The problems with the expat library are also gone into in more detail in:
  81. * :doc:`../installation-issues/expat-library`.
  82. **Q**: Why am I seeing the error message 'premature end of script headers' in
  83. the Apache error logs.
  84. **A**: If using daemon mode, this is a symptom of the mod_wsgi daemon process
  85. crashing when handling a request. You would probably also see the message
  86. 'segmentation fault'. See answer for question about 'segmentation fault'
  87. above.
  88. This error message can also occur where you haven't configured Apache correctly
  89. and your WSGI script file is being executed as a CGI script instead.
  90. HTTP Error Responses
  91. --------------------
  92. **Q**: When I try to use mod_wsgi daemon mode I get the error response '503
  93. Service Temporarily Unavailable'.
  94. **A**: The standard Apache runtime directory has restricted access and the
  95. Apache child process cannot access the daemon process sockets. You will
  96. need to use the WSGISocketPrefix directive to specify an alternative
  97. location for storing of runtime files such as sockets.
  98. For further information see section 'Location Of UNIX Sockets' of:
  99. * :doc:`../configuration-issues/index`.
  100. **Q**: I am getting a HTTP 500 error response and I can't find any error in
  101. the Apache error logs.
  102. **A**: Some users of mod_wsgi 1.3/2.0 and older minor revisions, are finding
  103. that mod_wsgi error messages are going missing, or ending up in the main
  104. Apache error log file rather than a virtual host specific error log file.
  105. Specifically, this is occurring when Apache ErrorLog directive is being
  106. used inside of a VirtualHost container.
  107. It is not known exactly what operating system setup and/or Apache
  108. configuration is the trigger for this problem. To avoid the problem, use
  109. a newer version of mod_wsgi or apply the patch related to initialisation
  110. of request status described in mod_wsgi issue
  111. `#79 <http://code.google.com/p/modwsgi/issues/detail?id=79>`_.
  112. HTTP Error Log Messages
  113. -----------------------
  114. **Q**: Why do I get the error 'IOError: client connection closed' appearing
  115. in the error logs?
  116. **A**: This occurs when the HTTP client making the request closes the
  117. connection before the complete response for a request has been written.
  118. This can occur where a user force reloads a web page before it had been
  119. completely displayed. It can also occur when using benchmarking tools such
  120. as 'ab' as they will over commit on the number of requests they make when
  121. doing concurrent requests, killing off any extra requests once the required
  122. number has been reached.
  123. In general this error message can be ignored. For more information see
  124. mod_wsgi issue
  125. `#29 <http://code.google.com/p/modwsgi/issues/detail?id=29>`_.
  126. Application Reloading
  127. ---------------------
  128. **Q**: Do I have to restart Apache every time I make a change to the Python
  129. code for my WSGI application?
  130. **A**: If your WSGI application is contained totally within the WSGI script
  131. file and it is that file that you are changing, then no you don't. In this
  132. case the WSGI script file will be automatically reloaded when a change is
  133. made provided that script reloading hasn't been disabled.
  134. If the code you are changing lies outside of the WSGI script file then what
  135. you may need to do will depend on how mod_wsgi is being used.
  136. If embedded mode of mod_wsgi is being used, the only option is to restart
  137. Apache. You could set Apache configuration directive MaxRequestsPerChild
  138. to 1 to force a reload of the application on every request, but this is not
  139. recommended because it will perform as bad as or as worse as CGI and will
  140. also affect serving up of static files and other applications being hosted
  141. by the same Apache instance.
  142. If using daemon mode with a single process you can send a SIGINT signal to
  143. the daemon process using the 'kill' command, or have the application send
  144. the signal to itself when a specific URL is triggered.
  145. If using daemon mode, with any number of processes, and the process reload
  146. mechanism of mod_wsgi 2.0 has been enabled, then all you need to do is
  147. touch the WSGI script file, thereby updating its modification time, and
  148. the daemon processes will automatically shutdown and restart the next time
  149. they receive a request.
  150. Use of daemon mode and the process reload mechanism is the preferred
  151. mechanism for handling automatic reloading of code after changes.
  152. More details on how source code reloading works with mod_wsgi can be
  153. found in:
  154. * :doc:`../developer-guides/reloading-source-code`
  155. **Q**: Why do requests against my application seem to take forever, but
  156. then after a bit they all run much quicker?
  157. **A**: This is because mod_wsgi by default performs lazy loading of any
  158. application. That is, an application is only loaded the first time that a
  159. request arrives which targets that WSGI application. This means that those
  160. initial requests will incur the overhead of loading all the application code
  161. and performing any startup initialisation.
  162. This startup overhead can appear to be quite significant, especially if
  163. using Apache prefork MPM and embedded mode. This is because the
  164. startup cost is incurred for each process and with prefork MPM there are
  165. typically a lot more processes that if using worker MPM or mod_wsgi
  166. daemon mode. Thus, as many requests as there are processes will run
  167. slowly and everything will only run full speed once code has all been
  168. loaded.
  169. Note that if recycling of Apache child processes or mod_wsgi daemon
  170. processes after a set number of requests is enabled, or for embedded mode
  171. Apache decides itself to reap any of the child processes, then you can
  172. periodically see these delayed requests occurring.
  173. Some number of the benchmarks for mod_wsgi which have been posted
  174. do not take into mind these start up costs and wrongly try to compare
  175. the results to other systems such as fastcgi or proxy based systems where
  176. the application code would be preloaded by default. As a result mod_wsgi
  177. is painted in a worse light than is reality. If mod_wsgi is configured
  178. correctly the results would be better than is shown by those benchmarks.
  179. For some cases, such as when WSGIScriptAlias is being used, it is actually
  180. possible to preload the application code when the processes first starts,
  181. rather than when the first request arrives. To preload an application see the
  182. :doc:`../configuration-directives/WSGIImportScript` directive.
  183. By preloading the application code you would not normally see delays in
  184. requests being handled. The only exception to this would be when running
  185. a single process under mod_wsgi daemon mode and the process is being
  186. restarted when a maximum number of requests arrives or explicitly via one
  187. of the means to trigger reloading of application code. Delays here can be
  188. avoided by running at least two processes in the daemon process group.
  189. This is because when one process is restarting, the others can handle the
  190. requests.
  191. Execution Environment
  192. ---------------------
  193. **Q**: Why do I get the error 'IOError: sys.stdout access restricted by
  194. mod_wsgi'?
  195. **A**: A portable WSGI application or application component should not
  196. output anything to standard output. This is because some WSGI hosting
  197. mechanisms use standard output to communicate with the web server. If
  198. a WSGI application outputs anything to standard output it will thus
  199. potentially interleave with the response sent back to the client.
  200. To promote portability of WSGI applications, mod_wsgi by default restricts
  201. direct use of 'sys.stdout' and 'sys.stdin'. Because the 'print' statement
  202. defaults to outputing text to 'sys.stdout', using 'print' for debugging
  203. purposes can cause this error.
  204. For more details about this issue, including how applications should do
  205. logging and how to disable this restriction see section 'Writing To Standard
  206. Output' in:
  207. * :doc:`../application-issues/index`
  208. and section 'Apache Error Log Files' in:
  209. * :doc:`../developer-guides/debugging-techniques`
  210. **Q**: Can mod_wsgi be used with Python virtual environments created using
  211. Ian Bicking's 'virtualenv' package?
  212. **A**: Yes. For more details see:
  213. * :doc:`../developer-guides/virtual-environments`
  214. Access Control Mechanisms
  215. -------------------------
  216. **Q**: Why are client user credentials not being passed through to the WSGI
  217. application in the 'HTTP_AUTHORIZATION' variable of the WSGI environment?
  218. **A**: User credentials are not passed by default as doing so is insecure and
  219. could expose a users password to WSGI applications which shouldn't be
  220. permitted to see it. Such a situation might occur within a corporate
  221. setting where HTTP authentication mechanisms were used to control access to
  222. a corporate web server but it was possible for users to provide their own
  223. web pages. The last thing a system administator will want is normal users
  224. being able to see other users passwords.
  225. As a result, the passing of HTTP authentication credentials must be
  226. explicitly enabled by the web server administrator. This can only be done
  227. using directives placed in the main Apache confguration file.
  228. For further information see:
  229. * :doc:`../configuration-guides/access-control-mechanisms`
  230. and the documentation for the
  231. :doc:`../configuration-directives/WSGIPassAuthorization` directive.
  232. **Q**: Is there a way of having a WSGI application provide user authentication
  233. for resources outside of the application such as static files, CGI scripts
  234. or even a distinct application. In other words, something akin to being able
  235. to define access, authentication and authorisation handlers in mod_python?
  236. **A**: Providing you are using Apache 2.0 or later, version 2.0 of mod_wsgi
  237. provides support for hooking into the Apache access, authentication and
  238. authorisation handler phases. This doesn't allow full control of how the
  239. Apache handler is implemented, but does allow control over how user
  240. credentials are validated, determination of what groups a user is a member
  241. of and whether specific hosts are allowed access. This is generally more
  242. than sufficient and makes the task somewhat simpler than needing to
  243. implement a full handler like in mod_python as Apache and mod_wsgi do all
  244. the hard work.
  245. For further information see:
  246. * :doc:`../configuration-guides/access-control-mechanisms`