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:
<VirtualHost> tags. If I do this for both confs, the default Apache2 page is displayed on both domains.<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.
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>