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
Provenance
- Commit
c64a4e0- Tree
ad130eb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a50a87c
7 files changed+318 added−308 removed
added:docs/deploying/cgi.rst+42 added
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-optimal6+performance.7+8+This is also the way you can use a Flask application on Google's9+`AppEngine`_, there however the execution does happen in a CGI-like10+environment. The application's performance is unaffected because of that.11+12+.. _AppEngine: http://code.google.com/appengine/13+14+Creating a `.cgi` file15+----------------------16+17+First you need to create the CGI application file. Let's call it18+`yourapplication.cgi`::19+20+ #!/usr/bin/python21+ from wsgiref.handlers import CGIHandler22+ from yourapplication import app23+24+ CGIHandler().run(app)25+26+If you're running Python 2.4 you will need the :mod:`wsgiref` package. Python27+2.5 and higher ship this as part of the standard library.28+29+Server Setup30+------------31+32+Usually there are two ways to configure the server. Either just copy the33+`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to34+rewrite the URL) or let the server point to the file directly.35+36+In Apache for example you can put a like like this into the config:37+38+.. sourcecode:: apache39+40+ ScriptAlias /app /path/to/the/application.cgi41+42+For more information consult the documentation of your webserver.added:docs/deploying/fastcgi.rst+128 added
1+FastCGI2+=======3+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.7+8+The most popular one is `flup`_ which we will use for this guide. Make9+sure to have it installed.10+11+Creating a `.fcgi` file12+-----------------------13+14+First you need to create the FastCGI server file. Let's call it15+`yourapplication.fcgi`::16+17+ #!/usr/bin/python18+ from flup.server.fcgi import WSGIServer19+ from yourapplication import app20+21+ WSGIServer(app).run()22+23+This is enough for Apache to work, however lighttpd and nginx need a24+socket to communicate with the FastCGI server. For that to work you25+need to pass the path to the socket to the26+:class:`~flup.server.fcgi.WSGIServer`::27+28+ WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()29+30+The path has to be the exact same path you define in the server31+config.32+33+Save the `yourapplication.fcgi` file somewhere you will find it again.34+It makes sense to have that in `/var/www/yourapplication` or something35+similar.36+37+Make sure to set the executable bit on that file so that the servers38+can execute it::39+40+ # chmod +x /var/www/yourapplication/yourapplication.fcgi41+42+Configuring lighttpd43+--------------------44+45+A basic FastCGI configuration for lighttpd looks like that::46+47+ 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+ )54+55+This configuration binds the application to `/yourapplication`. If you56+want the application to work in the URL root you have to work around a57+lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`58+middleware.59+60+Make sure to apply it only if you are mounting the application the URL61+root.62+63+Configuring nginx64+-----------------65+66+Installing FastCGI applications on nginx is a bit tricky because by default67+some FastCGI parameters are not properly forwarded.68+69+A basic FastCGI configuration for nginx looks like this::70+71+ 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+ }80+81+This configuration binds the application to `/yourapplication`. If you want82+to have it in the URL root it's a bit easier because you don't have to figure83+out how to calculate `PATH_INFO` and `SCRIPT_NAME`::84+85+ 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+ }91+92+Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You93+can either write an `init.d` script for that or execute it inside a screen94+session::95+96+ $ screen97+ $ /var/www/yourapplication/yourapplication.fcgi98+99+Debugging100+---------101+102+FastCGI deployments tend to be hard to debug on most webservers. Very often the103+only thing the server log tells you is something along the lines of "premature104+end of headers". In order to debug the application the only thing that can105+really give you ideas why it breaks is switching to the correct user and106+executing the application by hand.107+108+This example assumes your application is called `application.fcgi` and that your109+webserver user is `www-data`::110+111+ $ 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 yourapplication117+118+In this case the error seems to be "yourapplication" not being on the python119+path. Common problems are:120+121+- 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.125+126+.. _lighttpd: http://www.lighttpd.net/127+.. _nginx: http://nginx.net/128+.. _flup: http://trac.saddi.com/flupadded:docs/deploying/index.rst+19 added
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.8+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.12+13+.. toctree::14+ :maxdepth: 215+16+ mod_wsgi17+ cgi18+ fastcgi19+ othersadded:docs/deploying/mod_wsgi.rst+80 added
1+mod_wsgi (Apache)2+=================3+4+If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.5+6+.. _Apache: http://httpd.apache.org/7+8+Installing `mod_wsgi`9+---------------------10+11+If you don't have `mod_wsgi` installed yet you have to either install it using12+a package manager or compile it yourself.13+14+The mod_wsgi `installation instructions`_ cover installation instructions for15+source installations on UNIX systems.16+17+If you are using ubuntu / debian you can apt-get it and activate it as follows::18+19+ # apt-get install libapache2-mod-wsgi20+21+On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using22+pkg_add::23+24+ # pkg_add -r mod_wsgi25+26+If you are using pkgsrc you can install `mod_wsgi` by compiling the27+`www/ap2-wsgi` package.28+29+If you encounter segfaulting child processes after the first apache reload you30+can safely ignore them. Just restart the server.31+32+Creating a `.wsgi` file33+-----------------------34+35+To run your application you need a `yourapplication.wsgi` file. This file36+contains the code `mod_wsgi` is executing on startup to get the application37+object. The object called `application` in that file is then used as38+application.39+40+For most applications the following file should be sufficient::41+42+ from yourapplication import app as application43+44+If you don't have a factory function for application creation but a singleton45+instance you can directly import that one as `application`.46+47+Store that file somewhere where you will find it again (eg:48+`/var/www/yourapplication`) and make sure that `yourapplication` and all49+the libraries that are in use are on the python load path. If you don't50+want to install it system wide consider using a `virtual python`_ instance.51+52+Configuring Apache53+------------------54+55+The last thing you have to do is to create an Apache configuration file for56+your application. In this example we are telling `mod_wsgi` to execute the57+application under a different user for security reasons:58+59+.. sourcecode:: apache60+61+ <VirtualHost *>62+ ServerName example.com63+64+ WSGIDaemonProcess yourapplication user=user1 group=group1 threads=565+ WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi66+67+ <Directory /var/www/yourapplication>68+ WSGIProcessGroup yourapplication69+ WSGIApplicationGroup %{GLOBAL}70+ Order deny,allow71+ Allow from all72+ </Directory>73+ </VirtualHost>74+75+For more information consult the `mod_wsgi wiki`_.76+77+.. _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
1+Other Servers2+=============3+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.8+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+ 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()27+28+29+.. _Tornado: http://www.tornadoweb.org/30+.. _FriendFeed: http://friendfeed.com/31+32+33+Gevent34+-------35+36+`Gevent`_ is a coroutine-based Python networking library that uses37+`greenlet`_ to provide a high-level synchronous API on top of `libevent`_38+event loop::39+40+ from gevent.wsgi import WSGIServer41+ from yourapplication import app42+43+ http_server = WSGIServer(('', 5000), app)44+ http_server.serve_forever()45+46+.. _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
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.8-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.12-13-14-mod_wsgi (Apache)15------------------16-17-If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.18-19-.. _Apache: http://httpd.apache.org/20-21-Installing `mod_wsgi`22-`````````````````````23-24-If you don't have `mod_wsgi` installed yet you have to either install it using25-a package manager or compile it yourself.26-27-The mod_wsgi `installation instructions`_ cover installation instructions for28-source installations on UNIX systems.29-30-If you are using ubuntu / debian you can apt-get it and activate it as follows::31-32- # apt-get install libapache2-mod-wsgi33-34-On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using35-pkg_add::36-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-42-If you encounter segfaulting child processes after the first apache reload you43-can safely ignore them. Just restart the server.44-45-Creating a `.wsgi` file46-```````````````````````47-48-To run your application you need a `yourapplication.wsgi` file. This file49-contains the code `mod_wsgi` is executing on startup to get the application50-object. The object called `application` in that file is then used as51-application.52-53-For most applications the following file should be sufficient::54-55- from yourapplication import app as application56-57-If you don't have a factory function for application creation but a singleton58-instance you can directly import that one as `application`.59-60-Store that file somewhere where you will find it again (eg: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't63-want to install it system wide consider using a `virtual python`_ instance.64-65-Configuring Apache66-``````````````````67-68-The last thing you have to do is to create an Apache configuration file for69-your application. In this example we are telling `mod_wsgi` to execute the70-application under a different user for security reasons:71-72-.. sourcecode:: apache73-74- <VirtualHost *>75- ServerName example.com76-77- WSGIDaemonProcess yourapplication user=user1 group=group1 threads=578- WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi79-80- <Directory /var/www/yourapplication>81- WSGIProcessGroup yourapplication82- WSGIApplicationGroup %{GLOBAL}83- Order deny,allow84- Allow from all85- </Directory>86- </VirtualHost>87-88-For more information consult the `mod_wsgi wiki`_.89-90-.. _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/94-95-96-CGI97----98-99-If all other deployment methods do not work, CGI will work for sure. CGI100-is supported by all major servers but usually has a less-than-optimal101-performance.102-103-This is also the way you can use a Flask application on Google's104-`AppEngine`_, there however the execution does happen in a CGI-like105-environment. The application's performance is unaffected because of that.106-107-.. _AppEngine: http://code.google.com/appengine/108-109-Creating a `.cgi` file110-``````````````````````111-112-First you need to create the CGI application file. Let's call it113-`yourapplication.cgi`::114-115- #!/usr/bin/python116- from wsgiref.handlers import CGIHandler117- from yourapplication import app118-119- CGIHandler().run(app)120-121-If you're running Python 2.4 you will need the :mod:`wsgiref` package. Python122-2.5 and higher ship this as part of the standard library.123-124-Server Setup125-````````````126-127-Usually there are two ways to configure the server. Either just copy the128-`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to129-rewrite the URL) or let the server point to the file directly.130-131-In Apache for example you can put a like like this into the config:132-133-.. sourcecode:: apache134-135- ScriptAlias /app /path/to/the/application.cgi136-137-For more information consult the documentation of your webserver.138-139-140-141-FastCGI142--------143-144-A very popular deployment setup on servers like `lighttpd`_ and `nginx`_145-is FastCGI. To use your WSGI application with any of them you will need146-a FastCGI server first.147-148-The most popular one is `flup`_ which we will use for this guide. Make149-sure to have it installed.150-151-Creating a `.fcgi` file152-```````````````````````153-154-First you need to create the FastCGI server file. Let's call it155-`yourapplication.fcgi`::156-157- #!/usr/bin/python158- from flup.server.fcgi import WSGIServer159- from yourapplication import app160-161- WSGIServer(app).run()162-163-This is enough for Apache to work, however lighttpd and nginx need a164-socket to communicate with the FastCGI server. For that to work you165-need to pass the path to the socket to the166-:class:`~flup.server.fcgi.WSGIServer`::167-168- WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()169-170-The path has to be the exact same path you define in the server171-config.172-173-Save the `yourapplication.fcgi` file somewhere you will find it again.174-It makes sense to have that in `/var/www/yourapplication` or something175-similar.176-177-Make sure to set the executable bit on that file so that the servers178-can execute it::179-180- # chmod +x /var/www/yourapplication/yourapplication.fcgi181-182-Configuring lighttpd183-````````````````````184-185-A basic FastCGI configuration for lighttpd looks like that::186-187- 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- )194-195-This configuration binds the application to `/yourapplication`. If you196-want the application to work in the URL root you have to work around a197-lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`198-middleware.199-200-Make sure to apply it only if you are mounting the application the URL201-root.202-203-Configuring nginx204-`````````````````205-206-Installing FastCGI applications on nginx is a bit tricky because by default207-some FastCGI parameters are not properly forwarded.208-209-A basic FastCGI configuration for nginx looks like this::210-211- 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- }220-221-This configuration binds the application to `/yourapplication`. If you want222-to have it in the URL root it's a bit easier because you don't have to figure223-out how to calculate `PATH_INFO` and `SCRIPT_NAME`::224-225- 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- }231-232-Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You233-can either write an `init.d` script for that or execute it inside a screen234-session::235-236- $ screen237- $ /var/www/yourapplication/yourapplication.fcgi238-239-Debugging240-`````````241-242-FastCGI deployments tend to be hard to debug on most webservers. Very often the243-only thing the server log tells you is something along the lines of "premature244-end of headers". In order to debug the application the only thing that can245-really give you ideas why it breaks is switching to the correct user and246-executing the application by hand.247-248-This example assumes your application is called `application.fcgi` and that your249-webserver user is `www-data`::250-251- $ 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 yourapplication257-258-In this case the error seems to be "yourapplication" not being on the python259-path. Common problems are:260-261-- 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.265-266-.. _lighttpd: http://www.lighttpd.net/267-.. _nginx: http://nginx.net/268-.. _flup: http://trac.saddi.com/flup269-270-271-272-Tornado273---------274-275-`Tornado`_ is an open source version of the scalable, non-blocking web server and tools that power `FriendFeed`_.276-Because 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.277-Integrating this service with Flask is a trivial task::278-279- 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()288-289-290-.. _Tornado: http://www.tornadoweb.org/291-.. _FriendFeed: http://friendfeed.com/292-293-294-Gevent295--------296-297-`Gevent`_ is a coroutine-based Python networking library that uses `greenlet`_ to provide a high-level synchronous API on top of `libevent`_ event loop::298-299- from gevent.wsgi import WSGIServer300- from yourapplication import app301-302- http_server = WSGIServer(('', 5000), app)303- http_server.serve_forever()304-305-.. _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 lines40 quickstart41 tutorial/index42 testing43 patterns/index44- deploying44+ deploying/index45 becomingbig46 design47 48 Reference9 unmodified lines