Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

167 lines · 5.8 KiB

Last changed by

Not known.

History of this file

Take it

1.. _mod_wsgi-deployment:23mod_wsgi (Apache)4=================56If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.78.. admonition:: Watch Out910   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.1516.. _Apache: http://httpd.apache.org/1718Installing `mod_wsgi`19---------------------2021If 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.2425If you are using Ubuntu/Debian you can apt-get it and activate it as26follows:2728.. sourcecode:: text2930    # apt-get install libapache2-mod-wsgi3132On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by33using pkg_add:3435.. sourcecode:: text3637    # pkg_add -r mod_wsgi3839If you are using pkgsrc you can install `mod_wsgi` by compiling the40`www/ap2-wsgi` package.4142If you encounter segfaulting child processes after the first apache43reload you can safely ignore them.  Just restart the server.4445Creating 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't63want to install it system wide consider using a `virtual python`_64instance.6566Configuring Apache67------------------6869The 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: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/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 not103    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.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 user111    (``user`` and ``group`` parameter to the `WSGIDaemonProcess`112    directive)113114**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 errors131    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.135136    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-------------------------------142143To help deployment tools you can activate support for automatic144reloading.  Whenever something changes the `.wsgi` file, `mod_wsgi` will145reload all the daemon processes for us.146147For 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 what158is used where.  If you want to use a virtual environment with mod_wsgi159you have to modify your `.wsgi` file slightly.160161Add 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.