How to get apache to serve static files on Flask webapp

I get an internal 500 error trying to get Apache to serve my static files.

The application will be locally hosted (not facing WWW). There will be no DNS to resolve the name "www.domain.com". I want to have access to the application by entering the IP address of the server when I am on this network.

This is my httpd.conf file (I am on RHEL):

<Directory /var/www/testapp> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /var/www/testapp/service.wsgi 

If I changed WSGIScriptAlias ​​to WGSIScriptAlias /test /var/www/testapp/service.wsgi , then I can view my static files when entering IP, but I still can’t access the service.py script from [IP ] / test.

In any case, I want to be able to serve all GET / POST requests using service.py script, so I want my alias to start with / , and not elsewhere.

All my static files are in / var / www / html (Apache automatically displayed these files before I messed up with httpd.conf, now I just get 500).

This is my service.wsgi:

 import sys sys.path.insert(0, '/var/www/testapp') from service import app as application 

This is my service.py:

 from flask import Flask app = Flask(__name__) @app.route("/") def hello(environ, start_response): status = '200 OK' output = "Hello" response_headers = [('Content-type', 'text/plain'), ('Content-length', str(len(output)))] start_response(status, response_headers) return output if __name__=='__main__' app.run() 

Do I need to store my .wsgi files in the / var / www / html directory? Or can they go to another folder? I see that there may be a conflict between the message that I send to the server ("Hello") and the static files that are already in the / var / www / html / directory. So I tried setting the alias to /test , but that didn't work either.

I just want my Flask application to serve GET / POST requests and require apache to serve all static files.

+5
source share
1 answer

Fix 500 errors

You are currently getting 500 errors because your handler is the main WSGI handler, but flag handlers are not WSGI handlers (Flask / Werkzeug is an abstract for you). Change the handler to:

 @app.route("/") def hello(): return "Hello" 

and 500 errors should disappear.

Serving Static Files with Apache

The following methods can be used when your application maintains a domain root ( / ), depending on whether you WSGIScriptAlias or AddHandler .

When using WSGIScriptAlias

When using WSGIScriptAlias to install a WSGI application on / you can use the Apache Alias directive to ensure that certain subitems are not processed by WSGIScriptAlias (this is further described in the mod_wsgi wiki as well ):

 Alias "/static/" "/path/to/app/static/" <Directory "/path/to/app/static/"> Order allow,deny Allow from all </Directory> 

If you also want to support old static folders, you will also need to use the AliasMatch directive:

 AliasMatch "(?i)^/([^/]+)/static/(.*)$" "/path/to/app/blueprints-root/$1/static/$2" <Directory ~ "/path/to/app/blueprints-root/[^/]+/static/.*"> Order allow,deny Allow from all </Directory> 

See also: Directory .

When using AddHandler

As Graham Dumpleton noted in the comments, you can use mod_rewrite to pass requests to Python if and only if the file does not exist in DocumentRoot . Quoting from related documents:

When using the AddHandler directive with WSGI applications identified by the script file extension, the only way to make the WSGI application appear as the server root is to rewrite the URL internal to Apache using mod_rewrite on the fly. The necessary rules for mod_rewrite are to ensure that the WSGI application implemented in the script 'site.wsgi' in the root directory of the virtual host appears as mounted in the root of the virtual host:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /site.wsgi/$1 [QSA,PT,L] 

Note, however, that when the WSGI application is running for the request, the variable "SCRIPT_NAME" indicating which mount point the application was would be /site.wsgi. This will mean that when the WSGI application creates an absolute URL based on "SCRIPT_NAME", it will include "site.wsgi" in the URL rather than hiding it. Since this is likely to be undesirable, many web frameworks provide the ability to override the value for a mount point. If this configuration option is not available, it is also easy to configure the value "SCRIPT_NAME" in the file "site.wsgi" script.

 from your.app import app # Your Flask app import posixpath def application(environ, start_response): # Wrapper to set SCRIPT_NAME to actual mount point. environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME']) if environ['SCRIPT_NAME'] == '/': environ['SCRIPT_NAME'] = '' return app(environ, start_response) 

This shell ensures that "site.wsgi" will never be displayed in the URL until it is included first, and that access is always through the root of the website.

+8
source

All Articles