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
Provenance
- Commit
3add9c5- Tree
9f7ea0e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
d38c61f- Parent 2
04f2bbc
9 files changed+205 added−112 removed
modified:docs/deploying/cgi.rst+11 added-12 removed
1 CGI2 ===3 4-If all other deployment methods do not work, CGI will work for sure. CGI5-is supported by all major servers but usually has a less-than-optimal4+If all other deployment methods do not work, CGI will work for sure.5+CGI is supported by all major servers but usually has a sub-optimal6 performance.7 8-This is also the way you can use a Flask application on Google's9-`App Engine`_, there however the execution does happen in a CGI-like10-environment. The application's performance is unaffected because of that.8+This is also the way you can use a Flask application on Google's `App9+Engine`_, where execution happens in a CGI-like environment.10 11 .. admonition:: Watch Out12 14- 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.18 20-.. _App Engine: http://code.google.com/appengine/21-19 Creating a `.cgi` file20 ----------------------21 22 First you need to create the CGI application file. Let's call it18 unmodified lines41 42 ScriptAlias /app /path/to/the/application.cgi43 44 For more information consult the documentation of your webserver.45+46+.. _App Engine: http://code.google.com/appengine/modified:docs/deploying/fastcgi.rst+54 added-42 removed
1+.. _deploying-fastcgi:2+3 FastCGI4 =======5 4-A very popular deployment setup on servers like `lighttpd`_ and `nginx`_5-is FastCGI. To use your WSGI application with any of them you will need6-a FastCGI server first.6+FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_,7+and `cherokee`_; see :ref:`deploying-uwsgi` and8+:ref:`deploying-other-servers` for other options. To use your WSGI9+application with any of them you will need a FastCGI server first. The10+most popular one is `flup`_ which we will use for this guide. Make sure11+to have it installed to follow along.12 8-The most popular one is `flup`_ which we will use for this guide. Make9-sure to have it installed.10-13 .. admonition:: Watch Out14 13- 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.20 21 Creating a `.fcgi` file22 -----------------------23 2 unmodified lines26 27 #!/usr/bin/python28 from flup.server.fcgi import WSGIServer29 from yourapplication import app28- 30+31 if __name__ == '__main__':32 WSGIServer(app).run()33 34 This is enough for Apache to work, however nginx and older versions of33-lighttpd need a socket to be explicitly passed to communicate with the FastCGI34-server. For that to work you need to pass the path to the socket to the35-:class:`~flup.server.fcgi.WSGIServer`::35+lighttpd need a socket to be explicitly passed to communicate with the36+FastCGI server. For that to work you need to pass the path to the37+socket to the :class:`~flup.server.fcgi.WSGIServer`::38 39 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()40 41 The path has to be the exact same path you define in the server31 unmodified lines73 url.rewrite-once = (74 "^(/static.*)$" => "$1",75 "^(/.*)$" => "/yourapplication.fcgi$1"76 75-Remember to enable the FastCGI, alias and rewrite modules. This configuration76-binds the application to `/yourapplication`. If you want the application to77-work in the URL root you have to work around a lighttpd bug with the77+Remember to enable the FastCGI, alias and rewrite modules. This78+configuration binds the application to `/yourapplication`. If you want79+the application to work in the URL root you have to work around a80+lighttpd bug with the81 :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.82 83 Make sure to apply it only if you are mounting the application the URL81-root. Also, see the Lighty docs for more information on `FastCGI and Python82-<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ (note that83-explicitly passing a socket to run() is no longer necessary).84+root. Also, see the Lighty docs for more information on `FastCGI and85+Python <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_86+(note that explicitly passing a socket to run() is no longer necessary).87 88 89 Configuring nginx90 -----------------91 89-Installing FastCGI applications on nginx is a bit different because by default90-no FastCGI parameters are forwarded.92+Installing FastCGI applications on nginx is a bit different because by93+default no FastCGI parameters are forwarded.94 95 A basic flask FastCGI configuration for nginx looks like this::96 97 location = /yourapplication { rewrite ^ /yourapplication/ last; }5 unmodified lines103 fastcgi_param SCRIPT_NAME $fastcgi_script_name;104 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;105 }106 104-This configuration binds the application to `/yourapplication`. If you want105-to have it in the URL root it's a bit simpler because you don't have to figure106-out how to calculate `PATH_INFO` and `SCRIPT_NAME`::107+This configuration binds the application to `/yourapplication`. If you108+want to have it in the URL root it's a bit simpler because you don't109+have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::110 111 location / { try_files $uri @yourapplication; }112 location @yourapplication {113 include fastcgi_params;1 unmodified line115 fastcgi_param SCRIPT_NAME "";116 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;117 }118 116-Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You117-can either write an `init.d` script for that or execute it inside a screen118-session::119+Running FastCGI Processes120+-------------------------121 122+Since Nginx and others do not load FastCGI apps, you have to do it by123+yourself. `Supervisor can manage FastCGI processes.124+<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_125+You can look around for other FastCGI process managers or write a script126+to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.127+For a temporary solution, you can always run the ``.fcgi`` script inside128+GNU screen. See ``man screen`` for details, and note that this is a129+manual solution which does not persist across system restart::130+131 $ screen132 $ /var/www/yourapplication/yourapplication.fcgi133 134 Debugging135 ---------136 126-FastCGI deployments tend to be hard to debug on most webservers. Very often the127-only thing the server log tells you is something along the lines of "premature128-end of headers". In order to debug the application the only thing that can129-really give you ideas why it breaks is switching to the correct user and130-executing the application by hand.137+FastCGI deployments tend to be hard to debug on most webservers. Very138+often the only thing the server log tells you is something along the139+lines of "premature end of headers". In order to debug the application140+the only thing that can really give you ideas why it breaks is switching141+to the correct user and executing the application by hand.142 132-This example assumes your application is called `application.fcgi` and that your133-webserver user is `www-data`::143+This example assumes your application is called `application.fcgi` and144+that your webserver user is `www-data`::145 146 $ 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 yourapplication152 142-In this case the error seems to be "yourapplication" not being on the python143-path. Common problems are:153+In this case the error seems to be "yourapplication" not being on the154+python path. Common problems are:155 145-- 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.160 161+.. _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/flupmodified:docs/deploying/index.rst+9 added-7 removed
1 Deployment Options2 ==================3 4-Depending on what you have available there are multiple ways to run Flask5-applications. A very common method is to use the builtin server during6-development and maybe behind a proxy for simple applications, but there7-are more options available.4+Depending on what you have available there are multiple ways to run5+Flask applications. You can use the builtin server during development,6+but you should use a full deployment option for production applications.7+(Do not use the builtin development server in production.) Several8+options are available and documented here.9 9-If you have a different WSGI server look up the server documentation about10-how to use a WSGI app with it. Just remember that your application object11-is the actual WSGI application.10+If you have a different WSGI server look up the server documentation11+about how to use a WSGI app with it. Just remember that your12+:class:`Flask` application object is the actual WSGI application.13 14 .. toctree::15 :maxdepth: 216 17 mod_wsgi18 cgi19 fastcgi20+ uwsgi21 othersmodified:docs/deploying/mod_wsgi.rst+35 added-34 removed
1 unmodified line2 3 mod_wsgi (Apache)4 =================5 6-If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.6+If you are using the `Apache`_ webserver, consider using `mod_wsgi`_.7 8 .. admonition:: Watch Out9 10- 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.15 16 .. _Apache: http://httpd.apache.org/17 18 Installing `mod_wsgi`19 ---------------------20 21-If you don't have `mod_wsgi` installed yet you have to either install it using22-a package manager or compile it yourself.21+If you don't have `mod_wsgi` installed yet you have to either install it22+using a package manager or compile it yourself. The mod_wsgi23+`installation instructions`_ cover source installations on UNIX systems.24 24-The mod_wsgi `installation instructions`_ cover source installations on UNIX25-systems.25+If you are using Ubuntu/Debian you can apt-get it and activate it as26+follows:27 27-If you are using Ubuntu/Debian you can apt-get it and activate it as follows:28-28 .. sourcecode:: text29 30 # apt-get install libapache2-mod-wsgi31 33-On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using34-pkg_add:32+On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by33+using pkg_add:34 35 .. sourcecode:: text36 37 # pkg_add -r mod_wsgi38 39 If you are using pkgsrc you can install `mod_wsgi` by compiling the40 `www/ap2-wsgi` package.41 43-If you encounter segfaulting child processes after the first apache reload you44-can safely ignore them. Just restart the server.42+If you encounter segfaulting child processes after the first apache43+reload you can safely ignore them. Just restart the server.44 45 Creating a `.wsgi` file46 -----------------------47 11 unmodified lines59 60 Store that file somewhere that you will find it again (e.g.:61 `/var/www/yourapplication`) and make sure that `yourapplication` and all62 the libraries that are in use are on the python load path. If you don't64-want to install it system wide consider using a `virtual python`_ instance.63+want to install it system wide consider using a `virtual python`_64+instance.65 66 Configuring Apache67 ------------------68 69-The last thing you have to do is to create an Apache configuration file for70-your application. In this example we are telling `mod_wsgi` to execute the71-application under a different user for security reasons:69+The last thing you have to do is to create an Apache configuration file70+for your application. In this example we are telling `mod_wsgi` to71+execute the application under a different user for security reasons:72 73 .. sourcecode:: apache74 75 <VirtualHost *>23 unmodified lines99 If your application does not run, follow this guide to troubleshoot:100 101 **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.106 107 **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)113 114 **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 this10 unmodified lines127 import sys128 sys.stdout = sys.stderr129 130 **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.135 136 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.139 140 Support for Automatic Reloading141 -------------------------------142 142-To help deployment tools you can activate support for automatic reloading.143-Whenever something changes the `.wsgi` file, `mod_wsgi` will reload all144-the daemon processes for us.143+To help deployment tools you can activate support for automatic144+reloading. Whenever something changes the `.wsgi` file, `mod_wsgi` will145+reload all the daemon processes for us.146 147 For that, just add the following directive to your `Directory` section:148 149 .. sourcecode:: apache4 unmodified lines154 ---------------------------------155 156 Virtual environments have the advantage that they never install the157 required dependencies system wide so you have a better control over what157-is used where. If you want to use a virtual environment with mod_wsgi you158-have to modify your `.wsgi` file slightly.158+is used where. If you want to use a virtual environment with mod_wsgi159+you have to modify your `.wsgi` file slightly.160 161 Add the following lines to the top of your `.wsgi` file::162 163 activate_this = '/path/to/env/bin/activate_this.py'4 unmodified linesmodified:docs/deploying/others.rst+19 added-15 removed
1+.. _deploying-other-servers:2+3 Other Servers4 =============5 4-There are popular servers written in Python that allow the execution of5-WSGI applications as well. Keep in mind though that some of these servers6-were written for very specific applications and might not work as well for7-standard WSGI application such as Flask powered ones.6+There are popular servers written in Python that allow the execution of WSGI7+applications as well. These servers stand alone when they run; you can proxy8+to them from your web server.9 9-10 Tornado11 --------12 13 `Tornado`_ is an open source version of the scalable, non-blocking web14 server and tools that power `FriendFeed`_. Because it is non-blocking and15 uses epoll, it can handle thousands of simultaneous standing connections,16 which means it is ideal for real-time web services. Integrating this17 service with Flask is a trivial task::18- 18+19 from tornado.wsgi import WSGIContainer20 from tornado.httpserver import HTTPServer21 from tornado.ioloop import IOLoop22 from yourapplication import app23- 23+24 http_server = HTTPServer(WSGIContainer(app))25 http_server.listen(5000)26 IOLoop.instance().start()27 28 29 .. _Tornado: http://www.tornadoweb.org/30 .. _FriendFeed: http://friendfeed.com/31 32-32 Gevent33 -------34 35 `Gevent`_ is a coroutine-based Python networking library that uses9 unmodified lines45 .. _Gevent: http://www.gevent.org/46 .. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html47 .. _libevent: http://monkey.org/~provos/libevent/48 50-49 Gunicorn50 --------51 52 `Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork1 unmodified line54 and `greenlet`_. Running a Flask application on this server is quite simple::55 56 gunicorn myproject:app57 58+`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.59+For example, to run a Flask application with 4 worker processes (``-w60+4``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::61+62+ gunicorn -w 4 -b 127.0.0.1:4000 myproject:app63+64 .. _Gunicorn: http://gunicorn.org/65 .. _eventlet: http://eventlet.net/66 .. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html67 64-68 Proxy Setups69 ------------70 68-If you deploy your application behind an HTTP proxy you will need to69-rewrite a few headers in order for the application to work. The two70-problematic values in the WSGI environment usually are `REMOTE_ADDR` and71-`HTTP_HOST`. Werkzeug ships a fixer that will solve some common setups,72-but you might want to write your own WSGI middleware for specific setups.71+If you deploy your application using one of these servers behind an HTTP72+proxy you will need to rewrite a few headers in order for the73+application to work. The two problematic values in the WSGI environment74+usually are `REMOTE_ADDR` and `HTTP_HOST`. Werkzeug ships a fixer that75+will solve some common setups, but you might want to write your own WSGI76+middleware for specific setups.77 78 The most common setup invokes the host being set from `X-Forwarded-Host`79 and the remote address from `X-Forwarded-For`::80 22 unmodified linesadded:docs/deploying/uwsgi.rst+68 added
1+.. _deploying-uwsgi:2+3+uWSGI4+=====5+6+uWSGI 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 WSGI9+application with uWSGI protocol you will need a uWSGI server10+first. uWSGI is both a protocol and an application server; the11+application server can serve uWSGI, FastCGI, and HTTP protocols.12+13+The most popular uWSGI server is `uwsgi`_, which we will use for this14+guide. Make sure to have it installed to follow along.15+16+.. admonition:: Watch Out17+18+ 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.23+24+Starting your app with uwsgi25+----------------------------26+27+`uwsgi` is designed to operate on WSGI callables found in python modules.28+29+Given a flask application in myapp.py, use the following command:30+31+.. sourcecode:: text32+33+ $ uwsgi -s /tmp/uwsgi.sock --module myapp --callable app34+35+Or, if you prefer:36+37+.. sourcecode:: text38+39+ $ uwsgi -s /tmp/uwsgi.sock myapp:app40+41+Configuring nginx42+-----------------43+44+A basic flask uWSGI configuration for nginx looks like this::45+46+ 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+ }54+55+This configuration binds the application to `/yourapplication`. If you want56+to have it in the URL root it's a bit simpler because you don't have to tell57+it the WSGI `SCRIPT_NAME` or set the uwsgi modifier to make use of it::58+59+ location / { try_files $uri @yourapplication; }60+ location @yourapplication {61+ include uwsgi_params;62+ uwsgi_pass unix:/tmp/uwsgi.sock;63+ }64+65+.. _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 lines38 return app39 40 The downside is that you cannot use the application object in the blueprints41 at import time. You can however use it from within a request. How do you42-get access the application with the config? Use42+get access to the application with the config? Use43 :data:`~flask.current_app`::44 45 from flask import current_app, Blueprint, render_template46 admin = Blueprint('admin', __name__, url_prefix='/admin')29 unmodified linesmodified:docs/quickstart.rst+1 added-1 removed
9 unmodified lines10 11 A Minimal Application12 ---------------------13 14-A minimal Flask application looks something like that::14+A minimal Flask application looks something like this::15 16 from flask import Flask17 app = Flask(__name__)18 731 unmodified linesmodified:docs/security.rst+7 added
93 unmodified lines94 95 JSON Security96 -------------97 98+.. admonition:: ECMAScript 5 Changes99+100+ 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.104+105 JSON itself is a high-level serialization format, so there is barely106 anything that could cause security problems, right? You can't declare107 recursive structures that could cause problems and the only thing that108 could possibly break are very large responses that can cause some kind of67 unmodified lines