How to deploy flask app on lighttpd server on yocto linux using flup , fastCGI package for production

216 Views Asked by At

I am trying deploy the flask app on production on lighttpd webserver. I m using yocto based linux OS.

The folder structure is mentioned below:

/my_project/
    /app.py  
    /app.fcgi  
    /static/
    /templates/
        /index.html

The details of each files are mentioned below:

The flask app python file (app.py)

#!/usr/bin/env python3
from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('index.html')

The fcgi file (app.fcgi)

#!/usr/bin/env python3
from flup.server.fcgi import WSGIServer
from app import app

class ScriptNameStripper(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = ''
        return self.app(environ, start_response)

app = ScriptNameStripper(app)

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

The html index file(index.html)

<!DOCTYPE html>
<html>
    <body>Welcome to flask deployment on lighttpd</body>
</html>

The lighttpd.conf file (/etc/lighttpd.conf)

server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )
server.modules   += ( "mod_alias" )
server.modules   += ( "mod_accesslog" )
server.modules   += ( "mod_access" )
server.modules   += ( "mod_cgi" )

server.document-root        = "/home/root/my_project"

server.errorlog             = "/www/logs/lighttpd.error.log"

index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm" )


mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
 )


accesslog.filename          = "/www/logs/access.log"
debug.log-request-handling = "enable"


url.access-deny             = ( "~", ".inc" )

$HTTP["url"] =~ "\.pdf$" {
  server.range-requests = "disable"
}

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi",".py", ".pyc" )


server.port                = 9000

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                               ".cgi" => "/usr/bin/perl",
                               ".php" => "/usr/bin/php",
                               ".py"  => "/usr/bin/python" )

fastcgi.server = ("/" =>
                    ((
    "socket" => "/tmp/my_project-fcgi.sock",
    "bin-path" => "/home/root/my_project/app.fcgi",
    "check-local" => "disable",
    "max-procs" => 1
))
)

alias.url = (
        "/static" => "/home/root/my_project/static"
    )

url.rewrite-once = (
        "^(/static($|/.*))$" => "$1",
        "^(/.*)$" => "/app.fcgi$1",
    )



I did all the changes and restarted the lighttpd server, but when i am trying to access the server, its not rendering index.html file.

Is any thing missing, or need to make some changes in the files? What are the proper steps to deploy the flask app on lighttpd server on yocto linux OS?

1

There are 1 best solutions below

0
gstrauss On

I think you want fastcgi.server = ("/app.fcgi" ... rather than sending everything ("/") to the fastcgi server.