title: Ubuntu Apache Using Wrong VHosts For Sites
body: <p>I'm using Ubuntu 16.0.4, and have vhost configured, with all .conf files located at <code>/etc/apache2/sites-available</code> and all enabled sites at <code>/etc/apache2/sites-enabled</code> using <code>a2ensite</code> for each site conf. I have 000-default.conf disabled, and I'm using certbot, along with the latest of apache and PHP 7.2.x.</p>
  
  <p>Two of my sites have an issue with the vhost configuration. The sites are:</p>
  
  <pre><code>Site 1: clients.site1.com.conf
  Site 2: clients.site2.com.conf
  Site 3: site3.com.conf (Mention later)
  </code></pre>
  
  <p>If I go to <code>clients.site1.com</code> in the browser, it loads fine.</p>
  
  <p>If I go to <code>clients.site2.com</code> in the browser, the url shows <code>clients.site2.com</code> loaded, but it is actually loading <code>clients.site1.com</code> content from <code>/var/www/clients.site1.com/public_html</code>.</p>
  
  <p>I've checked <code>clients.site1.com.conf</code>, and have the following:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerAdmin webmaster@clients.site1.com
      ServerName clients.site1.com
      ServerAlias clients.site1.com
      DocumentRoot /var/www/clients.site1.com/public_html
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  
      RewriteEngine on
      RewriteCond %{SERVER_NAME} =clients.site1.com
      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>I've checked <code>clients.site2.com.conf</code>, and have the following:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerAdmin webmaster@clients.site2.com
      ServerName clients.site2.com
      ServerAlias clients.site2.com
      DocumentRoot /var/www/clients.site2.com/public_html
  
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  
      RewriteEngine on
      RewriteCond %{SERVER_NAME} =clients.site2.com
      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>I have played around with the file name of the conf files thinking that having two conf files with <code>clients.</code> had an issue with the wildcard matching. So I tried removing the dot <code>.</code>, and even tried replacing it with a hyphen <code>-</code>. Still the same result.</p>
  
  <p>The last test I did was I disabled <code>clients.site1.com.conf</code>. When I did that and then proceeded with loading <code>clients.site2.com</code> in my browser, the next site in order of the conf files (Site 3 above to serve as an example) is what loads.</p>
  
  <p><b>UPDATE:</b></p>
  
  <p>I ran <code>apache2ctl -S</code> and got the following:</p>
  
  <pre><code>root@ip-111-111-111-111:/etc/apache2/sites-available# apache2ctl -S
  VirtualHost configuration:
  *:443 is a NameVirtualHost
  
   default server clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com-le-ssl.conf:2)
   port 443 namevhost clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com-le-ssl.conf:2)
           alias clients.site1.com
   port 443 namevhost site3.com (/etc/apache2/sites-enabled/site3.com-le-ssl.conf:2)
           alias www.site3.com
   port 443 namevhost aaaaa.com (/etc/apache2/sites-enabled/aaaaa.com-le-ssl.conf:2)
           alias www.aaaaa.com
   port 443 namevhost bbbbb.com (/etc/apache2/sites-enabled/bbbbb.com-le-ssl.conf:2)
           alias www.bbbbb.com
   port 443 namevhost ccccc.com (/etc/apache2/sites-enabled/ccccc.com-le-ssl.conf:2)
           alias www.ccccc.com
   port 443 namevhost ddddd.com (/etc/apache2/sites-enabled/ddddd.com-le-ssl.conf:2)
           alias www.ddddd.com
   port 443 namevhost eeeee.com (/etc/apache2/sites-enabled/eeeee.com-le-ssl.conf:2)
           alias www.eeeee.com
   port 443 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/fffff.com-le-ssl.conf:2)
           alias www.fffff.com
   port 443 namevhost ggggg.com (/etc/apache2/sites-enabled/ggggg.com-le-ssl.conf:2)
           alias www.ggggg.com
   port 443 namevhost site2.com (/etc/apache2/sites-enabled/site2.com-le-ssl.conf:2)
           alias www.site2.com
  *:80 is a NameVirtualHost
   default server ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/000-default.conf:1)
   port 80 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/000-default.conf:1)
   port 80 namevhost clients.site1.com (/etc/apache2/sites-enabled/clients.site1.com.conf:1)
           alias clients.site1.com
   port 80 namevhost clients.site2.com (/etc/apache2/sites-enabled/clients.site2.com.conf:1)
           alias clients.site2.com
   port 80 namevhost site3.com (/etc/apache2/sites-enabled/site3.com.conf:1)
           alias www.site3.com
   port 80 namevhost aaaaa.com (/etc/apache2/sites-enabled/aaaaa.com.conf:1)
           alias www.aaaaa.com
   port 80 namevhost bbbbb.com (/etc/apache2/sites-enabled/bbbbb.com.conf:1)
           alias www.bbbbb.com
   port 80 namevhost ccccc.com (/etc/apache2/sites-enabled/ccccc.com.conf:1)
           alias www.ccccc.com
   port 80 namevhost ddddd.com (/etc/apache2/sites-enabled/ddddd.com.conf:1)
           alias www.ddddd.com
   port 80 namevhost eeeee.com (/etc/apache2/sites-enabled/eeeee.com.conf:1)
           alias www.eeeee.com
   port 80 namevhost ip-111-111-111-111.ec2.internal (/etc/apache2/sites-enabled/fffff.com.conf:1)
           alias www.fffff.com
   port 80 namevhost ggggg.com (/etc/apache2/sites-enabled/ggggg.com.conf:1)
           alias www.ggggg.com
   port 80 namevhost site2.com (/etc/apache2/sites-enabled/site2.com.conf:1)
           alias www.site2.com
  
  ServerRoot: "/etc/apache2"
  Main DocumentRoot: "/var/www/html"
  Main ErrorLog: "/var/log/apache2/error.log"
  Mutex ssl-stapling: using_defaults
  Mutex ssl-cache: using_defaults
  Mutex default: dir="/var/lock/apache2" mechanism=fcntl
  Mutex mpm-accept: using_defaults
  Mutex watchdog-callback: using_defaults
  Mutex rewrite-map: using_defaults
  Mutex ssl-stapling-refresh: using_defaults
  PidFile: "/var/run/apache2/apache2.pid"
  Define: DUMP_VHOSTS
  Define: DUMP_RUN_CFG
  User: name="www-data" id=33
  Group: name="www-data" id=33
  root@ip-111-111-111-111:/etc/apache2/sites-available#
  </code></pre>