| 
									
										
										
										
											2010-07-06 22:07:13 +08:00
										 |  |  | .. _mod_wsgi-deployment:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | mod_wsgi (Apache)
 | 
					
						
							|  |  |  | =================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | .. admonition:: Watch Out
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  |    Please make sure in advance that any ``app.run()`` calls you might
 | 
					
						
							|  |  |  |    have in your application file are inside an ``if __name__ ==
 | 
					
						
							|  |  |  |    '__main__':`` block or moved to a separate file.  Just make sure it's
 | 
					
						
							|  |  |  |    not called because this will always start a local WSGI server which
 | 
					
						
							|  |  |  |    we do not want if we deploy that application to mod_wsgi.
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | .. _Apache: http://httpd.apache.org/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Installing `mod_wsgi`
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | If you don't have `mod_wsgi` installed yet you have to either install it
 | 
					
						
							|  |  |  | using a package manager or compile it yourself.  The mod_wsgi
 | 
					
						
							|  |  |  | `installation instructions`_ cover source installations on UNIX systems.
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | If you are using Ubuntu/Debian you can apt-get it and activate it as
 | 
					
						
							|  |  |  | follows:
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: text
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # apt-get install libapache2-mod-wsgi
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by
 | 
					
						
							|  |  |  | using pkg_add:
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: text
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # pkg_add -r mod_wsgi
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you are using pkgsrc you can install `mod_wsgi` by compiling the
 | 
					
						
							|  |  |  | `www/ap2-wsgi` package.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | If you encounter segfaulting child processes after the first apache
 | 
					
						
							|  |  |  | reload you can safely ignore them.  Just restart the server.
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Creating a `.wsgi` file
 | 
					
						
							|  |  |  | -----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To run your application you need a `yourapplication.wsgi` file.  This file
 | 
					
						
							|  |  |  | contains the code `mod_wsgi` is executing on startup to get the application
 | 
					
						
							|  |  |  | object.  The object called `application` in that file is then used as
 | 
					
						
							|  |  |  | application.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For most applications the following file should be sufficient::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     from yourapplication import app as application
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you don't have a factory function for application creation but a singleton
 | 
					
						
							|  |  |  | instance you can directly import that one as `application`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-20 12:25:51 +08:00
										 |  |  | Store that file somewhere that you will find it again (e.g.:
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | `/var/www/yourapplication`) and make sure that `yourapplication` and all
 | 
					
						
							|  |  |  | the libraries that are in use are on the python load path.  If you don't
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | want to install it system wide consider using a `virtual python`_
 | 
					
						
							| 
									
										
										
										
											2011-12-05 07:59:37 +08:00
										 |  |  | instance.  Keep in mind that you will have to actually install your
 | 
					
						
							|  |  |  | application into the virtualenv as well.  Alternatively there is the
 | 
					
						
							|  |  |  | option to just patch the path in the `.wsgi` file before the import::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import sys
 | 
					
						
							|  |  |  |     sys.path.insert(0, '/path/to/the/application')
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Configuring Apache
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | The last thing you have to do is to create an Apache configuration file
 | 
					
						
							|  |  |  | for your application.  In this example we are telling `mod_wsgi` to
 | 
					
						
							|  |  |  | execute the application under a different user for security reasons:
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: apache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <VirtualHost *>
 | 
					
						
							|  |  |  |         ServerName example.com
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5
 | 
					
						
							|  |  |  |         WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         <Directory /var/www/yourapplication>
 | 
					
						
							|  |  |  |             WSGIProcessGroup yourapplication
 | 
					
						
							|  |  |  |             WSGIApplicationGroup %{GLOBAL}
 | 
					
						
							|  |  |  |             Order deny,allow
 | 
					
						
							|  |  |  |             Allow from all
 | 
					
						
							|  |  |  |         </Directory>
 | 
					
						
							|  |  |  |     </VirtualHost>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-17 02:08:02 +08:00
										 |  |  | Note: WSGIDaemonProcess isn't implemented in Windows and Apache will 
 | 
					
						
							|  |  |  | refuse to run with the above configuration. On a Windows system, eliminate those lines:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: apache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	<VirtualHost *>
 | 
					
						
							|  |  |  | 		ServerName example.com
 | 
					
						
							|  |  |  | 		WSGIScriptAlias / C:\yourdir\yourapp.wsgi
 | 
					
						
							|  |  |  | 		<Directory C:\yourdir>
 | 
					
						
							|  |  |  | 			Order deny,allow
 | 
					
						
							|  |  |  | 			Allow from all
 | 
					
						
							|  |  |  | 		</Directory>
 | 
					
						
							|  |  |  | 	</VirtualHost>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 17:23:44 +08:00
										 |  |  | For more information consult the `mod_wsgi wiki`_.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _mod_wsgi: http://code.google.com/p/modwsgi/
 | 
					
						
							|  |  |  | .. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
 | 
					
						
							|  |  |  | .. _virtual python: http://pypi.python.org/pypi/virtualenv
 | 
					
						
							|  |  |  | .. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 14:05:35 +08:00
										 |  |  | Troubleshooting
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | If your application does not run, follow this guide to troubleshoot:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 02:06:12 +08:00
										 |  |  | **Problem:** application does not run, errorlog shows SystemExit ignored
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  |     You have a ``app.run()`` call in your application file that is not
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  |     guarded by an ``if __name__ == '__main__':`` condition.  Either
 | 
					
						
							|  |  |  |     remove that :meth:`~flask.Flask.run` call from the file and move it
 | 
					
						
							|  |  |  |     into a separate `run.py` file or put it into such an if block.
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | **Problem:** application gives permission errors
 | 
					
						
							|  |  |  |     Probably caused by your application running as the wrong user.  Make
 | 
					
						
							|  |  |  |     sure the folders the application needs access to have the proper
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  |     privileges set and the application runs as the correct user
 | 
					
						
							|  |  |  |     (``user`` and ``group`` parameter to the `WSGIDaemonProcess`
 | 
					
						
							|  |  |  |     directive)
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | **Problem:** application dies with an error on print
 | 
					
						
							|  |  |  |     Keep in mind that mod_wsgi disallows doing anything with
 | 
					
						
							|  |  |  |     :data:`sys.stdout` and :data:`sys.stderr`.  You can disable this
 | 
					
						
							|  |  |  |     protection from the config by setting the `WSGIRestrictStdout` to
 | 
					
						
							|  |  |  |     ``off``:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .. sourcecode:: apache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         WSGIRestrictStdout Off
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Alternatively you can also replace the standard out in the .wsgi file
 | 
					
						
							|  |  |  |     with a different stream::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         import sys
 | 
					
						
							|  |  |  |         sys.stdout = sys.stderr
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **Problem:** accessing resources gives IO errors
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  |     Your application probably is a single .py file you symlinked into
 | 
					
						
							|  |  |  |     the site-packages folder.  Please be aware that this does not work,
 | 
					
						
							|  |  |  |     instead you either have to put the folder into the pythonpath the
 | 
					
						
							|  |  |  |     file is stored in, or convert your application into a package.
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-30 02:06:12 +08:00
										 |  |  |     The reason for this is that for non-installed packages, the module
 | 
					
						
							| 
									
										
										
										
											2010-05-24 21:08:35 +08:00
										 |  |  |     filename is used to locate the resources and for symlinks the wrong
 | 
					
						
							|  |  |  |     filename is picked up.
 | 
					
						
							| 
									
										
										
										
											2010-06-30 15:49:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-06 22:07:13 +08:00
										 |  |  | Support for Automatic Reloading
 | 
					
						
							|  |  |  | -------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | To help deployment tools you can activate support for automatic
 | 
					
						
							|  |  |  | reloading.  Whenever something changes the `.wsgi` file, `mod_wsgi` will
 | 
					
						
							|  |  |  | reload all the daemon processes for us.
 | 
					
						
							| 
									
										
										
										
											2010-07-06 22:07:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | For that, just add the following directive to your `Directory` section:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: apache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    WSGIScriptReloading On
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-30 15:49:04 +08:00
										 |  |  | Working with Virtual Environments
 | 
					
						
							|  |  |  | ---------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Virtual environments have the advantage that they never install the
 | 
					
						
							|  |  |  | required dependencies system wide so you have a better control over what
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | is used where.  If you want to use a virtual environment with mod_wsgi
 | 
					
						
							|  |  |  | you have to modify your `.wsgi` file slightly.
 | 
					
						
							| 
									
										
										
										
											2010-06-30 15:49:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Add the following lines to the top of your `.wsgi` file::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     activate_this = '/path/to/env/bin/activate_this.py'
 | 
					
						
							|  |  |  |     execfile(activate_this, dict(__file__=activate_this))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This sets up the load paths according to the settings of the virtual
 | 
					
						
							|  |  |  | environment.  Keep in mind that the path has to be absolute.
 |