sergiopereira
sergiopereira

Reputation: 2056

Run multiple independent Flask apps in Ubuntu

I'm trying to run two or more Flask applications in separate virtual directories with Apache, like http://localhost/site1 for /var/www/myapps/app1 and http://localhost/site2 for /var/www/myapps/app2. Each app has its own virtual environment under an env directory.

I started with a fresh install of Ubuntu 14.04 with Apache2 (v2.4.7), removed the default site configuration with sudo a2dissite 000-default and added configuration for my two apps.

Here's the conf file for app1 at /etc/apache2/sites-available/app1.conf. The cofiguration for app2 is identical, replacing app2 for app1 (and site2 for site1.)

<VirtualHost *:80>
    ServerName localhost
    WSGIProcessGroup site1
    WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-path=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
    WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
    WSGIScriptReloading On
    <Directory /var/www/mysites/app1>
        WSGIApplicationGroup site1
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Then I enabled each site with sudo a2ensite app1 (and app2) then restarted the server with sudo apache2ctl restart.

Each of these apps has the following application.wsgi file in the root dir:

# put a copy of this file in the root dir of each app instance
import os, sys
# activate the virtual environment
app_dir = os.path.dirname(__file__)
# removed next two lines after a comment left below
# activate_this = os.path.join(app_dir, 'env', 'bin', 'activate_this.py')
# execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, app_dir)
from myapp import app as application

What happens is that one of the sites runs fine, responding with the correct page when I hit it. The other gives me an Apache 404 page. There are no typos in the conf files.

It seems the configurations for the apps are clobbering each other and one is "winning." I've spent a lot of time tweaking the configurations but the only way I could make it work was if I added localhost2 to my hosts file and changed the ServerName to localhost2 in one of the app configs, which isn't desirable in my case. Can someone point me to the correct way to configure Apache? Or am I going the wrong way about this?

Ideally I'd want the configuration files to not care which host name was used to reach them, probably becoming multiple copies of this server running behind a load balancer.

Upvotes: 3

Views: 5745

Answers (1)

sergiopereira
sergiopereira

Reputation: 2056

I spent more time on this and, if I'm starting to understand Apache terminology and configuration a little better, I cannot use virtual hosts for this purpose. VirtualHost sections are intended for serving different host names (multiple domains or subdomains.)

For configuring parallel applications as subdirs I could have used Directory sections instead. I also didn't realize some of the WSGI* directives in the config file could appear more than once. This new knowledge allowed me to produce the following single config file that does what I wanted. So instead of enabling one Apache site for each app, I could enable a single site with the directories configured in it.

# this goes in /etc/apache2/sites-available/
<VirtualHost *:80>
    ServerName localhost

    # logs configuration
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
    WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
    <Directory /var/www/myapps/app1>
        WSGIApplicationGroup site1
        WSGIProcessGroup site1
        Order deny,allow
        Allow from all
    </Directory>

    WSGIDaemonProcess site2 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app2:/var/www/myapps/app2/env/lib/python2.7/site-packages
    WSGIScriptAlias /site2 /var/www/myapps/app2/application.wsgi
    <Directory /var/www/myapps/app2>
        WSGIApplicationGroup site2
        WSGIProcessGroup site2
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

EDIT:

I later followed Graham Dumpleton's suggestion and removed the activate_this stuff from application.wsgi and changed the WSGIDaemonProcess directive lines to:

WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1/env

Upvotes: 7

Related Questions