Skip to content
Myr

Merge branch 'master' into blueprints

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parents d38c61f, 04f2bbc · Browse files · 9 files+205 added−112 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+205 added−112 removed

  • other:+205 added−112 removed

9 files+205 added−112 removed

modified:docs/deploying/cgi.rst+11 added-12 removed
1CGI2===34If all other deployment methods do not work, CGI will work for sure. CGI5is supported by all major servers but usually has a less-than-optimal4If all other deployment methods do not work, CGI will work for sure.5CGI is supported by all major servers but usually has a sub-optimal6performance.78This is also the way you can use a Flask application on Google's9`App Engine`_, there however the execution does happen in a CGI-like10environment. The application's performance is unaffected because of that.8This is also the way you can use a Flask application on Google's `App9Engine`_, where execution happens in a CGI-like environment.1011.. admonition:: Watch Out1214 Please make sure in advance that your ``app.run()`` call you might15 have in your application file, is inside an ``if __name__ ==16 '__main__':`` or moved to a separate file. Just make sure it's not17 called because this will always start a local WSGI server which we do18 not want if we deploy that application to CGI / app engine.13 Please make sure in advance that any ``app.run()`` calls you might14 have in your application file are inside an ``if __name__ ==15 '__main__':`` block or moved to a separate file. Just make sure it's16 not called because this will always start a local WSGI server which17 we do not want if we deploy that application to CGI / app engine.1820.. _App Engine: http://code.google.com/appengine/2119Creating a `.cgi` file20----------------------2122First you need to create the CGI application file. Let's call it
18 unmodified lines
4142 ScriptAlias /app /path/to/the/application.cgi4344For more information consult the documentation of your webserver.4546.. _App Engine: http://code.google.com/appengine/
modified:docs/deploying/fastcgi.rst+54 added-42 removed
1.. _deploying-fastcgi:23FastCGI4=======54A very popular deployment setup on servers like `lighttpd`_ and `nginx`_5is FastCGI. To use your WSGI application with any of them you will need6a FastCGI server first.6FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_,7and `cherokee`_; see :ref:`deploying-uwsgi` and8:ref:`deploying-other-servers` for other options. To use your WSGI9application with any of them you will need a FastCGI server first. The10most popular one is `flup`_ which we will use for this guide. Make sure11to have it installed to follow along.128The most popular one is `flup`_ which we will use for this guide. Make9sure to have it installed.1013.. admonition:: Watch Out1413 Please make sure in advance that your ``app.run()`` call you might14 have in your application file, is inside an ``if __name__ ==15 '__main__':`` or moved to a separate file. Just make sure it's not16 called because this will always start a local WSGI server which we do17 not want if we deploy that application to FastCGI.15 Please make sure in advance that any ``app.run()`` calls you might16 have in your application file are inside an ``if __name__ ==17 '__main__':`` block or moved to a separate file. Just make sure it's18 not called because this will always start a local WSGI server which19 we do not want if we deploy that application to FastCGI.2021Creating a `.fcgi` file22-----------------------23
2 unmodified lines
2627 #!/usr/bin/python28 from flup.server.fcgi import WSGIServer29 from yourapplication import app28 3031 if __name__ == '__main__':32 WSGIServer(app).run()3334This is enough for Apache to work, however nginx and older versions of33lighttpd need a socket to be explicitly passed to communicate with the FastCGI34server. For that to work you need to pass the path to the socket to the35:class:`~flup.server.fcgi.WSGIServer`::35lighttpd need a socket to be explicitly passed to communicate with the36FastCGI server. For that to work you need to pass the path to the37socket to the :class:`~flup.server.fcgi.WSGIServer`::3839 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()4041The path has to be the exact same path you define in the server
31 unmodified lines
73 url.rewrite-once = (74 "^(/static.*)$" => "$1",75 "^(/.*)$" => "/yourapplication.fcgi$1"7675Remember to enable the FastCGI, alias and rewrite modules. This configuration76binds the application to `/yourapplication`. If you want the application to77work in the URL root you have to work around a lighttpd bug with the77Remember to enable the FastCGI, alias and rewrite modules. This78configuration binds the application to `/yourapplication`. If you want79the application to work in the URL root you have to work around a80lighttpd bug with the81:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.8283Make sure to apply it only if you are mounting the application the URL81root. Also, see the Lighty docs for more information on `FastCGI and Python82<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ (note that83explicitly passing a socket to run() is no longer necessary).84root. Also, see the Lighty docs for more information on `FastCGI and85Python <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_86(note that explicitly passing a socket to run() is no longer necessary).878889Configuring nginx90-----------------9189Installing FastCGI applications on nginx is a bit different because by default90no FastCGI parameters are forwarded.92Installing FastCGI applications on nginx is a bit different because by93default no FastCGI parameters are forwarded.9495A basic flask FastCGI configuration for nginx looks like this::9697 location = /yourapplication { rewrite ^ /yourapplication/ last; }
5 unmodified lines
103 fastcgi_param SCRIPT_NAME $fastcgi_script_name;104 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;105 }106104This configuration binds the application to `/yourapplication`. If you want105to have it in the URL root it's a bit simpler because you don't have to figure106out how to calculate `PATH_INFO` and `SCRIPT_NAME`::107This configuration binds the application to `/yourapplication`. If you108want to have it in the URL root it's a bit simpler because you don't109have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::110111 location / { try_files $uri @yourapplication; }112 location @yourapplication {113 include fastcgi_params;
1 unmodified line
115 fastcgi_param SCRIPT_NAME "";116 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;117 }118116Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You117can either write an `init.d` script for that or execute it inside a screen118session::119Running FastCGI Processes120-------------------------121122Since Nginx and others do not load FastCGI apps, you have to do it by123yourself. `Supervisor can manage FastCGI processes.124<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_125You can look around for other FastCGI process managers or write a script126to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.127For a temporary solution, you can always run the ``.fcgi`` script inside128GNU screen. See ``man screen`` for details, and note that this is a129manual solution which does not persist across system restart::130131 $ screen132 $ /var/www/yourapplication/yourapplication.fcgi133134Debugging135---------136126FastCGI deployments tend to be hard to debug on most webservers. Very often the127only thing the server log tells you is something along the lines of "premature128end of headers". In order to debug the application the only thing that can129really give you ideas why it breaks is switching to the correct user and130executing the application by hand.137FastCGI deployments tend to be hard to debug on most webservers. Very138often the only thing the server log tells you is something along the139lines of "premature end of headers". In order to debug the application140the only thing that can really give you ideas why it breaks is switching141to the correct user and executing the application by hand.142132This example assumes your application is called `application.fcgi` and that your133webserver user is `www-data`::143This example assumes your application is called `application.fcgi` and144that your webserver user is `www-data`::145146 $ su www-data147 $ cd /var/www/yourapplication148 $ python application.fcgi149 Traceback (most recent call last):150 File "yourapplication.fcgi", line 4, in <module>151 ImportError: No module named yourapplication152142In this case the error seems to be "yourapplication" not being on the python143path. Common problems are:153In this case the error seems to be "yourapplication" not being on the154python path. Common problems are:155145- relative paths being used. Don't rely on the current working directory146- the code depending on environment variables that are not set by the156- Relative paths being used. Don't rely on the current working directory157- The code depending on environment variables that are not set by the158 web server.148- different python interpreters being used.159- Different python interpreters being used.160161.. _nginx: http://nginx.org/162.. _lighttpd: http://www.lighttpd.net/151.. _nginx: http://nginx.net/163.. _cherokee: http://www.cherokee-project.com/164.. _flup: http://trac.saddi.com/flup
modified:docs/deploying/index.rst+9 added-7 removed
1Deployment Options2==================34Depending on what you have available there are multiple ways to run Flask5applications. A very common method is to use the builtin server during6development and maybe behind a proxy for simple applications, but there7are more options available.4Depending on what you have available there are multiple ways to run5Flask applications. You can use the builtin server during development,6but you should use a full deployment option for production applications.7(Do not use the builtin development server in production.) Several8options are available and documented here.99If you have a different WSGI server look up the server documentation about10how to use a WSGI app with it. Just remember that your application object11is the actual WSGI application.10If you have a different WSGI server look up the server documentation11about how to use a WSGI app with it. Just remember that your12:class:`Flask` application object is the actual WSGI application.1314.. toctree::15 :maxdepth: 21617 mod_wsgi18 cgi19 fastcgi20 uwsgi21 others
modified:docs/deploying/mod_wsgi.rst+35 added-34 removed
1 unmodified line
23mod_wsgi (Apache)4=================56If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.6If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.78.. admonition:: Watch Out910 Please make sure in advance that your ``app.run()`` call you might11 have in your application file, is inside an ``if __name__ ==12 '__main__':`` or moved to a separate file. Just make sure it's not13 called because this will always start a local WSGI server which we do14 not want if we deploy that application to mod_wsgi.10 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 it using22a package manager or compile it yourself.21If 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.2424The mod_wsgi `installation instructions`_ cover source installations on UNIX25systems.25If you are using Ubuntu/Debian you can apt-get it and activate it as26follows:2727If you are using Ubuntu/Debian you can apt-get it and activate it as follows:2828.. sourcecode:: text2930 # apt-get install libapache2-mod-wsgi3133On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using34pkg_add:32On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by33using pkg_add:3435.. sourcecode:: text3637 # pkg_add -r mod_wsgi3839If you are using pkgsrc you can install `mod_wsgi` by compiling the40`www/ap2-wsgi` package.4143If you encounter segfaulting child processes after the first apache reload you44can safely ignore them. Just restart the server.42If you encounter segfaulting child processes after the first apache43reload you can safely ignore them. Just restart the server.4445Creating a `.wsgi` file46-----------------------47
11 unmodified lines
5960Store that file somewhere that you will find it again (e.g.:61`/var/www/yourapplication`) and make sure that `yourapplication` and all62the libraries that are in use are on the python load path. If you don't64want to install it system wide consider using a `virtual python`_ instance.63want to install it system wide consider using a `virtual python`_64instance.6566Configuring Apache67------------------6869The last thing you have to do is to create an Apache configuration file for70your application. In this example we are telling `mod_wsgi` to execute the71application under a different user for security reasons:69The last thing you have to do is to create an Apache configuration file70for your application. In this example we are telling `mod_wsgi` to71execute the application under a different user for security reasons:7273.. sourcecode:: apache7475 <VirtualHost *>
23 unmodified lines
99If your application does not run, follow this guide to troubleshoot:100101**Problem:** application does not run, errorlog shows SystemExit ignored102 You have a ``app.run()`` call in your application file that is not103 guarded by an ``if __name__ == '__main__':`` condition. Either remove104 that :meth:`~flask.Flask.run` call from the file and move it into a105 separate `run.py` file or put it into such an if block.103 guarded by an ``if __name__ == '__main__':`` condition. Either104 remove that :meth:`~flask.Flask.run` call from the file and move it105 into a separate `run.py` file or put it into such an if block.106107**Problem:** application gives permission errors108 Probably caused by your application running as the wrong user. Make109 sure the folders the application needs access to have the proper110 privileges set and the application runs as the correct user (``user``111 and ``group`` parameter to the `WSGIDaemonProcess` directive)110 privileges set and the application runs as the correct user111 (``user`` and ``group`` parameter to the `WSGIDaemonProcess`112 directive)113114**Problem:** application dies with an error on print115 Keep in mind that mod_wsgi disallows doing anything with116 :data:`sys.stdout` and :data:`sys.stderr`. You can disable this
10 unmodified lines
127 import sys128 sys.stdout = sys.stderr129130**Problem:** accessing resources gives IO errors130 Your application probably is a single .py file you symlinked into the131 site-packages folder. Please be aware that this does not work,132 instead you either have to put the folder into the pythonpath the file133 is stored in, or convert your application into a package.131 Your application probably is a single .py file you symlinked into132 the site-packages folder. Please be aware that this does not work,133 instead you either have to put the folder into the pythonpath the134 file is stored in, or convert your application into a package.135136 The reason for this is that for non-installed packages, the module137 filename is used to locate the resources and for symlinks the wrong138 filename is picked up.139140Support for Automatic Reloading141-------------------------------142142To help deployment tools you can activate support for automatic reloading.143Whenever something changes the `.wsgi` file, `mod_wsgi` will reload all144the daemon processes for us.143To help deployment tools you can activate support for automatic144reloading. Whenever something changes the `.wsgi` file, `mod_wsgi` will145reload all the daemon processes for us.146147For that, just add the following directive to your `Directory` section:148149.. sourcecode:: apache
4 unmodified lines
154---------------------------------155156Virtual environments have the advantage that they never install the157required dependencies system wide so you have a better control over what157is used where. If you want to use a virtual environment with mod_wsgi you158have to modify your `.wsgi` file slightly.158is used where. If you want to use a virtual environment with mod_wsgi159you have to modify your `.wsgi` file slightly.160161Add the following lines to the top of your `.wsgi` file::162163 activate_this = '/path/to/env/bin/activate_this.py'
4 unmodified lines
modified:docs/deploying/others.rst+19 added-15 removed
1.. _deploying-other-servers:23Other Servers4=============54There are popular servers written in Python that allow the execution of5WSGI applications as well. Keep in mind though that some of these servers6were written for very specific applications and might not work as well for7standard WSGI application such as Flask powered ones.6There are popular servers written in Python that allow the execution of WSGI7applications as well. These servers stand alone when they run; you can proxy8to them from your web server.9910Tornado11--------1213`Tornado`_ is an open source version of the scalable, non-blocking web14server and tools that power `FriendFeed`_. Because it is non-blocking and15uses epoll, it can handle thousands of simultaneous standing connections,16which means it is ideal for real-time web services. Integrating this17service with Flask is a trivial task::18 1819 from tornado.wsgi import WSGIContainer20 from tornado.httpserver import HTTPServer21 from tornado.ioloop import IOLoop22 from yourapplication import app23 2324 http_server = HTTPServer(WSGIContainer(app))25 http_server.listen(5000)26 IOLoop.instance().start()272829.. _Tornado: http://www.tornadoweb.org/30.. _FriendFeed: http://friendfeed.com/313232Gevent33-------3435`Gevent`_ is a coroutine-based Python networking library that uses
9 unmodified lines
45.. _Gevent: http://www.gevent.org/46.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html47.. _libevent: http://monkey.org/~provos/libevent/485049Gunicorn50--------5152`Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork
1 unmodified line
54and `greenlet`_. Running a Flask application on this server is quite simple::5556 gunicorn myproject:app5758`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.59For example, to run a Flask application with 4 worker processes (``-w604``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::6162 gunicorn -w 4 -b 127.0.0.1:4000 myproject:app6364.. _Gunicorn: http://gunicorn.org/65.. _eventlet: http://eventlet.net/66.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html676468Proxy Setups69------------7068If you deploy your application behind an HTTP proxy you will need to69rewrite a few headers in order for the application to work. The two70problematic values in the WSGI environment usually are `REMOTE_ADDR` and71`HTTP_HOST`. Werkzeug ships a fixer that will solve some common setups,72but you might want to write your own WSGI middleware for specific setups.71If you deploy your application using one of these servers behind an HTTP72proxy you will need to rewrite a few headers in order for the73application to work. The two problematic values in the WSGI environment74usually are `REMOTE_ADDR` and `HTTP_HOST`. Werkzeug ships a fixer that75will solve some common setups, but you might want to write your own WSGI76middleware for specific setups.7778The most common setup invokes the host being set from `X-Forwarded-Host`79and the remote address from `X-Forwarded-For`::80
22 unmodified lines
added:docs/deploying/uwsgi.rst+68 added
1.. _deploying-uwsgi:23uWSGI4=====56uWSGI is a deployment option on servers like `nginx`_, `lighttpd`_, and7`cherokee`_; see :ref:`deploying-fastcgi` and8:ref:`deploying-other-servers` for other options. To use your WSGI9application with uWSGI protocol you will need a uWSGI server10first. uWSGI is both a protocol and an application server; the11application server can serve uWSGI, FastCGI, and HTTP protocols.1213The most popular uWSGI server is `uwsgi`_, which we will use for this14guide. Make sure to have it installed to follow along.1516.. admonition:: Watch Out1718 Please make sure in advance that any ``app.run()`` calls you might19 have in your application file are inside an ``if __name__ ==20 '__main__':`` block or moved to a separate file. Just make sure it's21 not called because this will always start a local WSGI server which22 we do not want if we deploy that application to uWSGI.2324Starting your app with uwsgi25----------------------------2627`uwsgi` is designed to operate on WSGI callables found in python modules.2829Given a flask application in myapp.py, use the following command:3031.. sourcecode:: text3233 $ uwsgi -s /tmp/uwsgi.sock --module myapp --callable app3435Or, if you prefer:3637.. sourcecode:: text3839 $ uwsgi -s /tmp/uwsgi.sock myapp:app4041Configuring nginx42-----------------4344A basic flask uWSGI configuration for nginx looks like this::4546 location = /yourapplication { rewrite ^ /yourapplication/; }47 location /yourapplication { try_files $uri @yourapplication; }48 location @yourapplication {49 include uwsgi_params;50 uwsgi_param SCRIPT_NAME /yourapplication;51 uwsgi_modifier1 30;52 uwsgi_pass unix:/tmp/uwsgi.sock;53 }5455This configuration binds the application to `/yourapplication`. If you want56to have it in the URL root it's a bit simpler because you don't have to tell57it the WSGI `SCRIPT_NAME` or set the uwsgi modifier to make use of it::5859 location / { try_files $uri @yourapplication; }60 location @yourapplication {61 include uwsgi_params;62 uwsgi_pass unix:/tmp/uwsgi.sock;63 }6465.. _nginx: http://nginx.org/66.. _lighttpd: http://www.lighttpd.net/67.. _cherokee: http://www.cherokee-project.com/68.. _uwsgi: http://projects.unbit.it/uwsgi/
modified:docs/patterns/appfactories.rst+1 added-1 removed
37 unmodified lines
38 return app3940The downside is that you cannot use the application object in the blueprints41at import time. You can however use it from within a request. How do you42get access the application with the config? Use42get access to the application with the config? Use43:data:`~flask.current_app`::4445 from flask import current_app, Blueprint, render_template46 admin = Blueprint('admin', __name__, url_prefix='/admin')
29 unmodified lines
modified:docs/quickstart.rst+1 added-1 removed
9 unmodified lines
1011A Minimal Application12---------------------1314A minimal Flask application looks something like that::14A minimal Flask application looks something like this::1516 from flask import Flask17 app = Flask(__name__)18
731 unmodified lines
modified:docs/security.rst+7 added
93 unmodified lines
9495JSON Security96-------------9798.. admonition:: ECMAScript 5 Changes99100 Starting with ECMAScript 5 the behavior of literals changed. Now they101 are not constructed with the constructor of ``Array`` and others, but102 with the builtin constructor of ``Array`` which closes this particular103 attack vector.104105JSON itself is a high-level serialization format, so there is barely106anything that could cause security problems, right? You can't declare107recursive structures that could cause problems and the only thing that108could possibly break are very large responses that can cause some kind of
67 unmodified lines