running mutliple flask application

February 20, 2015 2.2k views

Hello there , i have followed the tutorial here : https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps?comment=28202 , that is about how to deploy flask app on ubuntu and it works with me pretty fine.

but i'm wonder how to run multiple apps at the same time. when i enabled the virtual host the default one ( index.html for example diapered )
i want to give each application home directory
thus if my App1 route "/about" it will be domain.com/App1/about

i came from webfaction and i was familiar with the way the handle many applications at same host

2 Answers

If the two Flask apps are running on the same domain just as subfolders, then you only need one VirtualHost but you'll need multiple WSGIScriptAlias directives. Using the set up from the tutorial you linked as a base, your Apache configuration would look something like this:

WSGIPythonPath /var/www/FirstFlaskApp:/var/www/SecondFlaskApp

<VirtualHost *:80>
    ServerName domain.com
    ServerAdmin admin@domain.com

    WSGIScriptAlias /FirstFlaskApp /var/www/FirstFlaskApp/flaskapp.wsgi
    <Directory /var/www/FlaskApp/FlaskApp/>
            Order allow,deny
            Allow from all

    Alias /FirstFlaskApp/static /var/www/FirstFlaskApp/FirstFlaskApp/static
    <Directory /var/www/FirstFlaskApp/FirstFlaskApp/static/>
            Order allow,deny
            Allow from all

    WSGIScriptAlias /SecondFlaskApp /var/www/SecondFlaskApp/flaskapp.wsgi
    <Directory /var/www/FlaskApp/FlaskApp/>
            Order allow,deny
            Allow from all

    Alias /SecondFlaskApp/static /var/www/SecondFlaskApp/SecondFlaskApp/static
    <Directory /var/www/SecondFlaskApp/SecondFlaskApp/static/>
            Order allow,deny
            Allow from all

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined

With a directory structure like this:

├── FirstFlaskApp
│   ├── FirstFlaskApp
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── static
│   │   └── templates
│   └── flaskapp.wsgi
└── SecondFlaskApp
    ├── flaskapp.wsgi
    └── SecondFlaskApp
        ├── __init__.py
        ├── __init__.pyc
        ├── static
        └── templates
by Kundan Singh
Here's our guide on deploying a flask app on an Ubuntu VPS.
  • work great , but how about making it App1.domain.com , App2.domain.com instead of domain.com/App1 ,domain.com/App2
    there's no clear tutorial about how to sub-domain in DigitalOcean
    i have created A record with name test but it point to index (/var/www/index.html ) even though the configuration point to /var/www/test

  • thanks , i just figured it out :D

Have another answer? Share your knowledge.