Flask deployement on lighttpd and raspberry pi
Asked Answered
A

3

7

I'm trying to deploy a hello flask app to a raspberry pi using lighttpd fastCGI.

I followed the instructions on the http://flask.pocoo.org/docs/0.10/deploying/fastcgi/ to the best of my ability

Here is my flask app (/var/www/demoapp/hello.py)

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World From Flask Yeh!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

And here is my .fcgi file (/var/www/demoapp/hello.fcgi)

#!/usr/bin/python
from flup.server.fcgi import WSGIServer
from yourapplication import app

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

And here is what I added to my /etc/lighttpd/lighttpd.conf

fastcgi.server = ("/hello.fcgi" =>
    ((
        "socket" => "/tmp/hello-fcgi.sock",
        "bin-path" => "/var/www/demoapp/hello.fcgi",
        "check-local" => "disable",
        "max-procs" => 1
    ))
)

alias.url = (
    "/static/" => "/var/www/demoapp/static/",
)

I get a 404 Not Found error

By the way what is the /tmp/hello-fcgi.sock where do I get this file

Please help. I'm essentially trying to find a simple way to deploy flask on my raspberry pi web server. I have tried several methods. The fastcgi seemed to be the easiest. If there is an easier way then let me know please.

Thank you

Vincent

Arak answered 8/5, 2015 at 9:54 Comment(2)
I'm having the same issue... did you find something?Sibelius
fast cgi is old. go for wsgi. Gunicorn is your friend. Apache work fine for some projects.Decorticate
B
3

I believe the problem is that in your hello.fcgi file, you are importing a module named yourapplication, however, the flask application you created is named hello.

Try changing this line:

from yourapplication import app to from hello import app

Edit: Also - double check your url when testing - since your @app.route is set to the root, you must include the trailing slash in your url, eg:

http://xxx.xxx.x.xx/hello.fcgi/

and not

http://xxx.xxx.x.xx/hello.fcgi

Boeschen answered 6/9, 2015 at 23:12 Comment(0)
D
1

First, like c_tothe_k said, you need to change yourapplication to hello in your hello.fcgi file.

I found the instructions in the flask documentation to be lacking. It recommends reading this page, and so do I, http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModFastCGI#Troubleshooting

The bottom of the page has an example lighttpd.conf

I followed a hybrid of the instructions in the Flask documentation and the above page. I renamed the .fcgi file to .py, as shown in the Lightty documentation.

You don't have to worry about the .sock file if you follow this approach. It's the old way that lighttpd used to communicate with a FastCGI process, using a UNIX socket. It just needs to be here so the config parser isn't broken.

I used the following in my lighttpd.conf. Your other files look fine otherwise. (Note, this will put your app under /hello, and not /.)

fastcgi.server = (
    "/hello" =>
    (
        "python-fcgi" =>
        (
         "socket" => "/tmp/fastcgi.python.socket",
         "bin-path" => "/var/www/demoapp/hello.py",
         "check-local" => "disable",
         "max-procs" => 1,
        )
    )
)
Darden answered 2/12, 2015 at 7:25 Comment(0)
D
0

What worked for me is putting config part into /etc/lighttpd/conf-enabled/10-fastcgi.conf instead of /etc/lighttpd/lighttpd.conf

I am guessing that file appears there after you run sudo lighttpd-enable-mod fastcgi

I also ran pip3 install flup-py3 to make sure that python3 support is there.

Defensible answered 23/9, 2021 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.