deploying.rst
This file
Path
docs/deploying.rst
267 lines · 8.5 KiB
Last changed by
Fixed a doc display bug and setup.py workaround for dev version.Armin Ronacherbefore adoption1fff3e5
History of this fileTake it
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.121314FastCGI15-------1617A very popular deployment setup on servers like `lighttpd`_ and `nginx`_18is FastCGI. To use your WSGI application with any of them you will need19a FastCGI server first.2021The most popular one is `flup`_ which we will use for this guide. Make22sure to have it installed.2324Creating a `.fcgi` file25```````````````````````2627First you need to create the FastCGI server file. Let's call it28`yourapplication.fcgi`::2930 #!/usr/bin/python31 from flup.server.fcgi import WSGIServer32 from yourapplication import app3334 WSGIServer(app).run()3536This is enough for Apache to work, however lighttpd and nginx need a37socket to communicate with the FastCGI server. For that to work you38need to pass the path to the socket to the39:class:`~flup.server.fcgi.WSGIServer`::4041 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()4243The path has to be the exact same path you define in the server44config.4546Save the `yourapplication.fcgi` file somewhere you will find it again.47It makes sense to have that in `/var/www/yourapplication` or something48similar.4950Make sure to set the executable bit on that file so that the servers51can execute it::5253 # chmod +x /var/www/yourapplication/yourapplication.fcgi5455Configuring lighttpd56````````````````````5758A basic FastCGI configuration for lighttpd looks like that::5960 fastcgi.server = ("/yourapplication" =>61 "yourapplication" => (62 "socket" => "/tmp/yourapplication-fcgi.sock",63 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",64 "check-local" => "disable"65 )66 )6768This configuration binds the application to `/yourapplication`. If you69want the application to work in the URL root you have to work around a70lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`71middleware.7273Make sure to apply it only if you are mounting the application the URL74root.7576Configuring nginx77`````````````````7879Installing FastCGI applications on nginx is a bit tricky because by default80some FastCGI parameters are not properly forwarded.8182A basic FastCGI configuration for nginx looks like this::8384 location /yourapplication/ {85 include fastcgi_params;86 if ($uri ~ ^/yourapplication/(.*)?) {87 set $path_url $1;88 }89 fastcgi_param PATH_INFO $path_url;90 fastcgi_param SCRIPT_NAME /yourapplication;91 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;92 }9394This configuration binds the application to `/yourapplication`. If you want95to have it in the URL root it's a bit easier because you don't have to figure96out how to calculate `PATH_INFO` and `SCRIPT_NAME`::9798 location /yourapplication/ {99 include fastcgi_params;100 fastcgi_param PATH_INFO $fastcgi_script_name;101 fastcgi_param SCRIPT_NAME "";102 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;103 }104105Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You106can either write an `init.d` script for that or execute it inside a screen107session::108109 $ screen110 $ /var/www/yourapplication/yourapplication.fcgi111112Debugging113`````````114115FastCGI deployments tend to be hard to debug on most webservers. Very often the116only thing the server log tells you is something along the lines of "premature117end of headers". In order to debug the application the only thing that can118really give you ideas why it breaks is switching to the correct user and119executing the application by hand.120121This example assumes your application is called `application.fcgi` and that your122webserver user is `www-data`::123124 $ su www-data125 $ cd /var/www/yourapplication126 $ python application.fcgi127 Traceback (most recent call last):128 File "yourapplication.fcg", line 4, in <module>129 ImportError: No module named yourapplication130131In this case the error seems to be "yourapplication" not being on the python132path. Common problems are:133134- relative paths being used. Don't rely on the current working directory135- the code depending on environment variables that are not set by the136 web server.137- different python interpreters being used.138139.. _lighttpd: http://www.lighttpd.net/140.. _nginx: http://nginx.net/141.. _flup: http://trac.saddi.com/flup142143144mod_wsgi (Apache)145-----------------146147If you are using the `Apache`_ webserver you should consider using `mod_wsgi`_.148149.. _Apache: http://httpd.apache.org/150151Installing `mod_wsgi`152`````````````````````153154If you don't have `mod_wsgi` installed yet you have to either install it using155a package manager or compile it yourself.156157The mod_wsgi `installation instructions`_ cover installation instructions for158source installations on UNIX systems.159160If you are using ubuntu / debian you can apt-get it and activate it as follows::161162 # apt-get install libapache2-mod-wsgi163164On FreeBSD install `mod_wsgi` by compiling the `www/mod_wsgi` port or by using165pkg_add::166167 # pkg_add -r mod_wsgi168169If you are using pkgsrc you can install `mod_wsgi` by compiling the170`www/ap2-wsgi` package.171172If you encounter segfaulting child processes after the first apache reload you173can safely ignore them. Just restart the server.174175Creating a `.wsgi` file176```````````````````````177178To run your application you need a `yourapplication.wsgi` file. This file179contains the code `mod_wsgi` is executing on startup to get the application180object. The object called `application` in that file is then used as181application.182183For most applications the following file should be sufficient::184185 from yourapplication import app as application186187If you don't have a factory function for application creation but a singleton188instance you can directly import that one as `application`.189190Store that file somewhere where you will find it again (eg:191`/var/www/yourapplication`) and make sure that `yourapplication` and all192the libraries that are in use are on the python load path. If you don't193want to install it system wide consider using a `virtual python`_ instance.194195Configuring Apache196``````````````````197198The last thing you have to do is to create an Apache configuration file for199your application. In this example we are telling `mod_wsgi` to execute the200application under a different user for security reasons:201202.. sourcecode:: apache203204 <VirtualHost *>205 ServerName example.com206207 WSGIDaemonProcess yourapplication user=user1 group=group1 processes=1 threads=5208 WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi209210 <Directory /var/www/yourapplication>211 WSGIProcessGroup yourapplication212 WSGIApplicationGroup %{GLOBAL}213 Order deny,allow214 Allow from all215 </Directory>216 </VirtualHost>217218For more information consult the `mod_wsgi wiki`_.219220.. _mod_wsgi: http://code.google.com/p/modwsgi/221.. _installation instructions: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide222.. _virtual python: http://pypi.python.org/pypi/virtualenv223.. _mod_wsgi wiki: http://code.google.com/p/modwsgi/wiki/224225226CGI227---228229If all other deployment methods do not work, CGI will work for sure. CGI230is supported by all major browsers but usually has a less-than-optimal231performance.232233This is also the way you can use a Flask application on Google's234`AppEngine`_, there however the execution does happen in a CGI-like235environment. The application's performance is unaffected because of that.236237.. _AppEngine: http://code.google.com/appengine/238239Creating a `.cgi` file240``````````````````````241242First you need to create the CGI application file. Let's call it243`yourapplication.cgi`::244245 #!/usr/bin/python246 from wsgiref.handlers import CGIHandler247 from yourapplication import app248249 CGIHandler().run(app)250251If you're running Python 2.4 you will need the :mod:`wsgiref` package. Python2522.5 and higher ship this as part of the standard library.253254Server Setup255````````````256257Usually there are two ways to configure the server. Either just copy the258`.cgi` into a `cgi-bin` (and use `mod_rerwite` or something similar to259rewrite the URL) or let the server point to the file directly.260261In Apache for example you can put a like like this into the config:262263.. sourcecode:: apache264265 ScriptName /app /path/to/the/application.cgi266267For more information consult the documentation of your webserver.