Running a Flask application in an Apache subdirectory

    I wanted to have a “main” Apache-served site with a Flask application running alongside it, but in a subdirectory. I.e. and This was much harder than expected.

    Most documentation I found for this issue told me to create a brand new “flaskapp.conf” file in /etc/apache2/sites-available/, this, of course, made the Flask application work, but broke my main site. That was not what I wanted. I wanted full cooperation between Flask and what I was already running.

    I obviously have all the prerequisites already installed and/or running (Linux, Python, Apache, Flask, libapache2-mod-wsgi, wsgi, etc.). To give you an idea of my environment, this is my file structure:


    Basically, what I had to do was to modify my existing /etc/apache2/sites-available/000-default.conf file. I added the following lines in it:

WSGIDaemonProcess flaskapp user=www-data group=www-data threads=5 home=/var/www/html/flaskapp
WSGIScriptAlias /flaskapp /var/www/html/flaskapp/flaskapp.wsgi

<Directory /var/www/html/flaskapp/flaskapp>
    WSGIProcessGroup flaskapp
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptReloading On
    Order deny,allow
    Allow from all

    Note that the .wsgi file must be executable by the user specified in the user parameter in the WSGIDaemonProcess line. Pay special attention to the second line (WSGIScriptAlias), most documentation told me to write it like this:

WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

    Doing it like that, would redirect ALL the traffic exclusively to the Flask application, and, again, the main site would stop working. As for my WSGI file (flaskapp.wsgi), these are its contents:

import sys
from flaskapp import app as application

After all the modifications were done, I restarted Apache:

service apache2 restart

    And finally, everything was working as expected. I am not sure if this is the preferred way to accomplish this, but it is working for me.

    Conclusion: I expected to accomplish this much more easily, and for Flask to be easier to configure with Apache since the default, standalone-Flask method is not even recommended for production environments. While troubleshooting, I found many people having the same problem (either with Apache, Nginx, or other webservers). I felt a little disappointed that I had to do all these “hacks” in order to make a Python web application running in one of the most popular webservers. It seems unrealistic to have the entirety of the webserver assigned exclusively to a Flask application. Let’s hope that new versions of Flask and Apache/Nginx offer a better compatibility between them.