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:

    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:

    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:

After all the modifications were done, I restarted Apache:

    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.


8 thoughts on “Running a Flask application in an Apache subdirectory”

  1. Fantastic Post… I wasted a whole bunch of time on this myself moving towards a solution and your site filled in that last 15% I was stuck on. It is a little frustrating how hacky this feels, but it’ll work for me for right now :/
    Thanks again.

  2. Another important thing to note is that your APP is going to map / to whatever you have in:
    WSGIScriptAlias /flaskapp /var/www/html/flaskapp/flaskapp.wsgi

    So in your APP, consider /flaskapp as /.

  3. Do you know whether it should work until now because I can set up flask in seperated conf file easily, but integrating it into an existing one only leads to “Not Found”.

Leave a Reply