1

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 ...

kOssi
  • 745
  • 2
  • 10
  • 19
  • What variable are you talking about? Can you post the relevant code from `app`? – Sean Vieira Feb 12 '14 at 03:03
  • It is just a simple hello world example with following route in app.py: `@app.route('/') def index(): return render_template('index.html', name='hans')` It works well if it runs on my localhost, but on server the variable do not update changes. Deleting the browser cache did not solve the problem. – kOssi Feb 12 '14 at 09:27
  • So you are editing the file on the server (changing `name`) and then reloading the browser? – Sean Vieira Feb 12 '14 at 14:10
  • Yes I do. If I run my fcgi script directly with python the stdout is correct. The Browser output is not. – kOssi Feb 12 '14 at 16:48

1 Answers1

2

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.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293