16

I'm setting up a simple web server on my Raspberry Pi and I can't seem to set up lighttpd, fastcgi, and flask correctly.

By now, I've gone through a few iterations of /etc/lighttpd/lighttpd.conf, the most recent one being

fastcgi.server = ("/test" =>
    "test" => (
        "socket" => "/tmp/test-fcgi.sock",
        "bin-path" => "/var/www/py/test.fcgi",
        "check-local" => "disable"
    )
)

That spat out an error on /etc/init.d/lighttpd start. The first line looked wrong, so I added a set of parens after the fat arrow:

fastcgi.server = ("/test" => (
...
))

This didn't spit out an error, but when I tried to connect, I get ERR_CONNECTION_REFUSED in Chrome. Then I tried removing "/test" =>, and that had the same problem. I have also tried the config shown in this question, and the same problem occurred.

In /var/www/py/test.fgci:

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

WSGIServer(app, bindAddress="/tmp/test-fcgi.sock").run()

In /var/www/py/test.py:

from flask import Flask
app = Flask(__name__)

@app.route("/test")
def hello():
    return "<h1 style='color:red'>&#9773; hello, comrade &#9773;</h1>"

The current lighttpd.conf fails when I start it with /etc/init.d/lighttpd start.

Community
  • 1
  • 1
user1610406
  • 722
  • 1
  • 13
  • 24

2 Answers2

0

I can't really help you with the Python part as it is outside of my skillset, however when running php as a fcgi server i would use a lighttpd.conf like the following.

fastcgi.server += ( ".php" =>
    ((
        "host" => "127.0.0.1",
        "port" => "9000",
        "broken-scriptfilename" => "enable"
    ))
)

So I would assume that something like the following is what you need for python.

fastcgi.server += ( "/test" =>
    ((
        "socket" => "/tmp/test-fcgi.sock",
        "bin-path" => "/var/www/py/test.fcgi",
        "check-local" => "disable"
    ))
)
Kinetic
  • 1,714
  • 1
  • 13
  • 38
  • That's assuming that a separate process is running that handles these requests on port 9000. This is the same approach as `proxy-pass` in nginx. php-fpm already has that service running but for python, you need to install a dedicated server like `gunicorn` or `uwsgi` and configure it to run your app on that specified port. – Prahlad Yeri Jun 26 '19 at 02:26
0

Considering you received the error message ERR_CONNECTION_REFUSED, it looks like the problem might be related to incorrect authentication or limited permission rather than a syntax issue.

Possibility:

  1. Check the running port status of the lighttpd, fastcgi. (IPv6 check)
    netstat --listen

or lighttpd Logs.

  1. Did you realise that you wrote '/test' => 'test', just make sure the format as follows.
fastcgi.server = ("/hello.fcgi" =>
    ((
        "socket" => "/tmp/hello-fcgi.sock",
        "bin-path" => "/var/www/demoapp/hello.fcgi",
        "check-local" => "disable",
        "max-procs" => 1 
    ))
)
  1. You need to add this line 'if name == 'main' in to /var/www/py/test.fcgi due to the app imported from yourapplication.
#!/usr/bin/python
from flup.server.fcgi import WSGIServer
from yourapplication import app

if __name__ == '__main__':
    WSGIServer(app, bindAddress="/tmp/test-fcgi.sock").run()

Here is the reference.

Please make sure in advance that any app.run() calls you might have in your application file are inside an if name == 'main': block or moved to a separate file. Just make sure it’s not called because this will always start a local WSGI server which we do not want if we deploy that application to FastCGI.

https://flask.palletsprojects.com/en/2.0.x/deploying/fastcgi/

Once you've checked all possibilities, let's see what happens.

xPetersue
  • 21
  • 4