Flask FastCGI Setup

3.3k views Asked by At

I'm having problems with deploying a Flask application to an Apache server with FastCGI (Uberspace). My basic hello world app is working. I set a variable for the index view. But chances on the variable won't update the view in the browser. Running the process with python geoflask.fcgi will show an updated version (in the terminal) but with following warnings:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK

I am using a virtualenv and my files looks like this:

my fcgi-bin/geoflask.fcgi:

#!/home/usr/.virtualenvs/flaskenv/bin/python2.7

RELATIVE_WEB_URL_PATH = '/geoflask'
import os
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/geoflask'

import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)

from flup.server.fcgi import WSGIServer
from app import app


class ScriptNamePatch(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
        return self.app(environ, start_response)

app = ScriptNamePatch(app)

if __name__ == '__main__':
    WSGIServer(app).run()

my .htacces:

<IfModule mod_fcgid.c>
   AddHandler fcgid-script .fcgi
   <Files ~ (\.fcgi)>
       SetHandler fcgid-script
       Options +FollowSymLinks +ExecCGI
   </Files>
</IfModule>

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ /fcgi-bin/geoflask.fcgi/$1 [QSA,L]
</IfModule>

Any hints or suggestions? I am struggling with it the whole day ...

1

There are 1 answers

0
Sean Vieira On

Apache does not reload the FastCGI server process immediately. Looking at the docs for mod_fastcgi it seems that mod_fastcgi only supports reloading after an idle period, after a certain number of requests, or after a certain period of time. This is why your application does not seem to update, even though when you run it from the command line, it does.

To get the behavior you want (reloading on every change) it seems that you will need to set either FcgidMaxRequestsPerProcess or FcgidCmdOptions MaxRequestsPerProcess to 1 (essentially making your FastCGI setup into a CGI setup). This will reload the application on every request, so it shouldn't be used for production - but it will make development easier.