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 fileTake it
The walk stopped after 200 commits: lines older than 8ed051d point at it.
Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 1–5why, one step back1.. _mod_wsgi-deployment:23mod_wsgi (Apache)4=================5Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
line 6why, one step back6If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 7–9why, one step back78.. admonition:: Watch Out9Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 10–14why, one step back10 Please make sure in advance that any ``app.run()`` calls you might11 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.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 15–20why, one step back1516.. _Apache: http://httpd.apache.org/1718Installing `mod_wsgi`19---------------------20Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 21–23why, one step back21If you don't have `mod_wsgi` installed yet you have to either install it22using a package manager or compile it yourself. The mod_wsgi23`installation instructions`_ cover source installations on UNIX systems.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
line 24why, one step back24Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 25–26why, one step back25If you are using Ubuntu/Debian you can apt-get it and activate it as26follows:Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 27–31why, one step back2728.. sourcecode:: text2930 # apt-get install libapache2-mod-wsgi31Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 32–33why, one step back32On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by33using pkg_add:Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 34–41why, one step back3435.. sourcecode:: text3637 # pkg_add -r mod_wsgi3839If you are using pkgsrc you can install `mod_wsgi` by compiling the40`www/ap2-wsgi` package.41Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 42–43why, one step back42If you encounter segfaulting child processes after the first apache43reload you can safely ignore them. Just restart the server.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 44–62why, one step back4445Creating 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'tMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 63–64why, one step back63want to install it system wide consider using a `virtual python`_64instance.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 65–68why, one step back6566Configuring Apache67------------------68Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 69–71why, one step back69The last thing you have to do is to create an Apache configuration file70for your application. In this example we are telling `mod_wsgi` to71execute the application under a different user for security reasons:Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 72–102why, one step back7273.. 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/9596Troubleshooting97---------------9899If 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 notMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 103–105why, one step back103 guarded by an ``if __name__ == '__main__':`` condition. Either104 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.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 106–109why, one step back106107**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 properMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 110–112why, one step back110 privileges set and the application runs as the correct user111 (``user`` and ``group`` parameter to the `WSGIDaemonProcess`112 directive)Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 113–130why, one step back113114**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 errorsMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 131–134why, one step back131 Your application probably is a single .py file you symlinked into132 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.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 135–142why, one step back135136 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-------------------------------142Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 143–145why, one step back143To help deployment tools you can activate support for automatic144reloading. Whenever something changes the `.wsgi` file, `mod_wsgi` will145reload all the daemon processes for us.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 146–157why, one step back146147For 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 whatMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 158–159why, one step back158is used where. If you want to use a virtual environment with mod_wsgi159you have to modify your `.wsgi` file slightly.Fixed a terrible wrong part in the module documentationArmin Ronacherbefore adoption8ed051d
lines 160–167why, one step back160161Add 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.