English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Flask FastCGI

FastCGI is another deployment option for Flask applications on web servers (such as nginix, lighttpd, and Cherokee).

Configure FastCGI

Firstly, you need to create a FastCGI server file, for example, its name is: yourapplication.fcgiC .

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from flup.server.fcgi import WSGIServer
 from yourapplication import app
 if __name__ == '__main__':
     WSGIServer(app).run()

nginx and earlier versions of lighttpd require an explicit socket to communicate with the FastCGI server. The path needs to be passed to the WSGIServer's socket.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
WSGIServer(application, bindAddress = '/path/to/fcgi.sock').run()

Configure Apache

For basic Apache deployment, .fcgi The file will appear in your application URL, for example http://example.com/yourapplication.fcgi/hello/There are several methods to configure the application so that yourapplication.fcgi does not appear in the URL.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
<VirtualHost> *>
    ServerName example.com
    ScriptAlias / /path/to/yourapplication.fcgi/
 </VirtualHost>

Configure lighttpd

The basic configuration of lighttpd looks like this -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
fastcgi.server = ("/yourapplication.fcgi" => ((
    "socket" => "/tmp/yourapplication-fcgi.sock",
    "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",
    "check-local" => "disable",
    "max-procs" => 1
 )))
 alias.url = (
    "/static/" => "/path/to/your/static"
 )
 url.rewrite-once = (
    "^(/static($|/.*))$" => "$"1",
    "^(/.*)$" => "/yourapplication.fcgi$"1"
 )

Remember to enable FastCGI, alias, and rewrite module. This configuration binds the application to/yourapplication.