1

I'm configuring an apache server to support a few virtual hosts with different subdomains. Situation:

Right now, there are 4 subdomains pointing to same IP:

srv.example.com
svn.example.com
mail.example.com
mysql.example.com

List will be extended later with other names. First one, should load default vhost with something like Welcome page. Second one used in vhost serving svn repos, third one a webmail client and last one phpmyadmin.

Next, i have 4 sites enabled:

000-default.conf -> ../sites-available/000-default.conf
010-svn.conf -> ../sites-available/010-svn.conf
020-roundcube.conf -> ../sites-available/020-roundcube.conf
090-phpmyadmin.conf -> ../sites-available/090-phpmyadmin.conf

First one is a default apache2 config:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Second, svn repos host configuration:

<VirtualHost 192.168.22.11:38443>
  ServerName svn.example.com
  <Location />
    ... SVN stuff ...
  </Location>
  ... SSL stuff ...
</VirtualHost>

Webmail vhost:

<VirtualHost *:443>
  ServerName mail.example.com
  DocumentRoot /home/webapps/roundcubemail-1.1.2

  ... SSL stuff ...

  <Directory "/home/webapps/roundcubemail-1.1.2">
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName mail.example.com
  Redirect / https://mail.example.com/
</VirtualHost>

And phpmyadmin:

<VirtualHost 192.168.22.11:80>
  ServerName mysql.example.com
  DocumentRoot /usr/share/phpmyadmin
  <Directory />
    Options FollowSymLinks
    AllowOverride None
  </Directory>
  LogLevel notice
  CustomLog /var/log/apache2/access.log combined
  ErrorLog /var/log/apache2/error.log
  ServerSignature On 
</VirtualHost>

So, again, its supposed to:

http://srv.example.com => land on 'default' vhost (000-def...)
http://svn.example.com => land on 'default' as SVN is served on 38443 port
http://svn.example.com:38443 => land on svn vhost
http://mail.example.com => redirect to https://mail.example.com
https://mail.example.com => land on webmail vhost
http://mysql.example.com => land on phpmyadmin vhost

But, its no working like this:

http://srv.example.com => loads phpmyadmin vhost
http://svn.example.com => loads phpmyadmin vhost too
http://svn.example.com:38443 => lands on svn vhost
http://mail.example.com => loads phpmyadmin vhost
https://mail.example.com => land on webmail vhost
http://mysql.example.com => land on phpmyadmin vhost

So, generally, apache2 is taking last vhost (mysql) as default one. And for some reason mail redirect is not working.

I have no idea whats wrong, as i googled some, it should work, when subdomain doesn't fit to any of defined vhosts, it should load first one (000-default as from order of symlinks) - so why it loads last one?

Edit 11.09.2015

Problem seems to be in phpmyadmin configuration, when i disable its vhost, everything works as expected, and when i enable it again its bad again.

Dont know why this happens...

4 Answers 4

0

If you don't have a servername in the default vhost, you will just get a warning that apache can not reliably determine the hostname on every Apache restart or reload.

Did you restart or rload Apache after changing the config ?

Does the server actually run on 192.168.22.11 and how did you assign that ip-number to the hostnames when testing?

Are you using the /etc/hosts file for that purpose?

Make sure that instead of a hosts file line:

127.0.0.1 srv.example.com svn.example.com mysql.example.com

You have:

192.168.22.11 srv.example.com svn.example.com mysql.example.com

Or change the vhost from 192.168.22.11:80 to *:80

1
  • Server machine has 3 network interfaces and 192.168.22.0/24 subnet is on one of them. Both 'internal' networks are configured statically, 3rd one is dynamic. And yes, subdomains are defined in /etc/hosts by the second way you described and then distributed to all other LAN clients by dhcp, so all machines ale properly resolving all subdomains.
    – WombaT
    Sep 11, 2015 at 11:28
0

Well, after a lot of blind shot tests im still not sure what was exactly the problem, but i got it working as i desired by changing phpmyadmin configuration to not bind to specific interface but to all.

Now i have <VirtualHost *:80> instead of <VirtualHost 192.168.22.11:80>. That change "fixed" apache behavior in picking up right virtual hosts but resulted in a availability of phpmyadmin on all subnets - not so good.

Then i thought that binding vhost to only one subnet to prevent users from other subnets from accessing it wasnt best idea and maybe it would be better to set properly apache permissions? With that in mind after a few minutes a got added this to vhost conf:

<Directory "/www/webapps/adminer">
    Require ip 192.168.22.0/25
</Directory>

(and yes, in the meantime i switched phpmyadmin to adminer)

And now everything works as expected. Only one thing that im thinking about now is that maybe it would be better for users to see theres no such thing (404) instead of there is something, but you cannot access it (forbidden) but its a topic for another question i guess....

0

Couple of things jump out at me:

Is it supposed to be <VirtualHost 192.168.22.11:38443> ?

Not <VirtualHost *:38443> ?

and there's no ServerName in your default either. I tend to generally have one.

----- EDIT -----

If you are trying to restrict who can see the site then you would need to set up rules about that INSIDE the VirtualHost declaration:

<Location />
    Order Allow,Deny
    Allow from 123.123.123.0/24
</Location>
3
  • I agree with the ServerName and think that's probably the issue. The IP address is valid in the VirtualHost statement to listen only on a specific interface (and it's one of his servers that was originally working) Sep 10, 2015 at 22:47
  • @Andrew Myers: yes, its supposed to be with certain IP address as that vhost should be accessible only on this subnet.
    – WombaT
    Sep 11, 2015 at 11:25
  • @WombaT Please see my recent edit. Oct 5, 2015 at 12:16
0

I've observed the misbehavior mentioned in the original question: as soon as I enabled the PhpMyAdmin VirtualHost, it would override (hijack) my default VirtualHost.

The default VirtualHost is normally inferred from the order of the VirtualHost entries in Apache's config. The first VirtualHost becomes the default. That's why in the multi-file config, where apache.conf just includes all the site.conf files, the default VirtualHost's filename is often called 000-default or something like that.

In my case, originally I had all the VirtualHost entries declared as

<VirtualHost *:80>
     ServerName ...

and the default VirtualHost would get inferred just fine. After adding a VirtualHost for PhpMyAdmin, it would become the default.

And, contrary to the accepted solution, I had to rework the VirtualHost declarations to include a particular IP address. Which is where the "first VirtualHost to occur in config" took over again. I had to declare them all with an IP address : the default VirtualHost, the PhpMyAdmin VirtualHost, and all the other VirtualHosts that I have.

Apache httpd version: 2.4.10-10+deb8u16

Seems as if mod_php loads as a global extension to httpd and gets precedence over the standard "selection of default" on part of stock httpd...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.