Skip to content
Myr

Break up deployment docs into separate documents.

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 · parent a50a87c · Browse files · 7 files+318 added−308 removed
This commit

State

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

Diff stats

7 files+318 added−308 removed

  • other:+318 added−308 removed

7 files+318 added−308 removed

added:docs/deploying/cgi.rst+42 added
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-optimal6performance.78This is also the way you can use a Flask application on Google's9`AppEngine`_, there however the execution does happen in a CGI-like10environment. The application's performance is unaffected because of that.1112.. _AppEngine: http://code.google.com/appengine/1314Creating a `.cgi` file15----------------------1617First you need to create the CGI application file. Let's call it18`yourapplication.cgi`::1920 #!/usr/bin/python21 from wsgiref.handlers import CGIHandler22 from yourapplication import app2324 CGIHandler().run(app)2526If you're running Python 2.4 you will need the :mod:`wsgiref` package. Python272.5 and higher ship this as part of the standard library.2829Server Setup30------------3132Usually there are two ways to configure the server. Either just copy the33`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to34rewrite the URL) or let the server point to the file directly.3536In Apache for example you can put a like like this into the config:3738.. sourcecode:: apache3940 ScriptAlias /app /path/to/the/application.cgi4142For more information consult the documentation of your webserver.
added:docs/deploying/fastcgi.rst+128 added
1FastCGI2=======34A 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.78The most popular one is `flup`_ which we will use for this guide. Make9sure to have it installed.1011Creating a `.fcgi` file12-----------------------1314First you need to create the FastCGI server file. Let's call it15`yourapplication.fcgi`::1617 #!/usr/bin/python18 from flup.server.fcgi import WSGIServer19 from yourapplication import app2021 WSGIServer(app).run()2223This is enough for Apache to work, however lighttpd and nginx need a24socket to communicate with the FastCGI server. For that to work you25need to pass the path to the socket to the26:class:`~flup.server.fcgi.WSGIServer`::2728 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()2930The path has to be the exact same path you define in the server31config.3233Save the `yourapplication.fcgi` file somewhere you will find it again.34It makes sense to have that in `/var/www/yourapplication` or something35similar.3637Make sure to set the executable bit on that file so that the servers38can execute it::3940 # chmod +x /var/www/yourapplication/yourapplication.fcgi4142Configuring lighttpd43--------------------4445A basic FastCGI configuration for lighttpd looks like that::4647 fastcgi.server = ("/yourapplication" =>48 "yourapplication" => (49 "socket" => "/tmp/yourapplication-fcgi.sock",50 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",51 "check-local" => "disable"52 )53 )5455This configuration binds the application to `/yourapplication`. If you56want the application to work in the URL root you have to work around a57lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`58middleware.5960Make sure to apply it only if you are mounting the application the URL61root.6263Configuring nginx64-----------------6566Installing FastCGI applications on nginx is a bit tricky because by default67some FastCGI parameters are not properly forwarded.6869A basic FastCGI configuration for nginx looks like this::7071 location /yourapplication/ {72 include fastcgi_params;73 if ($uri ~ ^/yourapplication/(.*)?) {74 set $path_url $1;75 }76 fastcgi_param PATH_INFO $path_url;77 fastcgi_param SCRIPT_NAME /yourapplication;78 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;79 }8081This configuration binds the application to `/yourapplication`. If you want82to have it in the URL root it's a bit easier because you don't have to figure83out how to calculate `PATH_INFO` and `SCRIPT_NAME`::8485 location /yourapplication/ {86 include fastcgi_params;87 fastcgi_param PATH_INFO $fastcgi_script_name;88 fastcgi_param SCRIPT_NAME "";89 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;90 }9192Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You93can either write an `init.d` script for that or execute it inside a screen94session::9596 $ screen97 $ /var/www/yourapplication/yourapplication.fcgi9899Debugging100---------101102FastCGI deployments tend to be hard to debug on most webservers. Very often the103only thing the server log tells you is something along the lines of "premature104end of headers". In order to debug the application the only thing that can105really give you ideas why it breaks is switching to the correct user and106executing the application by hand.107108This example assumes your application is called `application.fcgi` and that your109webserver user is `www-data`::110111 $ su www-data112 $ cd /var/www/yourapplication113 $ python application.fcgi114 Traceback (most recent call last):115 File "yourapplication.fcg", line 4, in <module>116 ImportError: No module named yourapplication117118In this case the error seems to be "yourapplication" not being on the python119path. Common problems are:120121- relative paths being used. Don't rely on the current working directory122- the code depending on environment variables that are not set by the123 web server.124- different python interpreters being used.125126.. _lighttpd: http://www.lighttpd.net/127.. _nginx: http://nginx.net/128.. _flup: http://trac.saddi.com/flup
added:docs/deploying/index.rst+19 added
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.89If 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.1213.. toctree::14 :maxdepth: 21516 mod_wsgi17 cgi18 fastcgi19 others
added:docs/deploying/mod_wsgi.rst+80 added
1mod_wsgi (Apache)2=================34If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.56.. _Apache: http://httpd.apache.org/78Installing `mod_wsgi`9---------------------1011If you don't have `mod_wsgi` installed yet you have to either install it using12a package manager or compile it yourself.1314The mod_wsgi `installation instructions`_ cover installation instructions for15source installations on UNIX systems.1617If you are using ubuntu / debian you can apt-get it and activate it as follows::1819 # apt-get install libapache2-mod-wsgi2021On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using22pkg_add::2324 # pkg_add -r mod_wsgi2526If you are using pkgsrc you can install `mod_wsgi` by compiling the27`www/ap2-wsgi` package.2829If you encounter segfaulting child processes after the first apache reload you30can safely ignore them. Just restart the server.3132Creating a `.wsgi` file33-----------------------3435To run your application you need a `yourapplication.wsgi` file. This file36contains the code `mod_wsgi` is executing on startup to get the application37object. The object called `application` in that file is then used as38application.3940For most applications the following file should be sufficient::4142 from yourapplication import app as application4344If you don't have a factory function for application creation but a singleton45instance you can directly import that one as `application`.4647Store that file somewhere where you will find it again (eg:48`/var/www/yourapplication`) and make sure that `yourapplication` and all49the libraries that are in use are on the python load path. If you don't50want to install it system wide consider using a `virtual python`_ instance.5152Configuring Apache53------------------5455The last thing you have to do is to create an Apache configuration file for56your application. In this example we are telling `mod_wsgi` to execute the57application under a different user for security reasons:5859.. sourcecode:: apache6061 <VirtualHost *>62 ServerName example.com6364 WSGIDaemonProcess yourapplication user=user1 group=group1 threads=565 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi6667 <Directory /var/www/yourapplication>68 WSGIProcessGroup yourapplication69 WSGIApplicationGroup %{GLOBAL}70 Order deny,allow71 Allow from all72 </Directory>73 </VirtualHost>7475For more information consult the `mod_wsgi wiki`_.7677.. _mod_wsgi: http://code.google.com/p/modwsgi/78.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide79.. _virtual python: http://pypi.python.org/pypi/virtualenv80.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/
added:docs/deploying/others.rst+48 added
1Other Servers2=============34There 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.8910Tornado11--------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 19 from tornado.wsgi import WSGIContainer20 from tornado.httpserver import HTTPServer21 from tornado.ioloop import IOLoop22 from yourapplication import app23 24 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/313233Gevent34-------3536`Gevent`_ is a coroutine-based Python networking library that uses37`greenlet`_ to provide a high-level synchronous API on top of `libevent`_38event loop::3940 from gevent.wsgi import WSGIServer41 from yourapplication import app4243 http_server = WSGIServer(('', 5000), app)44 http_server.serve_forever()4546.. _Gevent: http://www.gevent.org/47.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html48.. _libevent: http://monkey.org/~provos/libevent/
deleted:docs/deploying.rst-307 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.89If 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.121314mod_wsgi (Apache)15-----------------1617If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.1819.. _Apache: http://httpd.apache.org/2021Installing `mod_wsgi`22`````````````````````2324If you don't have `mod_wsgi` installed yet you have to either install it using25a package manager or compile it yourself.2627The mod_wsgi `installation instructions`_ cover installation instructions for28source installations on UNIX systems.2930If you are using ubuntu / debian you can apt-get it and activate it as follows::3132 # apt-get install libapache2-mod-wsgi3334On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using35pkg_add::3637 # pkg_add -r mod_wsgi3839If you are using pkgsrc you can install `mod_wsgi` by compiling the40`www/ap2-wsgi` package.4142If you encounter segfaulting child processes after the first apache reload you43can safely ignore them. Just restart the server.4445Creating a `.wsgi` file46```````````````````````4748To run your application you need a `yourapplication.wsgi` file. This file49contains the code `mod_wsgi` is executing on startup to get the application50object. The object called `application` in that file is then used as51application.5253For most applications the following file should be sufficient::5455 from yourapplication import app as application5657If you don't have a factory function for application creation but a singleton58instance you can directly import that one as `application`.5960Store that file somewhere where you will find it again (eg: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't63want to install it system wide consider using a `virtual python`_ instance.6465Configuring Apache66``````````````````6768The last thing you have to do is to create an Apache configuration file for69your application. In this example we are telling `mod_wsgi` to execute the70application under a different user for security reasons:7172.. sourcecode:: apache7374 <VirtualHost *>75 ServerName example.com7677 WSGIDaemonProcess yourapplication user=user1 group=group1 threads=578 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi7980 <Directory /var/www/yourapplication>81 WSGIProcessGroup yourapplication82 WSGIApplicationGroup %{GLOBAL}83 Order deny,allow84 Allow from all85 </Directory>86 </VirtualHost>8788For more information consult the `mod_wsgi wiki`_.8990.. _mod_wsgi: http://code.google.com/p/modwsgi/91.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide92.. _virtual python: http://pypi.python.org/pypi/virtualenv93.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/949596CGI97---9899If all other deployment methods do not work, CGI will work for sure. CGI100is supported by all major servers but usually has a less-than-optimal101performance.102103This is also the way you can use a Flask application on Google's104`AppEngine`_, there however the execution does happen in a CGI-like105environment. The application's performance is unaffected because of that.106107.. _AppEngine: http://code.google.com/appengine/108109Creating a `.cgi` file110``````````````````````111112First you need to create the CGI application file. Let's call it113`yourapplication.cgi`::114115 #!/usr/bin/python116 from wsgiref.handlers import CGIHandler117 from yourapplication import app118119 CGIHandler().run(app)120121If you're running Python 2.4 you will need the :mod:`wsgiref` package. Python1222.5 and higher ship this as part of the standard library.123124Server Setup125````````````126127Usually there are two ways to configure the server. Either just copy the128`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to129rewrite the URL) or let the server point to the file directly.130131In Apache for example you can put a like like this into the config:132133.. sourcecode:: apache134135 ScriptAlias /app /path/to/the/application.cgi136137For more information consult the documentation of your webserver.138139140141FastCGI142-------143144A very popular deployment setup on servers like `lighttpd`_ and `nginx`_145is FastCGI. To use your WSGI application with any of them you will need146a FastCGI server first.147148The most popular one is `flup`_ which we will use for this guide. Make149sure to have it installed.150151Creating a `.fcgi` file152```````````````````````153154First you need to create the FastCGI server file. Let's call it155`yourapplication.fcgi`::156157 #!/usr/bin/python158 from flup.server.fcgi import WSGIServer159 from yourapplication import app160161 WSGIServer(app).run()162163This is enough for Apache to work, however lighttpd and nginx need a164socket to communicate with the FastCGI server. For that to work you165need to pass the path to the socket to the166:class:`~flup.server.fcgi.WSGIServer`::167168 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()169170The path has to be the exact same path you define in the server171config.172173Save the `yourapplication.fcgi` file somewhere you will find it again.174It makes sense to have that in `/var/www/yourapplication` or something175similar.176177Make sure to set the executable bit on that file so that the servers178can execute it::179180 # chmod +x /var/www/yourapplication/yourapplication.fcgi181182Configuring lighttpd183````````````````````184185A basic FastCGI configuration for lighttpd looks like that::186187 fastcgi.server = ("/yourapplication" =>188 "yourapplication" => (189 "socket" => "/tmp/yourapplication-fcgi.sock",190 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",191 "check-local" => "disable"192 )193 )194195This configuration binds the application to `/yourapplication`. If you196want the application to work in the URL root you have to work around a197lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`198middleware.199200Make sure to apply it only if you are mounting the application the URL201root.202203Configuring nginx204`````````````````205206Installing FastCGI applications on nginx is a bit tricky because by default207some FastCGI parameters are not properly forwarded.208209A basic FastCGI configuration for nginx looks like this::210211 location /yourapplication/ {212 include fastcgi_params;213 if ($uri ~ ^/yourapplication/(.*)?) {214 set $path_url $1;215 }216 fastcgi_param PATH_INFO $path_url;217 fastcgi_param SCRIPT_NAME /yourapplication;218 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;219 }220221This configuration binds the application to `/yourapplication`. If you want222to have it in the URL root it's a bit easier because you don't have to figure223out how to calculate `PATH_INFO` and `SCRIPT_NAME`::224225 location /yourapplication/ {226 include fastcgi_params;227 fastcgi_param PATH_INFO $fastcgi_script_name;228 fastcgi_param SCRIPT_NAME "";229 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;230 }231232Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You233can either write an `init.d` script for that or execute it inside a screen234session::235236 $ screen237 $ /var/www/yourapplication/yourapplication.fcgi238239Debugging240`````````241242FastCGI deployments tend to be hard to debug on most webservers. Very often the243only thing the server log tells you is something along the lines of "premature244end of headers". In order to debug the application the only thing that can245really give you ideas why it breaks is switching to the correct user and246executing the application by hand.247248This example assumes your application is called `application.fcgi` and that your249webserver user is `www-data`::250251 $ su www-data252 $ cd /var/www/yourapplication253 $ python application.fcgi254 Traceback (most recent call last):255 File "yourapplication.fcg", line 4, in <module>256 ImportError: No module named yourapplication257258In this case the error seems to be "yourapplication" not being on the python259path. Common problems are:260261- relative paths being used. Don't rely on the current working directory262- the code depending on environment variables that are not set by the263 web server.264- different python interpreters being used.265266.. _lighttpd: http://www.lighttpd.net/267.. _nginx: http://nginx.net/268.. _flup: http://trac.saddi.com/flup269270271272Tornado273--------274275`Tornado`_ is an open source version of the scalable, non-blocking web server and tools that power `FriendFeed`_.276Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services.277Integrating this service with Flask is a trivial task::278279 280 from tornado.wsgi import WSGIContainer281 from tornado.httpserver import HTTPServer282 from tornado.ioloop import IOLoop283 from yourapplication import app284 285 http_server = HTTPServer(WSGIContainer(app))286 http_server.listen(5000)287 IOLoop.instance().start()288289290.. _Tornado: http://www.tornadoweb.org/291.. _FriendFeed: http://friendfeed.com/292293294Gevent295-------296297`Gevent`_ is a coroutine-based Python networking library that uses `greenlet`_ to provide a high-level synchronous API on top of `libevent`_ event loop::298299 from gevent.wsgi import WSGIServer300 from yourapplication import app301302 http_server = WSGIServer(('', 5000), app)303 http_server.serve_forever()304305.. _Gevent: http://www.gevent.org/306.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html307.. _libevent: http://monkey.org/~provos/libevent/
modified:docs/index.rst+1 added-1 removed
39 unmodified lines
40 quickstart41 tutorial/index42 testing43 patterns/index44 deploying44 deploying/index45 becomingbig46 design4748Reference
9 unmodified lines