0

I have several domains which refer to one and the same server. Now I want to create a Vhost for each domain. This already works for some domains, but not for all.

Example: I navigate to the domain "b.com" and get the website which should be displayed for "a.com". With "a.com", however, everything works. The request from "b.com" is also saved in the log from "a.com" and there it also indicates that I have navigated to "a.com".

The DNS management is at Cloudflare. Each domain has a TXT record with its domain name.

Can you help me?

I have minified my configurations to the minimum. So thats the 2 files.

First config:

<VirtualHost a.com:80>
  Servername a.com
  DocumentRoot /var/www/a.com
</VirtualHost>

Second config:

<VirtualHost b.com:80>
  Servername b.com
  DocumentRoot /var/www/b.com
</VirtualHost>

Thank you in advance!

  • Can you please send me an example, how to use it in my case? – CaveFire May 22 '19 at 7:16
  • @EugenRieck: Note that the NameVirtualHost option is only for the obsolete Apache 2.2; the functionality is always on in 2.4 or later. – user1686 May 22 '19 at 7:32
  • @CaveFire: Could you explain this part: "Each domain has a TXT record with its domain name" – why is the TXT record there, and what is it supposed to achieve? – user1686 May 22 '19 at 7:33
  • @grawity Thanks! I guess my age shows ... BTW: It still works on a fully up-to-date Apache – Eugen Rieck May 22 '19 at 7:38
  • 1
    @grawity: I just checked (to confirm my sanity) and it still is in the german (my native language) Apache docs, but no longer in the english version. – Eugen Rieck May 22 '19 at 7:44
1

The parameter for <VirtualHost ...> tells the web server which local IP address to bind to. Even if you specify a name, it is only used to resolve an IP address. (For example, it is used when the server has many IP addresses and each domain has a dedicated IP address.)

Most vhosts are name-based, not address-based, so this parameter is useless and you should just specify the wildcard address * in its place. Instead, the Apache webserver distinguishes vhosts by their ServerName and ServerAlias parameters.

A standard name-based vhost configuration looks like this:

<VirtualHost *:80>
    ServerName a.example.tld
    DocumentRoot /var/www/a
</VirtualHost>

<VirtualHost *:443>
    ServerName a.example.tld
    DocumentRoot /var/www/a
    SSLEngine On
    SSLCertificateFile [...]
</VirtualHost>

---

<VirtualHost *:80>
    ServerName b.example.tld
    DocumentRoot /var/www/b
</VirtualHost>

<VirtualHost *:443>
    ServerName b.example.tld
    DocumentRoot /var/www/b
    SSLEngine On
    SSLCertificateFile [...]
</VirtualHost>

If the server is supposed to provide both HTTP and HTTPS, then you still need to define the vhost for each port (:80 and :443). Whether you need this or not possibly depends on your Cloudflare configuration.

If you are using old Apache versions such as v2.2 or older, you additionally need these options (they no longer exist in v2.4 or newer):

NameVirtualHost *:80
NameVirtualHost *:443
| improve this answer | |
  • Thank you very much. Its working now. But if I open a page, which is not entered in the config, it shows me the first config, he gets. Can I add a "default" vhost? – CaveFire May 22 '19 at 8:32
  • Yes, and you've already discovered how: Make it the first vhost. (The Apache document linked previously says just about the same thing, too.) For example, Debian even names the default config file 000default.conf just so that it'd always be first. – user1686 May 22 '19 at 8:35
  • Your right. Thank you :) – CaveFire May 22 '19 at 8:47

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.