| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | .. _deploying-uwsgi:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | uWSGI
 | 
					
						
							|  |  |  | =====
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | uWSGI is a deployment option on servers like `nginx`_, `lighttpd`_, and
 | 
					
						
							| 
									
										
										
										
											2012-04-01 22:54:27 +08:00
										 |  |  | `cherokee`_; see :ref:`deploying-fastcgi` and :ref:`deploying-wsgi-standalone`
 | 
					
						
							|  |  |  | for other options.  To use your WSGI application with uWSGI protocol you will
 | 
					
						
							|  |  |  | need a uWSGI server first. uWSGI is both a protocol and an application server;
 | 
					
						
							|  |  |  | the application server can serve uWSGI, FastCGI, and HTTP protocols.
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | The most popular uWSGI server is `uwsgi`_, which we will use for this
 | 
					
						
							|  |  |  | guide.  Make sure to have it installed to follow along.
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +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 uWSGI.
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Starting your app with uwsgi
 | 
					
						
							|  |  |  | ----------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `uwsgi` is designed to operate on WSGI callables found in python modules.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Given a flask application in myapp.py, use the following command:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. sourcecode:: text
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-22 06:56:54 +08:00
										 |  |  |     $ uwsgi -s /tmp/uwsgi.sock --manage-script-name --mount /yourapplication=myapp:app
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-28 19:38:56 +08:00
										 |  |  | The ``--manage-script-name`` will move the handling of ``SCRIPT_NAME`` to uwsgi,
 | 
					
						
							|  |  |  | since its smarter about that. It is used together with the ``--mount`` directive
 | 
					
						
							|  |  |  | which will make requests to ``/yourapplication`` be directed to ``myapp:app``.
 | 
					
						
							|  |  |  | If your application is accessible at root level, you can use a single ``/``
 | 
					
						
							|  |  |  | instead of ``/yourapplication``. ``myapp`` refers to the name of the file of
 | 
					
						
							|  |  |  | your flask application (without extension) or the module which provides ``app``.
 | 
					
						
							|  |  |  | ``app`` is the callable inside of your application (usually the line reads
 | 
					
						
							|  |  |  | ``app = Flask(__name__)``.
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-22 07:05:45 +08:00
										 |  |  | If you want to deploy your flask application inside of a virtual environment,
 | 
					
						
							|  |  |  | you need to also add ``--virtualenv /path/to/virtual/environment``. You might
 | 
					
						
							|  |  |  | also need to add ``--plugin python`` or ``--plugin python3`` depending on which
 | 
					
						
							|  |  |  | python version you use for your project.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | Configuring nginx
 | 
					
						
							|  |  |  | -----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-22 06:56:54 +08:00
										 |  |  | A basic flask nginx configuration looks like this::
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     location = /yourapplication { rewrite ^ /yourapplication/; }
 | 
					
						
							|  |  |  |     location /yourapplication { try_files $uri @yourapplication; }
 | 
					
						
							|  |  |  |     location @yourapplication {
 | 
					
						
							|  |  |  |       include uwsgi_params;
 | 
					
						
							| 
									
										
										
										
											2015-08-22 06:56:54 +08:00
										 |  |  |       uwsgi_pass unix:/tmp/yourapplication.sock;
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 12:19:02 +08:00
										 |  |  | This configuration binds the application to ``/yourapplication``.  If you want
 | 
					
						
							| 
									
										
										
										
											2015-08-22 06:56:54 +08:00
										 |  |  | to have it in the URL root its a bit simpler::
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     location / { try_files $uri @yourapplication; }
 | 
					
						
							|  |  |  |     location @yourapplication {
 | 
					
						
							|  |  |  |         include uwsgi_params;
 | 
					
						
							| 
									
										
										
										
											2015-08-22 06:56:54 +08:00
										 |  |  |         uwsgi_pass unix:/tmp/yourapplication.sock;
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _nginx: http://nginx.org/
 | 
					
						
							| 
									
										
										
										
											2011-06-11 00:15:50 +08:00
										 |  |  | .. _lighttpd: http://www.lighttpd.net/
 | 
					
						
							| 
									
										
										
										
											2014-07-24 23:03:56 +08:00
										 |  |  | .. _cherokee: http://cherokee-project.com/
 | 
					
						
							| 
									
										
										
										
											2011-06-10 16:38:26 +08:00
										 |  |  | .. _uwsgi: http://projects.unbit.it/uwsgi/
 |