1

Although I know that this topic has been already discussed in some other threads where it is explained how to configure Virtualhosts in Apache. The thing is after looking for possible answers to my problem here, researching in Google and reading Apache official documentation I cannot have my problem solved.

Firstly, I had one WordPress installation running on Apache 2.4 installed in CentOS 7 and I set up the following Virtualhost:

<VirtualHost *:80>

ServerName subdomain1.domain.com
ServerAlias www.subdomain1.domain.com
DocumentRoot /some/path/to/wordpress

<Directory "/some/path/to/wordpress">
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/subdomain1-error.log"
CustomLog "logs/subdomain1-access.log" combined      
LogLevel warn

</VirtualHost>

It worked fine but then I decided to add a Moodle installation (because the server doesn't have too much traffic) and then if I browse in Chrome subdomain1.domain.com or subdomain2.domain.com it doesn't matter, Chrome always goes to subdomain2.domain.com (Moodle website).

I tried in Chrome and Firefox with same results. The moodle Virtualhost is:

<VirtualHost *:80>

ServerName subdomain2.domain.com
ServerAlias www.subdomain2.domain.com
DocumentRoot /some/path/to/moodle

<Directory "/some/path/to/moodle">
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

<Files ".ht*">
   Require all denied
</Files>

ErrorLog "logs/subdomain2-error.log"
CustomLog "logs/subdomain2-access.log" combined
LogLevel warn

</VirtualHost>

Also, the Apache configuration file httpd.conf includes these VirtualHosts using the directive: IncludeOptional conf.d/*.conf

The file /etc/hosts is:

127.0.0.1   localhost 

134.122.191.221 subdomain1.domain.com

134.122.191.221 subdomain2.domain.com

I cannot see any obvious error in my Apache Virtualhosts configuration files so I would appreciate if someone could give me a hint about what it is wrong and any possible error.

Thanks a lot.

1 Answer 1

3

You are trying to setup Name-based Virtual Host and missing an important config:

NameVirtualHost *:80

So, add the above line in your httpd.conf and restart apache/httpd, and it should solve the problem.

N.B. According to apache 2.4 documentation, this directive is deprecated and no longer needed.

Prior to 2.3.11, NameVirtualHost was required to instruct the server that a particular IP address and port combination was usable as a name-based virtual host. In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address.

But I'm yet to find out why it works with the above directive.

5
  • Thank you for your previous answer. You are absolutely right but I should have been more specific because each one are inside blocks:<VirtualHost *:80> </VirtualHost> After reading your previous response, I created two different site files but unfortunately I still get same results. Can it be because both block headers are the same and then take one of them as default? (In my case the second one)
    – jocag
    Feb 17, 2016 at 11:12
  • @castle10, can you add the line NameVirtualHost *:80 in your httpd.conf (and restart apache), if it's not already there.
    – Diamond
    Feb 17, 2016 at 11:21
  • Thanks @bangal Apparently, it was the key! Honestly, I didn't add this directive because I got this message: AH00548: NameVirtualHost has no effect and will be removed in the next release but it made it works!
    – jocag
    Feb 17, 2016 at 11:34
  • @castle10, according to apache 2.4 documentation this entry is not needed and deprecated, and that's why you are getting the warning. Can you check if you have a Listen 80 or similar directive in your httpd.conf file?
    – Diamond
    Feb 17, 2016 at 12:00
  • Yes, I definitively have one and if I use netstat I can see that Apache is listening on port 80.
    – jocag
    Feb 17, 2016 at 12:03

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.