Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

172 lines · 6.0 KiB

Last changed by

Updated mod_wsgi docs to reference the sys.path hackeryArmin Ronacherbefore adoptiona9726c4

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.  Keep in mind that you will have to actually install your65application into the virtualenv as well.  Alternatively there is the66option to just patch the path in the `.wsgi` file before the import::6768    import sys69    sys.path.insert(0, '/path/to/the/application')7071Configuring Apache72------------------7374The last thing you have to do is to create an Apache configuration file75for your application.  In this example we are telling `mod_wsgi` to76execute the application under a different user for security reasons:7778.. sourcecode:: apache7980    <VirtualHost *>81        ServerName example.com8283        WSGIDaemonProcess yourapplication user=user1 group=group1 threads=584        WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi8586        <Directory /var/www/yourapplication>87            WSGIProcessGroup yourapplication88            WSGIApplicationGroup %{GLOBAL}89            Order deny,allow90            Allow from all91        </Directory>92    </VirtualHost>9394For more information consult the `mod_wsgi wiki`_.9596.. _mod_wsgi: http://code.google.com/p/modwsgi/97.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide98.. _virtual python: http://pypi.python.org/pypi/virtualenv99.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/100101Troubleshooting102---------------103104If your application does not run, follow this guide to troubleshoot:105106**Problem:** application does not run, errorlog shows SystemExit ignored107    You have a ``app.run()`` call in your application file that is not108    guarded by an ``if __name__ == '__main__':`` condition.  Either109    remove that :meth:`~flask.Flask.run` call from the file and move it110    into a separate `run.py` file or put it into such an if block.111112**Problem:** application gives permission errors113    Probably caused by your application running as the wrong user.  Make114    sure the folders the application needs access to have the proper115    privileges set and the application runs as the correct user116    (``user`` and ``group`` parameter to the `WSGIDaemonProcess`117    directive)118119**Problem:** application dies with an error on print120    Keep in mind that mod_wsgi disallows doing anything with121    :data:`sys.stdout` and :data:`sys.stderr`.  You can disable this122    protection from the config by setting the `WSGIRestrictStdout` to123    ``off``:124125    .. sourcecode:: apache126127        WSGIRestrictStdout Off128129    Alternatively you can also replace the standard out in the .wsgi file130    with a different stream::131132        import sys133        sys.stdout = sys.stderr134135**Problem:** accessing resources gives IO errors136    Your application probably is a single .py file you symlinked into137    the site-packages folder.  Please be aware that this does not work,138    instead you either have to put the folder into the pythonpath the139    file is stored in, or convert your application into a package.140141    The reason for this is that for non-installed packages, the module142    filename is used to locate the resources and for symlinks the wrong143    filename is picked up.144145Support for Automatic Reloading146-------------------------------147148To help deployment tools you can activate support for automatic149reloading.  Whenever something changes the `.wsgi` file, `mod_wsgi` will150reload all the daemon processes for us.151152For that, just add the following directive to your `Directory` section:153154.. sourcecode:: apache155156   WSGIScriptReloading On157158Working with Virtual Environments159---------------------------------160161Virtual environments have the advantage that they never install the162required dependencies system wide so you have a better control over what163is used where.  If you want to use a virtual environment with mod_wsgi164you have to modify your `.wsgi` file slightly.165166Add the following lines to the top of your `.wsgi` file::167168    activate_this = '/path/to/env/bin/activate_this.py'169    execfile(activate_this, dict(__file__=activate_this))170171This sets up the load paths according to the settings of the virtual172environment.  Keep in mind that the path has to be absolute.