Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

186 lines · 6.4 KiB

Last changed by

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

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>9394Note: WSGIDaemonProcess isn't implemented in Windows and Apache will 95refuse to run with the above configuration. On a Windows system, eliminate those lines:9697.. sourcecode:: apache9899	<VirtualHost *>100		ServerName example.com101		WSGIScriptAlias / C:\yourdir\yourapp.wsgi102		<Directory C:\yourdir>103			Order deny,allow104			Allow from all105		</Directory>106	</VirtualHost>107108For more information consult the `mod_wsgi wiki`_.109110.. _mod_wsgi: http://code.google.com/p/modwsgi/111.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide112.. _virtual python: http://pypi.python.org/pypi/virtualenv113.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/114115Troubleshooting116---------------117118If your application does not run, follow this guide to troubleshoot:119120**Problem:** application does not run, errorlog shows SystemExit ignored121    You have a ``app.run()`` call in your application file that is not122    guarded by an ``if __name__ == '__main__':`` condition.  Either123    remove that :meth:`~flask.Flask.run` call from the file and move it124    into a separate `run.py` file or put it into such an if block.125126**Problem:** application gives permission errors127    Probably caused by your application running as the wrong user.  Make128    sure the folders the application needs access to have the proper129    privileges set and the application runs as the correct user130    (``user`` and ``group`` parameter to the `WSGIDaemonProcess`131    directive)132133**Problem:** application dies with an error on print134    Keep in mind that mod_wsgi disallows doing anything with135    :data:`sys.stdout` and :data:`sys.stderr`.  You can disable this136    protection from the config by setting the `WSGIRestrictStdout` to137    ``off``:138139    .. sourcecode:: apache140141        WSGIRestrictStdout Off142143    Alternatively you can also replace the standard out in the .wsgi file144    with a different stream::145146        import sys147        sys.stdout = sys.stderr148149**Problem:** accessing resources gives IO errors150    Your application probably is a single .py file you symlinked into151    the site-packages folder.  Please be aware that this does not work,152    instead you either have to put the folder into the pythonpath the153    file is stored in, or convert your application into a package.154155    The reason for this is that for non-installed packages, the module156    filename is used to locate the resources and for symlinks the wrong157    filename is picked up.158159Support for Automatic Reloading160-------------------------------161162To help deployment tools you can activate support for automatic163reloading.  Whenever something changes the `.wsgi` file, `mod_wsgi` will164reload all the daemon processes for us.165166For that, just add the following directive to your `Directory` section:167168.. sourcecode:: apache169170   WSGIScriptReloading On171172Working with Virtual Environments173---------------------------------174175Virtual environments have the advantage that they never install the176required dependencies system wide so you have a better control over what177is used where.  If you want to use a virtual environment with mod_wsgi178you have to modify your `.wsgi` file slightly.179180Add the following lines to the top of your `.wsgi` file::181182    activate_this = '/path/to/env/bin/activate_this.py'183    execfile(activate_this, dict(__file__=activate_this))184185This sets up the load paths according to the settings of the virtual186environment.  Keep in mind that the path has to be absolute.