Flask deployement on lighttpd and raspberry pi

2019-04-09 19:40发布

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

2条回答
Ridiculous、
2楼-- · 2019-04-09 19:55

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,
        )
    )
)
查看更多
我想做一个坏孩纸
3楼-- · 2019-04-09 20:17

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

查看更多
登录 后发表回答