Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

216 lines · 7.2 KiB

Last changed by

Merge branch '1.1.x'David Lordbefore adoption15a49e7

History of this file

Take it

1mod_wsgi (Apache)2=================34If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.56.. admonition:: Watch Out78   Please make sure in advance that any ``app.run()`` calls you might9   have in your application file are inside an ``if __name__ ==10   '__main__':`` block or moved to a separate file.  Just make sure it's11   not called because this will always start a local WSGI server which12   we do not want if we deploy that application to mod_wsgi.1314.. _Apache: https://httpd.apache.org/1516Installing `mod_wsgi`17---------------------1819If you don't have `mod_wsgi` installed yet you have to either install it20using a package manager or compile it yourself.  The mod_wsgi21`installation instructions`_ cover source installations on UNIX systems.2223If you are using Ubuntu/Debian you can apt-get it and activate it as24follows:2526.. sourcecode:: text2728    $ apt-get install libapache2-mod-wsgi-py32930If you are using a yum based distribution (Fedora, OpenSUSE, etc..) you31can install it as follows:3233.. sourcecode:: text3435    $ yum install mod_wsgi3637On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by38using pkg_add:3940.. sourcecode:: text4142    $ pkg install ap24-py37-mod_wsgi4344If you are using pkgsrc you can install `mod_wsgi` by compiling the45`www/ap2-wsgi` package.4647If you encounter segfaulting child processes after the first apache48reload you can safely ignore them.  Just restart the server.4950Creating a `.wsgi` file51-----------------------5253To run your application you need a :file:`yourapplication.wsgi` file.54This file contains the code `mod_wsgi` is executing on startup55to get the application object.  The object called `application`56in that file is then used as application.5758For most applications the following file should be sufficient::5960    from yourapplication import app as application6162If a factory function is used in a :file:`__init__.py` file, then the function should be imported::6364    from yourapplication import create_app65    application = create_app()6667If you don't have a factory function for application creation but a singleton68instance you can directly import that one as `application`.6970Store that file somewhere that you will find it again (e.g.:71:file:`/var/www/yourapplication`) and make sure that `yourapplication` and all72the libraries that are in use are on the python load path.  If you don't73want to install it system wide consider using a `virtual python`_74instance.  Keep in mind that you will have to actually install your75application into the virtualenv as well.  Alternatively there is the76option to just patch the path in the ``.wsgi`` file before the import::7778    import sys79    sys.path.insert(0, '/path/to/the/application')8081Configuring Apache82------------------8384The last thing you have to do is to create an Apache configuration file85for your application.  In this example we are telling `mod_wsgi` to86execute the application under a different user for security reasons:8788.. sourcecode:: apache8990    <VirtualHost *>91        ServerName example.com9293        WSGIDaemonProcess yourapplication user=user1 group=group1 threads=594        WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi9596        <Directory /var/www/yourapplication>97            WSGIProcessGroup yourapplication98            WSGIApplicationGroup %{GLOBAL}99            Order deny,allow100            Allow from all101        </Directory>102    </VirtualHost>103104Note: WSGIDaemonProcess isn't implemented in Windows and Apache will105refuse to run with the above configuration. On a Windows system, eliminate those lines:106107.. sourcecode:: apache108109    <VirtualHost *>110        ServerName example.com111        WSGIScriptAlias / C:\yourdir\yourapp.wsgi112        <Directory C:\yourdir>113            Order deny,allow114            Allow from all115        </Directory>116    </VirtualHost>117118Note: There have been some changes in access control configuration119for `Apache 2.4`_.120121.. _Apache 2.4: https://httpd.apache.org/docs/trunk/upgrading.html122123Most notably, the syntax for directory permissions has changed from httpd 2.2124125.. sourcecode:: apache126127    Order allow,deny128    Allow from all129130to httpd 2.4 syntax131132.. sourcecode:: apache133134    Require all granted135136137For more information consult the `mod_wsgi documentation`_.138139.. _mod_wsgi: https://github.com/GrahamDumpleton/mod_wsgi140.. _installation instructions: https://modwsgi.readthedocs.io/en/develop/installation.html141.. _virtual python: https://pypi.org/project/virtualenv/142.. _mod_wsgi documentation: https://modwsgi.readthedocs.io/en/develop/index.html143144Troubleshooting145---------------146147If your application does not run, follow this guide to troubleshoot:148149**Problem:** application does not run, errorlog shows SystemExit ignored150    You have an ``app.run()`` call in your application file that is not151    guarded by an ``if __name__ == '__main__':`` condition.  Either152    remove that :meth:`~flask.Flask.run` call from the file and move it153    into a separate :file:`run.py` file or put it into such an if block.154155**Problem:** application gives permission errors156    Probably caused by your application running as the wrong user.  Make157    sure the folders the application needs access to have the proper158    privileges set and the application runs as the correct user159    (``user`` and ``group`` parameter to the `WSGIDaemonProcess`160    directive)161162**Problem:** application dies with an error on print163    Keep in mind that mod_wsgi disallows doing anything with164    :data:`sys.stdout` and :data:`sys.stderr`.  You can disable this165    protection from the config by setting the `WSGIRestrictStdout` to166    ``off``:167168    .. sourcecode:: apache169170        WSGIRestrictStdout Off171172    Alternatively you can also replace the standard out in the .wsgi file173    with a different stream::174175        import sys176        sys.stdout = sys.stderr177178**Problem:** accessing resources gives IO errors179    Your application probably is a single .py file you symlinked into180    the site-packages folder.  Please be aware that this does not work,181    instead you either have to put the folder into the pythonpath the182    file is stored in, or convert your application into a package.183184    The reason for this is that for non-installed packages, the module185    filename is used to locate the resources and for symlinks the wrong186    filename is picked up.187188Support for Automatic Reloading189-------------------------------190191To help deployment tools you can activate support for automatic192reloading.  Whenever something changes the ``.wsgi`` file, `mod_wsgi` will193reload all the daemon processes for us.194195For that, just add the following directive to your `Directory` section:196197.. sourcecode:: apache198199   WSGIScriptReloading On200201Working with Virtual Environments202---------------------------------203204Virtual environments have the advantage that they never install the205required dependencies system wide so you have a better control over what206is used where.  If you want to use a virtual environment with mod_wsgi207you have to modify your ``.wsgi`` file slightly.208209Add the following lines to the top of your ``.wsgi`` file::210211    activate_this = '/path/to/env/bin/activate_this.py'212    with open(activate_this) as file_:213        exec(file_.read(), dict(__file__=activate_this))214215This sets up the load paths according to the settings of the virtual216environment.  Keep in mind that the path has to be absolute.