32

I'm trying to add a second virtual host to my apache configuration, but cannot seem to get the new virtual host to be used.

My httpd.conf just contains the following line:

ServerName radiofreebrighton.org.uk

I also have a ports.conf file, which contains the following:

NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    Listen 443
</IfModule>

I have two files in sites-available which were symlinked to sites-enabled by a2ensite:

  • radiofreebrighton.org.uk
  • trafalgararches.co.uk

The contents of the first is:

<VirtualHost _default_:80>
    DocumentRoot /home/tom/www

    ServerAdmin [email protected]
    ServerName radiofreebrighton.org.uk
    ServerAlias www.radiofreebrighton.org.uk

    <Directory /home/tom/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log
    LogLevel error
    CustomLog /var/log/apache2/access.log combined

    Alias /wiki /home/tom/www/mediawiki/index.php
</VirtualHost>

The contents of the latter is:

<VirtualHost *:80>
    DocumentRoot /home/tom/tata-www

    ServerAdmin [email protected]
    ServerName trafalgararches.co.uk
    ServerAlias www.trafalgararches.co.uk

    <Directory /home/tom/tata-www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    logLevel error
    ErrorLog /var/log/apache2/error.log
</VirtualHost>

But any time I request a page from trafalgararches.co.uk, I am given a page from radiofreebrighton.org.uk. Why might this be happening? How can I fix it?


Edit:

Virtual host configuration as understood by apache:

tom@rfb:/usr/local$ apache2ctl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server radiofreebrighton.org.uk (/etc/apache2/sites-enabled/radiofreebrighton.org.uk:1)
         port 80 namevhost radiofreebrighton.org.uk (/etc/apache2/sites-enabled/radiofreebrighton.org.uk:1)
         port 80 namevhost trafalgararches.co.uk (/etc/apache2/sites-enabled/trafalgararches.co.uk:1)
Syntax OK

(Gleaned via apache2ctl -S aka httpd -S.)

13
  • 1
    Not sure this is the cause, but you ought to remove the slashes from the ends of your ServerName and ServerAlias lines. Also, make sure you've restarted apache.
    – EEAA
    Jul 8, 2011 at 12:53
  • I'm almost positive that's the cause. It means the hostname in the request will never match the ServerName for that virtual host.
    – larsks
    Jul 8, 2011 at 12:57
  • 1
    @ErikA, @larsks - You guys got my hopes up! I removed the trailing slashes and restarted apache, but it didn't change anything.
    – Tom Wright
    Jul 8, 2011 at 13:12
  • 1
    Do you have NameVirtualHost *:80 somewhere in your configuration?
    – larsks
    Jul 8, 2011 at 13:15
  • 1
    I am not sure if the sites mentioned above are production or development, but actually going to those URLs does give two different pages for me (correct pages by the looks of it). If the vhosts above are on a development server, ignore this comment. Otherwise, you might have fixed your problem somewhere along the way and still have a cached copy sitting around.
    – cyberx86
    Jul 8, 2011 at 17:30

10 Answers 10

18

This may be obvious, but don't forget to restart the apache service after enabling additional virtual host.

After executing a2ensite for the second virtual host, the output of apache2ctl -S reflects that both sites are configured (and one of them is the default), but it won't be live until you reload apache.


Let's say you have two virtual hosts - site1 and site2. You run a2ensite site1 and then reload apache service. Now you can access http://site1 and it is the default. Now you run a2ensite site2, but forget to restart apache. The output of apache2ctl -S will be:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server site1 (/etc/apache2/sites-enabled/site1:1)
         port 80 namevhost site1 (/etc/apache2/sites-enabled/site1:1)
         port 80 namevhost site2 (/etc/apache2/sites-enabled/site2:1)
Syntax OK

But when you try to load http://site2, it will actually load the default site (site1), since the configuration isn't loaded.

1
  • I get the same output but even then it doesn't load the second website.
    – arqam
    Sep 24, 2019 at 10:08
16

I had a similar problem where my additional vhosts on port 443 (SSL/HTTPS) were all being directed to the directory of the first vhost listed. Apache was essentially ignoring the servername property and matching on the ip:port only.

Turns out that I was missing the command 'NameVirtualHost *:443' to enable Named virtual hosting for port 443.

'NameVirtualHost *:443' just needs to be called once, and must be defined above your vhosts for port 443. I put my definition in the ports.config file so it looks like:

NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    NameVirtualHost *:443
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    NameVirtualHost *:443
    Listen 443
</IfModule>

Don't forget to restart apache after any changes.

1
  • 4
    For what it's worth... in Apache 2.4.18, using NameVirtualHost produces this message on startup: AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/ports.conf
    – Lambart
    May 23, 2017 at 19:49
4

My 2 cents: as I have to stick with an IP (I don't want the site to be served on all networks installed), it happened that after the local private IP of the server changed, I forgot to change it here:

NameVirtualHost 192.168.100.20:80 <VirtualHost 192.168.100.20:80>

Of course it's not an Apache problem to let you know that IP does not exist locally.

2

Tom, please look here http://httpd.apache.org/docs/2.0/en/mod/core.html#namevirtualhost

Note

Note, that the "main server" and any default servers will never be served for a request to a NameVirtualHost IP address (unless for some reason you specify NameVirtualHost but then don't define any VirtualHosts for that address).

So it should be okay if you change the default to the ip-adress of your server.

1
  • I tried that, but it didn't change anything. I'd also like to avoid tying myself to a particular IP, so I'm going to change it back. Also, I should point out that the default vhost is the one that works.
    – Tom Wright
    Jul 8, 2011 at 13:52
2

I discovered the source of this problem was an /etc/hosts entry on my server with the URL in it pointing to the server's external IP.

At one point I must have been setting it up before DNS was ready so I entered a /etc/hosts entry on my server pointing to its own external IP:

1.2.3.4  vhost.example.com

Then i set up a ServerAlias to an existing site for "vhost.example.com"

But nothing I could do would stop Apache serving up the default-ssl.conf site for SSL requests to vhost.example.com. Port 80 HTTP worked OK, but the SSL always showed the default site instead. In the end this SO thread led me to try "apachectl -S" which shows sites and finally I was able to figure it out.

So if you're getting the default SSL site instead of the site you're expecting, make sure you didn't add your server's external IP address in a /etc/hosts entry! A pretty weird thing to have done in hindsight, but hopefully this helps someone else!

2
  • Doesn't fix it for me (Apache 2.2 on CentOS.) Feb 21, 2020 at 8:36
  • 1
    Or add the server name DNS entry (which should show the default) before the the vhost entry into the /etc/hosts too. That solved it for me, so in /etc/hosts then: 1.2.3.4 defaultnameoftheserver.example.com → is used by apache as default then 1.2.3.4 vhost.example.com → is used by apache only as vhost then May 25, 2021 at 7:15
1

I find answer from here: http://alexking.org/blog/2007/11/01/apache-2-only-serves-first-virtual-host

Put 2 servername in same 1 VirtualHost tag as below:

<VirtualHost *:80>
ServerName beta-site-1.com
DocumentRoot "/Library/WebServer/beta-site-1"

ServerName beta-site-2.com
DocumentRoot "/Library/WebServer/beta-site-2"
</VirtualHost>

I ended up having issues with the second site because I had two VirtualHost tag blocks.

1
  • This post is probably wrong, the documentation and examples clearly show multiple virtualhost directives, probably the poster forgot the NameVirtualHost Directive, ex: NameVirtualHost *:80 or 443, thi is actually one of the comments in the linked site. By the date of this comment and from Apache v. 2.4 the NameVirtualHost is set by default and i think deprecated?
    – cognacc
    Feb 6, 2020 at 13:31
0

I had this problem migrating sites to a new Ubuntu 16 server. After a bit of head-scratching, I realised the SSL Module was not enabled by default, so anything inside the <IfModule mod_ssl.c> blocks is of course silently ignored.

Years ago I wrapped all my SSL vhosts in this conditional, and this time I had just copied the config files across to the new server.

I fixed it by enabling the module:

sudo a2enmod ssl
0

Coming in very late, but one of my virtual hosts, which had worked previously, suddenly was ignored by Apache (defaulting to the, well, default).

Turns out, I had obtained a LetsEncrypt cert for it and directed all requests to be SSL-encrypted. And ... in the conf file in sites-enabled for the SSL-protected site, the VirtualHost line read:

<VirtualHost 74.50.53.111>

Notice the lack of a port.

Changed it to:

<VirtualHost 74.50.53.111:443>

Suddenly the site is visible.

I spent hours trying to find that, so I thought I'd share the message to save everyone else the trouble.

0

For me, I didn't realize Google Chrome was hiding the www prefix on the URL. When I went to example.com my apache server was redirecting that to www.example.com without me realizing it.

0

Something I ran into, which isn't mentioned here yet, is that the UseCanonicalName setting may be Off by default.

UseCanonicalName: Determines how Apache constructs self-referencing URLs and the SERVER_NAME and SERVER_PORT variables. When set “Off”, Apache will use the Hostname and Port supplied by the client. When set “On”, Apache will use the value of the ServerName directive.

In my case I had to override the setting in httpd-default.conf

UseCanonicalName On

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .