Seting Up Multiple Flask Apps

Question:How do I set up a website that runs mainly on Python and the Flask framework and consists of several unrelated Flask “apps” – and for good measure also a few PHP-based pages? Also, I’m using Ubuntu and Apache2. And I want to host all of this on DigitalOcean.

Long answer (in the form of a series of annotated links with a brief introductory story which you may want to skip if you’re just looking for the actual answer):

When I started working on my personal website, I took over my wife’s Bluehost account. She was working on a 90s nostalgia site; I actually came up with a great name for it: the90scalled.com (I don’t recommend visiting this URL as the project died just before I started using my wife’s account and the domain has been expired for a while – there could be anything on it right now).

Anyway.

My first few projects were mainly done with front end technologies, with occasional PHP code. Well, some of my mini-projects relied on PHP more than others, so eventually I ended up with enough legacy PHP code.

I decided to try Python (with the Flask framework) and Bluehost suddenly didn’t give me enough freedom for that. I wanted to give DigitalOcean a try as it seemed perfect – a VPS (so that I could install and run pretty much anything) and very cheap (I’m currently using their cheapest, \$5/month “droplet” for all my sites – fourtonfish.com, spaceandquotes.com and simplesharingbuttons.com – the last of which can get up to 10,000 views a month, so it seems this is a pretty good setup as I haven’t run into any problems with uptime, bandwidth or anything else.

Originally I followed the “official” way of managing large Flask apps (my actual website being one large app). This worked really well until my code base started to grow and I also started using custom domains for some of the “apps” – things spiraled out of control pretty quickly.

A much better solution, as I learned, is to set up each Flask app to run independently and let the server manage the low-level stuff, like most of the routing.

So what you’ll want to do is to create a configuration file mywebsite.conf inside /etc/apache2/sites-available. Then you can enable your mywebsite site with:

a2ensite mywebsite

Note: I am assuming here that you already have a basic understanding of how Apache works and can look up some of the details yourself; if you need an overview, you can read this tutorial on how to set up Apache virtual hosts – or use Google.

Here is a very simplified example of what your mywebsite.conf file could look like. It assumes you want to have your main Flask app handle your root directory and your projects handled separately in sub-folders.