Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

167 lines · 5.8 KiB

Last changed by

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

History of this file

Take it

The walk stopped after 200 commits: lines older than 2912ff6 point at it.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 1–5why, one step back
1.. _mod_wsgi-deployment:
23mod_wsgi (Apache)4=================5

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

line 6why, one step back
6If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 7–9why, one step back
7
8.. admonition:: Watch Out9

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 10–14why, one step back
10 Please make sure in advance that any ``app.run()`` calls you might
11 have in your application file are inside an ``if __name__ ==12 '__main__':`` block or moved to a separate file. Just make sure it's13 not called because this will always start a local WSGI server which14 we do not want if we deploy that application to mod_wsgi.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 15–20why, one step back
15
16.. _Apache: http://httpd.apache.org/1718Installing `mod_wsgi`19---------------------20

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 21–23why, one step back
21If you don't have `mod_wsgi` installed yet you have to either install it
22using a package manager or compile it yourself. The mod_wsgi23`installation instructions`_ cover source installations on UNIX systems.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

line 24why, one step back
24

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

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

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 27–31why, one step back
27
28.. sourcecode:: text2930 # apt-get install libapache2-mod-wsgi31

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 32–33why, one step back
32On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by
33using pkg_add:

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 34–41why, one step back
34
35.. sourcecode:: text3637 # pkg_add -r mod_wsgi3839If you are using pkgsrc you can install `mod_wsgi` by compiling the40`www/ap2-wsgi` package.41

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 42–43why, one step back
42If you encounter segfaulting child processes after the first apache
43reload you can safely ignore them. Just restart the server.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 44–62why, one step back
44
45Creating a `.wsgi` file46-----------------------4748To run your application you need a `yourapplication.wsgi` file. This file49contains the code `mod_wsgi` is executing on startup to get the application50object. The object called `application` in that file is then used as51application.5253For most applications the following file should be sufficient::5455 from yourapplication import app as application5657If you don't have a factory function for application creation but a singleton58instance you can directly import that one as `application`.5960Store that file somewhere that you will find it again (e.g.:61`/var/www/yourapplication`) and make sure that `yourapplication` and all62the libraries that are in use are on the python load path. If you don't

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 63–64why, one step back
63want to install it system wide consider using a `virtual python`_
64instance.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 65–68why, one step back
65
66Configuring Apache67------------------68

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 69–71why, one step back
69The last thing you have to do is to create an Apache configuration file
70for your application. In this example we are telling `mod_wsgi` to71execute the application under a different user for security reasons:

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 72–95why, one step back
72
73.. sourcecode:: apache7475 <VirtualHost *>76 ServerName example.com7778 WSGIDaemonProcess yourapplication user=user1 group=group1 threads=579 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi8081 <Directory /var/www/yourapplication>82 WSGIProcessGroup yourapplication83 WSGIApplicationGroup %{GLOBAL}84 Order deny,allow85 Allow from all86 </Directory>87 </VirtualHost>8889For more information consult the `mod_wsgi wiki`_.9091.. _mod_wsgi: http://code.google.com/p/modwsgi/92.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide93.. _virtual python: http://pypi.python.org/pypi/virtualenv94.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/95

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption5ecf01d

lines 96–97why, one step back
96Troubleshooting
97---------------

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 98–102why, one step back
98
99If your application does not run, follow this guide to troubleshoot:100101**Problem:** application does not run, errorlog shows SystemExit ignored102 You have a ``app.run()`` call in your application file that is not

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 103–105why, one step back
103 guarded by an ``if __name__ == '__main__':`` condition. Either
104 remove that :meth:`~flask.Flask.run` call from the file and move it105 into a separate `run.py` file or put it into such an if block.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 106–109why, one step back
106
107**Problem:** application gives permission errors108 Probably caused by your application running as the wrong user. Make109 sure the folders the application needs access to have the proper

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 110–112why, one step back
110 privileges set and the application runs as the correct user
111 (``user`` and ``group`` parameter to the `WSGIDaemonProcess`112 directive)

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 113–130why, one step back
113
114**Problem:** application dies with an error on print115 Keep in mind that mod_wsgi disallows doing anything with116 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this117 protection from the config by setting the `WSGIRestrictStdout` to118 ``off``:119120 .. sourcecode:: apache121122 WSGIRestrictStdout Off123124 Alternatively you can also replace the standard out in the .wsgi file125 with a different stream::126127 import sys128 sys.stdout = sys.stderr129130**Problem:** accessing resources gives IO errors

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 131–134why, one step back
131 Your application probably is a single .py file you symlinked into
132 the site-packages folder. Please be aware that this does not work,133 instead you either have to put the folder into the pythonpath the134 file is stored in, or convert your application into a package.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 135–142why, one step back
135
136 The reason for this is that for non-installed packages, the module137 filename is used to locate the resources and for symlinks the wrong138 filename is picked up.139140Support for Automatic Reloading141-------------------------------142

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 143–145why, one step back
143To help deployment tools you can activate support for automatic
144reloading. Whenever something changes the `.wsgi` file, `mod_wsgi` will145reload all the daemon processes for us.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 146–157why, one step back
146
147For that, just add the following directive to your `Directory` section:148149.. sourcecode:: apache150151 WSGIScriptReloading On152153Working with Virtual Environments154---------------------------------155156Virtual environments have the advantage that they never install the157required dependencies system wide so you have a better control over what

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 158–159why, one step back
158is used where. If you want to use a virtual environment with mod_wsgi
159you have to modify your `.wsgi` file slightly.

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 160–167why, one step back
160
161Add the following lines to the top of your `.wsgi` file::162163 activate_this = '/path/to/env/bin/activate_this.py'164 execfile(activate_this, dict(__file__=activate_this))165166This sets up the load paths according to the settings of the virtual167environment. Keep in mind that the path has to be absolute.