Skip to content
Myr

mod_wsgi.rst

This file

Path

docs/deploying/mod_wsgi.rst

217 lines · 7.2 KiB

Last changed by

resolve merge conflictsKenneth Reitzbefore adoption31174fe

History of this file

Take it

The walk stopped after 200 commits: lines older than a94e767 point at it.

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 1–15why, one step back
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.15

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 16why, one step back
16.. _Apache: https://httpd.apache.org/

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 17–54why, one step back
17
18Installing `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-----------------------54

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 55why, one step back
55To run your application you need a :file:`yourapplication.wsgi` file. This file

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 56–67why, one step back
56contains the code `mod_wsgi` is executing on startup to get the application
57object. 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 back
68:file:`/var/www/yourapplication`) and make sure that `yourapplication` and all

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 69–72why, one step back
69the libraries that are in use are on the python load path. If you don't
70want 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 the

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 73why, one step back
73option to just patch the path in the ``.wsgi`` file before the import::

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 74–116why, one step back
74
75 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>100101Note: WSGIDaemonProcess isn't implemented in Windows and Apache will102refuse 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>114115Note: There have been some changes in access control configuration for `Apache 2.4`_.116

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 117why, one step back
117.. _Apache 2.4: https://httpd.apache.org/docs/trunk/upgrading.html

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

line 118why, one step back
118

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 119why, one step back
119Most notably, the syntax for directory permissions has changed from httpd 2.2

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

line 120why, one step back
120

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 121–136why, one step back
121.. sourcecode:: apache
122123 Order allow,deny124 Allow from all125126to httpd 2.4 syntax127128.. sourcecode:: apache129130 Require all granted131132133For more information consult the `mod_wsgi documentation`_.134135.. _mod_wsgi: https://github.com/GrahamDumpleton/mod_wsgi136.. _installation instructions: https://modwsgi.readthedocs.io/en/develop/installation.html

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

line 137why, one step back
137.. _virtual python: https://pypi.python.org/pypi/virtualenv

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 138why, one step back
138.. _mod_wsgi documentation: https://modwsgi.readthedocs.io/en/develop/index.html

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 139–145why, one step back
139
140Troubleshooting141---------------142143If your application does not run, follow this guide to troubleshoot:144145**Problem:** application does not run, errorlog shows SystemExit ignored

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 146why, one step back
146 You have an ``app.run()`` call in your application file that is not

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 147–148why, one step back
147 guarded by an ``if __name__ == '__main__':`` condition. Either
148 remove that :meth:`~flask.Flask.run` call from the file and move it

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 149why, one step back
149 into a separate :file:`run.py` file or put it into such an if block.

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 150–187why, one step back
150
151**Problem:** application gives permission errors152 Probably caused by your application running as the wrong user. Make153 sure the folders the application needs access to have the proper154 privileges set and the application runs as the correct user155 (``user`` and ``group`` parameter to the `WSGIDaemonProcess`156 directive)157158**Problem:** application dies with an error on print159 Keep in mind that mod_wsgi disallows doing anything with160 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this161 protection from the config by setting the `WSGIRestrictStdout` to162 ``off``:163164 .. sourcecode:: apache165166 WSGIRestrictStdout Off167168 Alternatively you can also replace the standard out in the .wsgi file169 with a different stream::170171 import sys172 sys.stdout = sys.stderr173174**Problem:** accessing resources gives IO errors175 Your application probably is a single .py file you symlinked into176 the site-packages folder. Please be aware that this does not work,177 instead you either have to put the folder into the pythonpath the178 file is stored in, or convert your application into a package.179180 The reason for this is that for non-installed packages, the module181 filename is used to locate the resources and for symlinks the wrong182 filename is picked up.183184Support for Automatic Reloading185-------------------------------186187To help deployment tools you can activate support for automatic

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 188why, one step back
188reloading. Whenever something changes the ``.wsgi`` file, `mod_wsgi` will

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 189–202why, one step back
189reload all the daemon processes for us.
190191For that, just add the following directive to your `Directory` section:192193.. sourcecode:: apache194195 WSGIScriptReloading On196197Working with Virtual Environments198---------------------------------199200Virtual environments have the advantage that they never install the201required dependencies system wide so you have a better control over what202is used where. If you want to use a virtual environment with mod_wsgi

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 203why, one step back
203you have to modify your ``.wsgi`` file slightly.

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

line 204why, one step back
204

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 205why, one step back
205Add the following lines to the top of your ``.wsgi`` file::

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 206–208why, one step back
206
207 activate_this = '/path/to/env/bin/activate_this.py'208 execfile(activate_this, dict(__file__=activate_this))

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 209–214why, one step back
209
210For Python 3 add the following lines to the top of your ``.wsgi`` file::211212 activate_this = '/path/to/env/bin/activate_this.py'213 with open(activate_this) as file_:214 exec(file_.read(), dict(__file__=activate_this))

AppGroups produce more AppGroupsArmin Ronacherbefore adoptiona94e767

lines 215–217why, one step back
215
216This sets up the load paths according to the settings of the virtual217environment. Keep in mind that the path has to be absolute.