librelist archives

« back to archive

Deploying Flask to an Lighttpd environment

Deploying Flask to an Lighttpd environment

From:
Daniel
Date:
2012-08-09 @ 18:09
Hey,

I'm currently having a hard time deploying my Flask app to an Lighttpd
environment.

After searching I found and adapted a lighttpd configuration - but as
soon as I use sessions in my app, I get an internal server error.
I get this weird behavior only while using lighttpd, with
app.run(host='0.0.0.0') it's running perfectly.

I know about the Lighttpd fix from werkzeug.contrib.fixers and I'm using
it to fix "domain.org/app.fcgi/about" to "domain.org/about".

Here's the configuration:

> $HTTP["host"] =~ "(^|www\.|m\.)domain\.org" {
>     server.document-root = "/var/www/MyApp/"
> 
>     accesslog.filename = "/var/log/lighttpd/MyApp-access.log"
>     server.errorlog = "/var/log/lighttpd/MyApp-error.log"
> 
>     fastcgi.server = (
>         "/" =>
>             ((
>             "socket" => "/tmp/MyApp-fcgi.sock",
>             "bin-path" => "/var/www/MyApp/MyApp.fcgi",
>             "check-local" => "disable",
>             "max-procs" => 1,
>             "fix-root-scriptname" => "enable"
>             )),
>     )
>     alias.url = (
>         "/static" => "/var/www/MyApp/static/",
>     )
>     url.rewrite-once = (
>         "^(/static.*)$" => "$1",
>         "^/favicon\.ico$" => "/static/favicon.ico",
>         "^/robots\.txt$" => "/static/robots.txt",
>     )
> }


Unfortunately I found neither the Flask documentation on deploying, nor
the lighttpd documentation helpful in this regard;
and on your IRC channel was at the time no one that could have helped me.

What obvious configuration option am I missing here?

Do you have any tips or hints on how to correctly deploy my Flask app?

Re: [flask] Deploying Flask to an Lighttpd environment

From:
Simon Sapin
Date:
2012-08-09 @ 18:28
Le 09/08/2012 20:09, Daniel a écrit :
> After searching I found and adapted a lighttpd configuration - but as
> soon as I use sessions in my app, I get an internal server error.
> I get this weird behavior only while using lighttpd, with
> app.run(host='0.0.0.0') it's running perfectly.

Hi,

Anything that helps in lighty’s error log? If not, try running the fcgi 
script in a terminal and see if you get a traceback:

     sudo -u lighttpd /var/www/MyApp/MyApp.fcgi

-- 
Simon Sapin

Re: [flask] Deploying Flask to an Lighttpd environment

From:
Daniel
Date:
2012-08-09 @ 21:31
Unfortunately the Lighttpd log only shows what I know.

The server gets started without problems, and it sends the files
(favicon and the html) on reuqest.
The last line in the log is the POST request, on which I'm getting an
"internal server error" back, after submitting the form.

access.log
> "POST /login HTTP/1.1" 500 291 "http://127.0.0.1/login" "Mozilla/5.0 
(X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) 
Chrome/21.0.1180.57 Safari/537.1"

error.log
> (log.c.166) server started


The fcgi script runs fine, except for a few 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
> Content-Type: text/html; charset=utf-8
> Content-Length: 886
> 
> <!DOCTYPE html>
> <html lang="en">
>   ...


Here is the smallest snippet I could come up with, which gives me the
"internal server error" on loading /example:

> @app.route('/example')
> def example():
>     session['name'] = 'example'

Re: [flask] Deploying Flask to an Lighttpd environment

From:
Kerem Ulutaş
Date:
2012-08-09 @ 21:54
Internal Server Error happens to me when I have some code block which is
not try / catch'ed.
Try setting a logger for your application and wrap every addition which
brings Internal Server Error with try / catch blocks - it really helps mate.

2012/8/10 Daniel <daniel@trvx.org>

> Unfortunately the Lighttpd log only shows what I know.
>
> The server gets started without problems, and it sends the files
> (favicon and the html) on reuqest.
> The last line in the log is the POST request, on which I'm getting an
> "internal server error" back, after submitting the form.
>
> access.log
> > "POST /login HTTP/1.1" 500 291 "http://127.0.0.1/login" "Mozilla/5.0
> (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko)
> Chrome/21.0.1180.57 Safari/537.1"
>
> error.log
> > (log.c.166) server started
>
>
> The fcgi script runs fine, except for a few 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
> > Content-Type: text/html; charset=utf-8
> > Content-Length: 886
> >
> > <!DOCTYPE html>
> > <html lang="en">
> >   ...
>
>
> Here is the smallest snippet I could come up with, which gives me the
> "internal server error" on loading /example:
>
> > @app.route('/example')
> > def example():
> >     session['name'] = 'example'
>
>


-- 

Blog'umu okudunuz mu? http://www.ulutas.gen.tr

The box said "Requires Windows 95, NT, or better", so I installed Linux.

Re: [flask] Deploying Flask to an Lighttpd environment

From:
Daniel
Date:
2012-08-10 @ 01:03
Oh wow, thanks; this helped me so much!

By using a logger and letting it print the exception to a file I was
able to detect the problem:

I had the secret key in the "if __name__ == '__main__':" part of my
application, which would totally work, if started locally, but not if
the module is imported and started by e.g. some other component.

So now I switched to the paradigm found in the examples

> https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py

and it's working perfectly.


Thanks for saving my day!

Re: [flask] Deploying Flask to an Lighttpd environment

From:
Kerem Ulutaş
Date:
2012-08-10 @ 01:37
This is my to-do list when starting a new project (not only Flask ones):

- set up a decent logging mechanism (import logging)
- set up a decent config file interaction (import configparser) (this is an
alternate to the paradigm you mentioned)

Here is a snippet for the second one:

from ConfigParser import ConfigParser

class application(object):
    def __init__(self):
        self.cp = ConfigParser()
        conf_file = join(abspath(curdir), 'application.ini')
        if(not self.cp.read(conf_file)): self.cfgfile_setup(conf_file)

    def cfgfile_setup(self, filename):
        cfgfile = open(filename,'w')
        cp = ConfigParser()
        cp.add_section('Section1')
        cp.set(' Section1 ', 'key1', 'value1')
        cp.set(' Section1 ', 'key2', 'value2')
        cp.add_section('Section2')
        cp.set(' Section2 ', 'key1', 'value1')
        cp.set(' Section2 ', 'key2', 'value2')
        cp.write(cfgfile)
        cfgfile.close()
        print('Config parameters are written to "{0}"'.format(filename))
        print("You must edit this file and fill in ALL config parameters
correctly.")
        exit(0)

I've minimized this and its pseudo-coded - but you get the idea.

Keep up the good work.

2012/8/10 Daniel <daniel@trvx.org>

> Oh wow, thanks; this helped me so much!
>
> By using a logger and letting it print the exception to a file I was
> able to detect the problem:
>
> I had the secret key in the "if __name__ == '__main__':" part of my
> application, which would totally work, if started locally, but not if
> the module is imported and started by e.g. some other component.
>
> So now I switched to the paradigm found in the examples
>
> > https://github.com/mitsuhiko/flask/blob/master/examples/flaskr/flaskr.py
>
> and it's working perfectly.
>
>
> Thanks for saving my day!
>



-- 

Blog'umu okudunuz mu? http://www.ulutas.gen.tr

The box said "Requires Windows 95, NT, or better", so I installed Linux.