Question

running mutliple flask application

Posted February 20, 2015 15.1k 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
like
/var/www/App1
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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
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
    </Directory>

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

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

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

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

With a directory structure like this:

/var/www/
├── 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.

How the virtualhost will look like if each of the apps would have different domain names? Apache is not allowing two virtualhosts listening on the same port Number.

Submit an Answer