mod_wsgi.rst
This file
Path
docs/deploying/mod_wsgi.rst
197 lines · 6.7 KiB
Last changed by
Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
History of this fileTake it
The walk stopped after 200 commits: lines older than d4f8634 point at it.
Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 1–31why, one step back1.. _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-wsgi31Merge pull request #1090 from wxcafe/masterKenneth Reitzbefore adoptionb0f594e
lines 32–38why, one step back32If you are using a yum based distribution (Fedora, OpenSUSE, etc..) you33can install it as follows:3435.. sourcecode:: text3637 # yum install mod_wsgi38Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 39–43why, one step back39On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by40using pkg_add:4142.. sourcecode:: text43Merge pull request #1090 from wxcafe/masterKenneth Reitzbefore adoptionb0f594e
line 44why, one step back44 # pkg install ap22-mod_wsgi2Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 45–54why, one step back4546If 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-----------------------54Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 55why, one step back55To run your application you need a :file:`yourapplication.wsgi` file. This fileMerge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 56–67why, one step back56contains the code `mod_wsgi` is executing on startup to get the application57object. The object called `application` in that file is then used as58application.5960For most applications the following file should be sufficient::6162 from yourapplication import app as application6364If you don't have a factory function for application creation but a singleton65instance you can directly import that one as `application`.6667Store that file somewhere that you will find it again (e.g.:Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 68why, one step back68:file:`/var/www/yourapplication`) and make sure that `yourapplication` and allMerge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 69–72why, one step back69the libraries that are in use are on the python load path. If you don't70want to install it system wide consider using a `virtual python`_71instance. Keep in mind that you will have to actually install your72application into the virtualenv as well. Alternatively there is theMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 73why, one step back73option to just patch the path in the ``.wsgi`` file before the import::Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 74–100why, one step back7475 import sys76 sys.path.insert(0, '/path/to/the/application')7778Configuring Apache79------------------8081The last thing you have to do is to create an Apache configuration file82for your application. In this example we are telling `mod_wsgi` to83execute the application under a different user for security reasons:8485.. sourcecode:: apache8687 <VirtualHost *>88 ServerName example.com8990 WSGIDaemonProcess yourapplication user=user1 group=group1 threads=591 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi9293 <Directory /var/www/yourapplication>94 WSGIProcessGroup yourapplication95 WSGIApplicationGroup %{GLOBAL}96 Order deny,allow97 Allow from all98 </Directory>99 </VirtualHost>100Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01
line 101why, one step back101Note: WSGIDaemonProcess isn't implemented in Windows and Apache willMerge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 102–114why, one step back102refuse to run with the above configuration. On a Windows system, eliminate those lines:103104.. sourcecode:: apache105106 <VirtualHost *>107 ServerName example.com108 WSGIScriptAlias / C:\yourdir\yourapp.wsgi109 <Directory C:\yourdir>110 Order deny,allow111 Allow from all112 </Directory>113 </VirtualHost>114Merge pull request #1077 from hdformat/patch-1Armin Ronacherbefore adoption3fc10bd
lines 115–118why, one step back115Note: There have been some changes in access control configuration for `Apache 2.4`_.116117.. _Apache 2.4: http://httpd.apache.org/docs/trunk/upgrading.html118Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 119–122why, one step back119For more information consult the `mod_wsgi wiki`_.120121.. _mod_wsgi: http://code.google.com/p/modwsgi/122.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuideMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption67e4d3e
line 123why, one step back123.. _virtual python: https://pypi.python.org/pypi/virtualenvMerge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01
line 124why, one step back124.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/w/listMerge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 125–134why, one step back125126Troubleshooting127---------------128129If your application does not run, follow this guide to troubleshoot:130131**Problem:** application does not run, errorlog shows SystemExit ignored132 You have a ``app.run()`` call in your application file that is not133 guarded by an ``if __name__ == '__main__':`` condition. Either134 remove that :meth:`~flask.Flask.run` call from the file and move itMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 135why, one step back135 into a separate :file:`run.py` file or put it into such an if block.Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 136–173why, one step back136137**Problem:** application gives permission errors138 Probably caused by your application running as the wrong user. Make139 sure the folders the application needs access to have the proper140 privileges set and the application runs as the correct user141 (``user`` and ``group`` parameter to the `WSGIDaemonProcess`142 directive)143144**Problem:** application dies with an error on print145 Keep in mind that mod_wsgi disallows doing anything with146 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this147 protection from the config by setting the `WSGIRestrictStdout` to148 ``off``:149150 .. sourcecode:: apache151152 WSGIRestrictStdout Off153154 Alternatively you can also replace the standard out in the .wsgi file155 with a different stream::156157 import sys158 sys.stdout = sys.stderr159160**Problem:** accessing resources gives IO errors161 Your application probably is a single .py file you symlinked into162 the site-packages folder. Please be aware that this does not work,163 instead you either have to put the folder into the pythonpath the164 file is stored in, or convert your application into a package.165166 The reason for this is that for non-installed packages, the module167 filename is used to locate the resources and for symlinks the wrong168 filename is picked up.169170Support for Automatic Reloading171-------------------------------172173To help deployment tools you can activate support for automaticMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 174why, one step back174reloading. Whenever something changes the ``.wsgi`` file, `mod_wsgi` willMerge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 175–188why, one step back175reload all the daemon processes for us.176177For that, just add the following directive to your `Directory` section:178179.. sourcecode:: apache180181 WSGIScriptReloading On182183Working with Virtual Environments184---------------------------------185186Virtual environments have the advantage that they never install the187required dependencies system wide so you have a better control over what188is used where. If you want to use a virtual environment with mod_wsgiMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 189why, one step back189you have to modify your ``.wsgi`` file slightly.Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
line 190why, one step back190Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 191why, one step back191Add the following lines to the top of your ``.wsgi`` file::Merge pull request #899 from pengfei-xue/masterDaniel Neuhäuserbefore adoptiond4f8634
lines 192–197why, one step back192193 activate_this = '/path/to/env/bin/activate_this.py'194 execfile(activate_this, dict(__file__=activate_this))195196This sets up the load paths according to the settings of the virtual197environment. Keep in mind that the path has to be absolute.