Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

166 lines · 5.8 KiB

Last changed by

Added chapter about fabric based deploymentsArmin Ronacherbefore adoption34fcd19

History of this file

Take it

The walk stopped after 200 commits: lines older than 1592c53 point at it.

Added chapter about fabric based deploymentsArmin Ronacherbefore adoption34fcd19

lines 1–2why, one step back
1.. _mod_wsgi-deployment:
2

Fixed a MANIFEST issueArmin Ronacherbefore adoption1592c53

lines 3–137why, one step back
3mod_wsgi (Apache)
4=================56If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.78.. admonition:: Watch Out910 Please make sure in advance that your ``app.run()`` call you might11 have in your application file, is inside an ``if __name__ ==12 '__main__':`` or moved to a separate file. Just make sure it's not13 called because this will always start a local WSGI server which we do14 not want if we deploy that application to mod_wsgi.1516.. _Apache: http://httpd.apache.org/1718Installing `mod_wsgi`19---------------------2021If you don't have `mod_wsgi` installed yet you have to either install it using22a package manager or compile it yourself.2324The mod_wsgi `installation instructions`_ cover source installations on UNIX25systems.2627If you are using Ubuntu/Debian you can apt-get it and activate it as follows:2829.. sourcecode:: text3031 # apt-get install libapache2-mod-wsgi3233On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using34pkg_add:3536.. sourcecode:: text3738 # pkg_add -r mod_wsgi3940If you are using pkgsrc you can install `mod_wsgi` by compiling the41`www/ap2-wsgi` package.4243If you encounter segfaulting child processes after the first apache reload you44can safely ignore them. Just restart the server.4546Creating a `.wsgi` file47-----------------------4849To run your application you need a `yourapplication.wsgi` file. This file50contains the code `mod_wsgi` is executing on startup to get the application51object. The object called `application` in that file is then used as52application.5354For most applications the following file should be sufficient::5556 from yourapplication import app as application5758If you don't have a factory function for application creation but a singleton59instance you can directly import that one as `application`.6061Store that file somewhere that you will find it again (e.g.:62`/var/www/yourapplication`) and make sure that `yourapplication` and all63the libraries that are in use are on the python load path. If you don't64want to install it system wide consider using a `virtual python`_ instance.6566Configuring Apache67------------------6869The last thing you have to do is to create an Apache configuration file for70your application. In this example we are telling `mod_wsgi` to execute the71application under a different user for security reasons:7273.. 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/9596Toubleshooting97--------------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 not103 guarded by an ``if __name__ == '__main__':`` condition. Either remove104 that :meth:`~flask.Flask.run` call from the file and move it into a105 separate `run.py` file or put it into such an if block.106107**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 proper110 privileges set and the application runs as the correct user (``user``111 and ``group`` parameter to the `WSGIDaemonProcess` directive)112113**Problem:** application dies with an error on print114 Keep in mind that mod_wsgi disallows doing anything with115 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this116 protection from the config by setting the `WSGIRestrictStdout` to117 ``off``:118119 .. sourcecode:: apache120121 WSGIRestrictStdout Off122123 Alternatively you can also replace the standard out in the .wsgi file124 with a different stream::125126 import sys127 sys.stdout = sys.stderr128129**Problem:** accessing resources gives IO errors130 Your application probably is a single .py file you symlinked into the131 site-packages folder. Please be aware that this does not work,132 instead you either have to put the folder into the pythonpath the file133 is stored in, or convert your application into a package.134135 The reason for this is that for non-installed packages, the module136 filename is used to locate the resources and for symlinks the wrong137 filename is picked up.

Added chapter about fabric based deploymentsArmin Ronacherbefore adoption34fcd19

lines 138–150why, one step back
138
139Support for Automatic Reloading140-------------------------------141142To help deployment tools you can activate support for automatic reloading.143Whenever something changes the `.wsgi` file, `mod_wsgi` will reload all144the daemon processes for us.145146For that, just add the following directive to your `Directory` section:147148.. sourcecode:: apache149150 WSGIScriptReloading On

Mention virtualenv in mod_wsgi deployment docsArmin Ronacherbefore adoptionf5d2d32

lines 151–166why, one step back
151
152Working with Virtual Environments153---------------------------------154155Virtual environments have the advantage that they never install the156required dependencies system wide so you have a better control over what157is used where. If you want to use a virtual environment with mod_wsgi you158have to modify your `.wsgi` file slightly.159160Add the following lines to the top of your `.wsgi` file::161162 activate_this = '/path/to/env/bin/activate_this.py'163 execfile(activate_this, dict(__file__=activate_this))164165This sets up the load paths according to the settings of the virtual166environment. Keep in mind that the path has to be absolute.