Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

223 lines · 7.4 KiB

Last changed by

Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a

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: https://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-wsgi3132If you are using a yum based distribution (Fedora, OpenSUSE, etc..) you33can install it as follows:3435.. sourcecode:: text3637    $ yum install mod_wsgi3839On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by40using pkg_add:4142.. sourcecode:: text4344    $ pkg install ap22-mod_wsgi24546If you are using pkgsrc you can install `mod_wsgi` by compiling the47`www/ap2-wsgi` package.4849If you encounter segfaulting child processes after the first apache50reload you can safely ignore them.  Just restart the server.5152Creating a `.wsgi` file53-----------------------5455To run your application you need a :file:`yourapplication.wsgi` file.56This file contains the code `mod_wsgi` is executing on startup57to get the application object.  The object called `application`58in that file is then used as application.5960For most applications the following file should be sufficient::6162    from yourapplication import app as application6364If a factory function is used in a :file:`__init__.py` file, then the function should be imported::6566    from yourapplication import create_app67    application = create_app()6869If you don't have a factory function for application creation but a singleton70instance you can directly import that one as `application`.7172Store that file somewhere that you will find it again (e.g.:73:file:`/var/www/yourapplication`) and make sure that `yourapplication` and all74the libraries that are in use are on the python load path.  If you don't75want to install it system wide consider using a `virtual python`_76instance.  Keep in mind that you will have to actually install your77application into the virtualenv as well.  Alternatively there is the78option to just patch the path in the ``.wsgi`` file before the import::7980    import sys81    sys.path.insert(0, '/path/to/the/application')8283Configuring Apache84------------------8586The last thing you have to do is to create an Apache configuration file87for your application.  In this example we are telling `mod_wsgi` to88execute the application under a different user for security reasons:8990.. sourcecode:: apache9192    <VirtualHost *>93        ServerName example.com9495        WSGIDaemonProcess yourapplication user=user1 group=group1 threads=596        WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi9798        <Directory /var/www/yourapplication>99            WSGIProcessGroup yourapplication100            WSGIApplicationGroup %{GLOBAL}101            Order deny,allow102            Allow from all103        </Directory>104    </VirtualHost>105106Note: WSGIDaemonProcess isn't implemented in Windows and Apache will107refuse to run with the above configuration. On a Windows system, eliminate those lines:108109.. sourcecode:: apache110111    <VirtualHost *>112        ServerName example.com113        WSGIScriptAlias / C:\yourdir\yourapp.wsgi114        <Directory C:\yourdir>115            Order deny,allow116            Allow from all117        </Directory>118    </VirtualHost>119120Note: There have been some changes in access control configuration121for `Apache 2.4`_.122123.. _Apache 2.4: https://httpd.apache.org/docs/trunk/upgrading.html124125Most notably, the syntax for directory permissions has changed from httpd 2.2126127.. sourcecode:: apache128129    Order allow,deny130    Allow from all131132to httpd 2.4 syntax133134.. sourcecode:: apache135136    Require all granted137138139For more information consult the `mod_wsgi documentation`_.140141.. _mod_wsgi: https://github.com/GrahamDumpleton/mod_wsgi142.. _installation instructions: https://modwsgi.readthedocs.io/en/develop/installation.html143.. _virtual python: https://pypi.org/project/virtualenv/144.. _mod_wsgi documentation: https://modwsgi.readthedocs.io/en/develop/index.html145146Troubleshooting147---------------148149If your application does not run, follow this guide to troubleshoot:150151**Problem:** application does not run, errorlog shows SystemExit ignored152    You have an ``app.run()`` call in your application file that is not153    guarded by an ``if __name__ == '__main__':`` condition.  Either154    remove that :meth:`~flask.Flask.run` call from the file and move it155    into a separate :file:`run.py` file or put it into such an if block.156157**Problem:** application gives permission errors158    Probably caused by your application running as the wrong user.  Make159    sure the folders the application needs access to have the proper160    privileges set and the application runs as the correct user161    (``user`` and ``group`` parameter to the `WSGIDaemonProcess`162    directive)163164**Problem:** application dies with an error on print165    Keep in mind that mod_wsgi disallows doing anything with166    :data:`sys.stdout` and :data:`sys.stderr`.  You can disable this167    protection from the config by setting the `WSGIRestrictStdout` to168    ``off``:169170    .. sourcecode:: apache171172        WSGIRestrictStdout Off173174    Alternatively you can also replace the standard out in the .wsgi file175    with a different stream::176177        import sys178        sys.stdout = sys.stderr179180**Problem:** accessing resources gives IO errors181    Your application probably is a single .py file you symlinked into182    the site-packages folder.  Please be aware that this does not work,183    instead you either have to put the folder into the pythonpath the184    file is stored in, or convert your application into a package.185186    The reason for this is that for non-installed packages, the module187    filename is used to locate the resources and for symlinks the wrong188    filename is picked up.189190Support for Automatic Reloading191-------------------------------192193To help deployment tools you can activate support for automatic194reloading.  Whenever something changes the ``.wsgi`` file, `mod_wsgi` will195reload all the daemon processes for us.196197For that, just add the following directive to your `Directory` section:198199.. sourcecode:: apache200201   WSGIScriptReloading On202203Working with Virtual Environments204---------------------------------205206Virtual environments have the advantage that they never install the207required dependencies system wide so you have a better control over what208is used where.  If you want to use a virtual environment with mod_wsgi209you have to modify your ``.wsgi`` file slightly.210211Add the following lines to the top of your ``.wsgi`` file::212213    activate_this = '/path/to/env/bin/activate_this.py'214    execfile(activate_this, dict(__file__=activate_this))215216For Python 3 add the following lines to the top of your ``.wsgi`` file::217218    activate_this = '/path/to/env/bin/activate_this.py'219    with open(activate_this) as file_:220        exec(file_.read(), dict(__file__=activate_this))221222This sets up the load paths according to the settings of the virtual223environment.  Keep in mind that the path has to be absolute.