Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

151 lines · 5.4 KiB

Last changed by

Mention virtualenv in mod_wsgi deployment docsArmin Ronacherbefore adoptionf5d2d32

History of this file

Take it

The walk stopped after 200 commits: lines older than 5876a8f point at it.

Merge more upstream changescgrindsbefore adoption5876a8f

lines 1–5why, one step back
1mod_wsgi (Apache)
2=================34If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.5

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 6–13why, one step back
6.. admonition:: Watch Out
78 Please make sure in advance that your ``app.run()`` call you might9 have in your application file, is inside an ``if __name__ ==10 '__main__':`` or moved to a separate file. Just make sure it's not11 called because this will always start a local WSGI server which we do12 not want if we deploy that application to mod_wsgi.13

Merge more upstream changescgrindsbefore adoption5876a8f

lines 14–21why, one step back
14.. _Apache: http://httpd.apache.org/
1516Installing `mod_wsgi`17---------------------1819If you don't have `mod_wsgi` installed yet you have to either install it using20a package manager or compile it yourself.21

More typo fixes.Georg Brandlbefore adoptiona224fec

line 22why, one step back
22The mod_wsgi `installation instructions`_ cover source installations on UNIX

Merge more upstream changescgrindsbefore adoption5876a8f

lines 23–24why, one step back
23systems.
24

More typo fixes.Georg Brandlbefore adoptiona224fec

line 25why, one step back
25If you are using Ubuntu/Debian you can apt-get it and activate it as follows:

Merge more upstream changescgrindsbefore adoption5876a8f

line 26why, one step back
26

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 27–28why, one step back
27.. sourcecode:: text
28

Merge more upstream changescgrindsbefore adoption5876a8f

lines 29–31why, one step back
29 # apt-get install libapache2-mod-wsgi
3031On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

line 32why, one step back
32pkg_add:

Merge more upstream changescgrindsbefore adoption5876a8f

line 33why, one step back
33

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 34–35why, one step back
34.. sourcecode:: text
35

Merge more upstream changescgrindsbefore adoption5876a8f

lines 36–92why, one step back
36 # pkg_add -r mod_wsgi
3738If you are using pkgsrc you can install `mod_wsgi` by compiling the39`www/ap2-wsgi` package.4041If you encounter segfaulting child processes after the first apache reload you42can safely ignore them. Just restart the server.4344Creating a `.wsgi` file45-----------------------4647To run your application you need a `yourapplication.wsgi` file. This file48contains the code `mod_wsgi` is executing on startup to get the application49object. The object called `application` in that file is then used as50application.5152For most applications the following file should be sufficient::5354 from yourapplication import app as application5556If you don't have a factory function for application creation but a singleton57instance you can directly import that one as `application`.5859Store that file somewhere that you will find it again (e.g.:60`/var/www/yourapplication`) and make sure that `yourapplication` and all61the libraries that are in use are on the python load path. If you don't62want to install it system wide consider using a `virtual python`_ instance.6364Configuring Apache65------------------6667The last thing you have to do is to create an Apache configuration file for68your application. In this example we are telling `mod_wsgi` to execute the69application under a different user for security reasons:7071.. sourcecode:: apache7273 <VirtualHost *>74 ServerName example.com7576 WSGIDaemonProcess yourapplication user=user1 group=group1 threads=577 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi7879 <Directory /var/www/yourapplication>80 WSGIProcessGroup yourapplication81 WSGIApplicationGroup %{GLOBAL}82 Order deny,allow83 Allow from all84 </Directory>85 </VirtualHost>8687For more information consult the `mod_wsgi wiki`_.8889.. _mod_wsgi: http://code.google.com/p/modwsgi/90.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide91.. _virtual python: http://pypi.python.org/pypi/virtualenv92.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 93–98why, one step back
93
94Toubleshooting95--------------9697If your application does not run, follow this guide to troubleshoot:98

More typo fixes.Georg Brandlbefore adoptiona224fec

line 99why, one step back
99**Problem:** application does not run, errorlog shows SystemExit ignored

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 100–132why, one step back
100 You have a ``app.run()`` call in your application file that is not
101 guarded by an ``if __name__ == '__main__':`` condition. Either remove102 that :meth:`~flask.Flask.run` call from the file and move it into a103 separate `run.py` file or put it into such an if block.104105**Problem:** application gives permission errors106 Probably caused by your application running as the wrong user. Make107 sure the folders the application needs access to have the proper108 privileges set and the application runs as the correct user (``user``109 and ``group`` parameter to the `WSGIDaemonProcess` directive)110111**Problem:** application dies with an error on print112 Keep in mind that mod_wsgi disallows doing anything with113 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this114 protection from the config by setting the `WSGIRestrictStdout` to115 ``off``:116117 .. sourcecode:: apache118119 WSGIRestrictStdout Off120121 Alternatively you can also replace the standard out in the .wsgi file122 with a different stream::123124 import sys125 sys.stdout = sys.stderr126127**Problem:** accessing resources gives IO errors128 Your application probably is a single .py file you symlinked into the129 site-packages folder. Please be aware that this does not work,130 instead you either have to put the folder into the pythonpath the file131 is stored in, or convert your application into a package.132

More typo fixes.Georg Brandlbefore adoptiona224fec

line 133why, one step back
133 The reason for this is that for non-installed packages, the module

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 134–135why, one step back
134 filename is used to locate the resources and for symlinks the wrong
135 filename is picked up.

Mention virtualenv in mod_wsgi deployment docsArmin Ronacherbefore adoptionf5d2d32

lines 136–151why, one step back
136
137Working with Virtual Environments138---------------------------------139140Virtual environments have the advantage that they never install the141required dependencies system wide so you have a better control over what142is used where. If you want to use a virtual environment with mod_wsgi you143have to modify your `.wsgi` file slightly.144145Add the following lines to the top of your `.wsgi` file::146147 activate_this = '/path/to/env/bin/activate_this.py'148 execfile(activate_this, dict(__file__=activate_this))149150This sets up the load paths according to the settings of the virtual151environment. Keep in mind that the path has to be absolute.