RHEL: Apache, mod_wsgi daemon - Multiple Virtual Hosts defined but it always picks up the first one

158 views
Skip to first unread message

rm...@cornell.edu

unread,
Aug 12, 2017, 8:46:54 PM8/12/17
to modwsgi
Hello,

I am trying to setup two virtual hosts pertaining to two websites - lets call them www.A.com and www.B.com using apache, mod_wsgi daemon. Both the apps are written using flask.

Below is my apache conf file and for some reason it always picks the first declared virtual host URL and executes the first flask application for both URLs.

I am still new to the mod_wsgi scene and was reading multiple virtual hosts setup instructions from here - http://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html

Any idea how to get both of them working? The apache server is run as root.

Conf:
WSGIPythonHome /usr/local/venvs/myenv

<VirtualHost *:80>
  ServerName www.A.com

  WSGIDaemonProcess www.A.com threads=15 maximum-requests=10000

  WSGIScriptAlias / /var/www/A/A.wsgi
  WSGIProcessGroup www.A.com

  <Directory /var/www/Ar>
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

<VirtualHost *:80>
  ServerName www.B.com

  WSGIDaemonProcess www.B.com threads=15 maximum-requests=10000

  WSGIScriptAlias / /var/www/B/B.wsgi
  WSGIProcessGroup www.Bl.com

  <Directory /var/www/B>
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

Thanks,
RM


Graham Dumpleton

unread,
Aug 12, 2017, 8:49:43 PM8/12/17
to mod...@googlegroups.com
For general advice see:


See further comments below.

On 13 Aug 2017, at 9:45 am, <rm...@cornell.edu> <rm...@cornell.edu> wrote:

Hello,

I am trying to setup two virtual hosts pertaining to two websites - lets call them www.A.com and www.B.com using apache, mod_wsgi daemon. Both the apps are written using flask.

Below is my apache conf file and for some reason it always picks the first declared virtual host URL and executes the first flask application for both URLs.

What are the URLs you are using?

Does the hostname in the URL match exactly what you are setting ServerName to?

I am still new to the mod_wsgi scene and was reading multiple virtual hosts setup instructions from here - http://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html

Any idea how to get both of them working? The apache server is run as root.

Conf:
WSGIPythonHome /usr/local/venvs/myenv

<VirtualHost *:80>
  ServerName www.A.com

  WSGIDaemonProcess www.A.com threads=15 maximum-requests=10000

  WSGIScriptAlias / /var/www/A/A.wsgi
  WSGIProcessGroup www.A.com

  <Directory /var/www/Ar>
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

<VirtualHost *:80>
  ServerName www.B.com

  WSGIDaemonProcess www.B.com threads=15 maximum-requests=10000

  WSGIScriptAlias / /var/www/B/B.wsgi
  WSGIProcessGroup www.Bl.com

Do you mean www.B.com here. Not www.BI.com?


  <Directory /var/www/B>
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

Thanks,
RM



--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

rm...@cornell.edu

unread,
Aug 13, 2017, 12:13:56 AM8/13/17
to modwsgi
The URLs that I am using are two separate web domain addresses that I have purchased recently and set their DNS to point to my VPS's IP.

Yes, that was a typo (it was meant to be B) but I have verified that my actual conf file has no typos.

Each URL is tied to a flask application but with that config, only the first one gets invoked for both of them. I will like the first one to go to first flask application and the second url to hit the second one.

Thanks in advance for your help on this.

rm...@cornell.edu

unread,
Aug 13, 2017, 12:16:21 AM8/13/17
to modwsgi
Also, I checked the blog link below but that is a little different. Both the URLs used in there end with the same domain address (e.g. example.com). My case is a little different as both the URLs have different domain addresses. Not sure if that matters?


On Saturday, August 12, 2017 at 8:49:43 PM UTC-4, Graham Dumpleton wrote:

rm...@cornell.edu

unread,
Aug 13, 2017, 11:30:46 AM8/13/17
to modwsgi
Actually I figured it out, my config was missing -

NameVirtualHost *:80

After adding that, the virtual hosts are getting called correctly.

Thanks and Cheers!

Graham Dumpleton

unread,
Aug 13, 2017, 8:43:18 PM8/13/17
to mod...@googlegroups.com
That would suggest you are using very old version of Apache 2.2. That directive is not required in Apache 2.4.

Shane nunya

unread,
Jan 8, 2018, 2:20:48 AM1/8/18
to modwsgi
Hi

I've got a similar problem, that I'm not sure is covered by your blog post.

I have got 3 possible domains, and one alias, everything was working fine, apache/mod_wsgi served the correct content for the appropriate URL. However, I decided to clever (which is what *always* leads to my downfall) and added HTTPS to one of my domains. In the process I had a problem where the ACME client provided by letsencrypt had issues caused by it copying the *80 virtual hosts verbatim, therefore creating several mod_wsgi instances (one for each vhost) with the same name.

I found a SO post that suggested I switch to mod_wsgi as a global for the site, which fixed the issue, but now when I access one of the domains, the content from the default is loaded.

So the problem URL is git.mydomain.com, myotherdomain.nz is *fine*; I suspect because of the WSGIProcessGroup directive.

As I type this out I am wondering if the solution is to go back to an individual mod_wsgi process per vhost, but name each one differently, eg. https_mydomain and http_mydomain

##### Apache Conf file #####
LogFormat "%v - %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-vhost
WSGIApplicationGroup %{GLOBAL}
WSGIRestrictEmbedded On
WSGIScriptAlias / /var/www/vhosts/mydomain/mydomain/wsgi.py 
 
# adjust the following line to match your Python path 
WSGIDaemonProcess mydomain.com processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mydomain/venv/lib/python2.7
WSGIProcessGroup mydomain.com

<VirtualHost *:80> 
ServerName www.mydomain.com 
ServerAlias mydomain.com 
DocumentRoot /var/www/vhosts/mydomain
        CustomLog /var/log/apache2/mydomain.log combined-vhost
 
<directory /var/www/vhosts/mydomain> 
   AllowOverride none 
   Require all granted 
   Options FollowSymlinks 
</directory> 
 
Alias /static/ /var/www/vhosts/mydomain/mydomain/static/ 
 
<Directory /var/www/vhosts/mydomain/mydomain/static> 
          AllowOverride none
  Require all granted 
</Directory> 
</VirtualHost> 
<VirtualHost *:443> 
ServerName www.mydomain.com 
ServerAlias mydomain.com 
DocumentRoot /var/www/vhosts/mydomain
        CustomLog /var/log/apache2/mydomain_secure.log combined-vhost
 
<directory /var/www/vhosts/mydomain> 
   AllowOverride none 
   Require all granted 
   Options FollowSymlinks 
</directory> 
 
Alias /static/ /var/www/vhosts/mydomain/mydomain/static/ 
 
<Directory /var/www/vhosts/mydomain/mydomain/static> 
          AllowOverride none
  Require all granted 
</Directory> 
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/myotherdomain.nz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/myotherdomain.nz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost> 
<VirtualHost *:80>
    ServerName git.mydomain.com
    ServerAlias git.mydomain.com
    DocumentRoot /home/shane/code_repository/public
    CustomLog /var/log/apache2/git.log combined-vhost
    <Directory /home/shane/code_repository/public>
Options Indexes 
Require all granted
        AllowOverride none
    </Directory>
</VirtualHost>
##### END #####

Graham Dumpleton

unread,
Jan 8, 2018, 2:35:55 AM1/8/18
to mod...@googlegroups.com

On 8 Jan 2018, at 6:16 pm, Shane nunya <shan...@gmail.com> wrote:

Hi

I've got a similar problem, that I'm not sure is covered by your blog post.

I have got 3 possible domains, and one alias, everything was working fine, apache/mod_wsgi served the correct content for the appropriate URL. However, I decided to clever (which is what *always* leads to my downfall) and added HTTPS to one of my domains. In the process I had a problem where the ACME client provided by letsencrypt had issues caused by it copying the *80 virtual hosts verbatim, therefore creating several mod_wsgi instances (one for each vhost) with the same name.

I found a SO post that suggested I switch to mod_wsgi as a global for the site, which fixed the issue, but now when I access one of the domains, the content from the default is loaded.

So the problem URL is git.mydomain.com, myotherdomain.nz is *fine*; I suspect because of the WSGIProcessGroup directive.

As I type this out I am wondering if the solution is to go back to an individual mod_wsgi process per vhost, but name each one differently, eg. https_mydomain and http_mydomain

You definitely want separate mod_wsgi daemon process group for each separate site.

This is necessary as:

    WSGIApplicationGroup %{GLOBAL}

would cause everything to run in the same Python interpreter context of the daemon processes.

Some web frameworks can't handle mixing multiple sites in one interpreter, eg Django, plus you will have other problems with different sites need different versions of modules.

So the pattern you want is:

# Global stuff defined once.

WSGIApplicationGroup %{GLOBAL}
WSGIRestrictEmbedded On

# For first site.

WSGIDaemonProcess mysite1 processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mydomain/site1-venv

<VirtualHost *:80>
ServerName site1.com

WSGIScriptAlias / /some/path/site1/wsgi.py process-group=mysite1 application-group=%{GLOBAL}
</VirtualHost>

</VirtualHost *:443>
ServerName site1.com

WSGIScriptAlias / /some/path/site1/wsgi.py process-group=mysite1 application-group=%{GLOBAL}
</VirtualHost>

# For second site.

WSGIDaemonProcess mysite2 processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mydomain/site2-venv

<VirtualHost *:80>
ServerName site2.com

WSGIScriptAlias / /some/path/site2/wsgi.py process-group=mysite2 application-group=%{GLOBAL}
</VirtualHost>

</VirtualHost *:443>
ServerName site2.com

WSGIScriptAlias / /some/path/site2/wsgi.py process-group=mysite2 application-group=%{GLOBAL}
</VirtualHost>



##### Apache Conf file #####
LogFormat "%v - %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-vhost
WSGIApplicationGroup %{GLOBAL}
WSGIRestrictEmbedded On
WSGIScriptAlias / /var/www/vhosts/mydomain/mydomain/wsgi.py 
 
# adjust the following line to match your Python path 
WSGIDaemonProcess mydomain.com processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/vhosts/mydomain/venv/lib/python2.7

The value of python-home looks wrong.

It should be the root of Python virtual environment, which is what sys.prefix gives for interpreter. I would expect it to be '/var/www/vhosts/mydomain/venv'.

Not sure if that helps.

Graham

Shane nunya

unread,
Jan 8, 2018, 3:06:29 AM1/8/18
to modwsgi
Hi

So, that's fixed it, explicitly setting the script alias per vhost, now the problems are no longer bleeding into the (non-django serviced) additional vhost.

Thanks
Shane

Amber L

unread,
Jul 8, 2021, 7:53:00 PM7/8/21
to modwsgi
Hi, 

I also have a similar problem. I have two flask apps with separate domains but only the first is working properly. HTTPS only applies in the first domain app1.com. Below is the conf file. 

Any ideas on what I may be doing wrong? 


#  app1. com

<VirtualHost *:443>
    ServerName app1.com
    ServerAlias  app1.com *. app1.com
    ServerAdmin em...@example.com

   # WSGI Settings
    WSGIDaemonProcess  app1 user=apache threads=7 python-home=/var/www/app1/venv
    WSGIScriptAlias / /var/www/app1/wsgi.py process-group= app1 application-group=%{GLOBAL}
    WSGIScriptReloading On

   # Tell Apache where your app's code directory is
    DocumentRoot /var/www/app1
       
   # Logging 
   ErrorLog /var/www/app1/logs/app.log
   CustomLog /var/www/app1/logs/access.log combined
   LogLevel info ssl:warn

   # Relax Apache security settings
   <Directory /var/www/app1>
     <Files site.wsgi>
       Allow from all
       Options -MultiViews        
       Require all granted
     </Files>
   </Directory>

   Alias /static /var/www/app1/application/static
   <Directory /var/www/app1/application/static>
     Allow from all     
     Options -MultiViews
     Require all granted
   </Directory>


RewriteEngine on
RewriteCond %{SERVER_NAME} = app1.com [OR]
RewriteCond %{SERVER_NAME} =*. app1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/app1.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/app1.com/chain.pem

</VirtualHost>

<VirtualHost *:80>
ServerName app.1com

WSGIScriptAlias / /var/www/app1/wsgi.py  application-group=%{GLOBAL}
</VirtualHost>



<VirtualHost *:80>
    ServerName app2.com
    ServerAlias app2.com *.app2.com
    ServerAdmin em...@example.com

    # WSGI Settings
    WSGIDaemonProcess app2 user=apache threads=7 python-home=/var/www/app2/venv
    WSGIScriptAlias / /var/www/app2/wsgi.py process-group= app2 application-group=%{GLOBAL}
    WSGIScriptReloading On

    # Tell Apache where your app's code directory is
    DocumentRoot /var/www/app2 
       
   # Logging 
   ErrorLog /var/www/app2/logs/app.log
   CustomLog /var/www/app2/logs/access.log combined
   LogLevel info ssl:warn

    # Relax Apache security settings
    <Directory /var/www/app2>
      Allow from all
      Options -MultiViews
      # Uncomment this if you're on Apache >= 2.4:
      Require all granted
    </Directory>

    Alias /static /var/www/app2/application/static
    <Directory /var/www/app2/application/static>
      Allow from all
      Options -MultiViews
      Require all granted
    </Directory>


RewriteEngine on
RewriteCond %{SERVER_NAME} =app2.com [OR]
RewriteCond %{SERVER_NAME} =*. app2.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>


Thanks in advance for your help.

Graham Dumpleton

unread,
Jul 8, 2021, 7:56:27 PM7/8/21
to mod...@googlegroups.com
You aren't defining a VirtualHost for port 443 for app2.com.

In the app1.com you have VirtualHost for both port 80 and 443 with port 80 redirecting to 443.

You would need similar arrangement for app2.com.

Graham

Reply all
Reply to author
Forward
0 new messages