Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Running two Flask apps on two different domains using apache2 and WSGI

I have two python (3.6) Flask apps running on Ubuntu 18.04 and I am trying to use Apache2 (v2.4.29) to serve these two apps to two different domains – app1domain.com and app2domain.com. I have two .conf files that I have been trying to modify to get this to work. They currently look like this (replace app1 with app2 for the second one):

WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
WSGIScriptAlias / /var/www/app1/app.wsgi
WSGIProcessGroup app1
WSGIApplicationGroup %{GLOBAL}

<VirtualHost *:80>

    ServerAdmin myemail@outlook.com
    ServerName app1domain.com
    ServerAlias www.app1domain.com

    <Directory /var/www/app1>
        Require all granted
    </Directory>

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =app1domain.com [OR]
    RewriteCond %{SERVER_NAME} =www.app1domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

<VirtualHost *:443>

        ServerName app1domain.com
        ServerAlias www.app1domain.com
        ServerAdmin myemail@outlook.com

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

        Include /etc/letsencrypt/options-ssl-apache.conf

        SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Here is where I get stuck, and here is what I have tried:

  1. If both conf files are in the format shown above, app1 will be served to both https://app1domain.com and https://app2domain.com, and app2 will not be shown.
  2. If I disable the conf for app1, app2 is served to both https://app1domain.com and https://app2domain.com which suggests that, at a minimum, both conf files 'work' and the apps are working correctly.
  3. From my investigations, I see a lot of conf files have the WSGI instructions inside the <VirtualHost> tags. If I do this for both confs, the default Apache2 page is displayed on both domains.
  4. I have tried just about every combination of the WSGI instructions inside and outside the <VirtualHost> tag and also the nested <Directory> tag. Most of the just resulting in the default apache2 page.

Am I missing some other option that I need to change? What am I doing wrong here?

I have also been looking for some good documentation on how to interpret these conf files, what the options actually do, so would love if someone could point me to something, particularly if it covers WSGI.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Seems so obvious in hind sight. Turns out the issue was a result of me blindly trusting certbot to autogenerate the new VirtualHost and redirect, and me not really thinking about how this was working.

If you look at the example .conf file in the question, the reason it works is because the WSGI instructions are created outside the scope of the <VirtualHost> tags, which allows them to get picked up by both VirtualHosts. But at the same time, because they are created globally, the WSGI instructions in the .conf file that comes first alphabetically override the others, hence app1 shows up on app1domain.com and app2domain.com.

When I moved the WSGI instructions inside the <VirtualHost> tags, I was moving it inside the <VirtualHost *:80> tags, because all the examples I found were doing that (because they weren't using SSL). When I did that, instead of running the app, the RewriteEngine was redirecting the request to the https version of the website. That gets picked up by <VirtualHost *:443> where you will notice I had no instructions on how to run the app, so we get a default page.

In the end I rewrote my .conf files as follows:

<VirtualHost *:80>

    ServerAdmin myemail@outlook.com
    ServerName app1domain.com
    ServerAlias www.app1domain.com

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =app1domain.com [OR]
    RewriteCond %{SERVER_NAME} =www.app1domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

<VirtualHost *:443>

    ServerName app1domain.com
    ServerAlias www.app1domain.com
    ServerAdmin myemail@outlook.com

    WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
    WSGIScriptAlias / /var/www/app1/app.wsgi

    <Directory /var/www/app1>
        WSGIProcessGroup app1
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>

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

    Include /etc/letsencrypt/options-ssl-apache.conf

    SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem

</VirtualHost>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!