Reputation: 353
I'm trying out to deploy flask application with lighttpd and Fast-CGI. On starting the lighttpd server, HTML page is rendered on the browser, but corresponding CSS and images wasn't served by lighttpd.
Following are the minimal setup code snippet:-
Flask (File name: - example.py)
#!/usr/bin/env python
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route("/")
def hello():
return render_template('topic.html')
HTML(File name: - topic.html)
<!DOCTYPE html>
<body>
<img alt="logo" src="{{ url_for('static', filename='img/example.png') }}">
</body>
</html>
fcgi enabled WSGI (File name: - test.fcgi)
#!/usr/bin/env python
from flup.server.fcgi import WSGIServer
from example import app
if __name__ == '__main__':
WSGIServer(app, debug = True).run()
lighttpd config file
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_rewrite" )
server.modules += ( "mod_alias" )
server.modules += ( "mod_accesslog" )
server.document-root = "/var/www/testing/"
server.port = 5000
server.modules += ( "mod_cgi" )
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png",
".js" => "text/javascript",
".css" => "text/css"
)
cgi.assign = ( ".pl" => "/usr/bin/perl",
".cgi" => "/usr/bin/perl",
".rb" => "/usr/bin/ruby",
".erb" => "/usr/bin/eruby",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php-cgi" )
index-file.names += ( "index.pl", "default.pl",
"index.rb", "default.rb",
"index.erb", "default.erb",
"index.py", "default.py",
"index.php", "default.php" )
server.errorlog = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"
$HTTP["url"] =~ "^/" {
fastcgi.server = ("" =>
((
"socket" => "/tmp/test-fcgi.sock",
"bin-path" => "/var/www/testing/test.fcgi",
"check-local" => "disable",
"max-procs" => 1
))
)
}
Flask folder structure
/testing/
/example.py
/test.fcgi
/static/
/img/
/example.png
/template/
/topic.html
With flask development server things are working fine. But deploying with lighttpd, css and images are not served properly. What can be the proper method to deploy flask with lighttpd and Fast-CGI ?
Upvotes: 1
Views: 1066
Reputation: 353
With this updated lighttpd configuration file, css and images are served properly from lighttpd.
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_rewrite" )
server.modules += ( "mod_alias" )
server.modules += ( "mod_accesslog" )
server.document-root = "/var/www/testing/"
server.port = 5000
server.modules += ( "mod_cgi" )
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png",
".js" => "text/javascript",
".css" => "text/css"
)
cgi.assign = ( ".pl" => "/usr/bin/perl",
".cgi" => "/usr/bin/perl",
".rb" => "/usr/bin/ruby",
".erb" => "/usr/bin/eruby",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php-cgi" )
index-file.names += ( "index.pl", "default.pl",
"index.rb", "default.rb",
"index.erb", "default.erb",
"index.py", "default.py",
"index.php", "default.php" )
server.errorlog = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"
fastcgi.server = ("/test.fcgi" =>
((
"socket" => "/tmp/test-fcgi.sock",
"bin-path" => "/var/www/testing/test.fcgi",
"check-local" => "disable",
"max-procs" => 1
))
)
alias.url = (
"/static" => "/var/www/testing/static"
)
url.rewrite-once = (
"^(/static($|/.*))$" => "$1",
"^(/.*)$" => "/test.fcgi$1"
)
Upvotes: 2