Apache Multiple Sub Domains With One IP Address

44,811

Sounds like you need to add NameVirtualHost directive to your configuration.

NameVirtualHost         *:80

Under some circumstances Apache may not be able to handle *:80 VirtualHosts correctly. In those cases you should map VirtualHosts directly on specific IPs.

NameVirtualHost         8.8.8.8:80

<VirtualHost 8.8.8.8:80>
    ServerName  staging.mydomain.com
    ServerAlias stage.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>

<VirtualHost 8.8.8.8:80>
    ServerName  dev.mydomain.com
    ServerAlias development.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>

You can also run apachectl -t -D DUMP_VHOSTS to see how Apache parses the VirtualHost configuration.

Update: As mentioned in the comments, usually you can just use NameVirtualHost *:80. So most of the time you can configure the virtual hosts as follows.

NameVirtualHost         *:80

<VirtualHost *:80>
    ServerName  staging.mydomain.com
    ServerAlias stage.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>

<VirtualHost *:80>
    ServerName  dev.mydomain.com
    ServerAlias development.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>
Share:
44,811

Related videos on Youtube

Jongosi
Author by

Jongosi

Updated on June 16, 2020

Comments

  • Jongosi
    Jongosi almost 4 years

    This has probably been asked but I can't find a straight answer, or the ones I found don't work.

    I have one domain mydomain.com, resolving to an IP; let's call it 8.8.8.8. The DNS settings also point two subdomains to that IP address with an A record. These are dev.mydomain.com and staging.mydomain.com. Both have an A-record pointing to 8.8.8.8.

    On the server (8.8.8.8) I have two virtual hosts files. These are as follows:

    staging.mydomain.com.conf

    <VirtualHost *:80>
        ServerName  staging.mydomain.com
        DocumentRoot /var/www/html/mydomain.com/staging/
    </VirtualHost>
    

    And...

    dev.mydomain.com.conf

    <VirtualHost *:80>
        ServerName  dev.mydomain.com
        DocumentRoot /var/www/html/mydomain.com/dev/
    </VirtualHost>
    

    The problem is:

    Regardless of whether I visit http://staging.mydomain.com or http://dev.mydomain.com, I always land on staging.mydomain.com (Apache serves these files).

    I have restarted Apache and even the server. If I change the order of the .conf files so that dev is first, I always see that. Any suggestions would be so appreciated. Thanks!


    update

    I find myself back at this problem again! If you know that your syntax is correct, you might have a bad symlink. Delete it and recreate again, restarting apache in-between. I just did this and it solved hours of head-scratching. On CentOS you can check your available vhosts with httpd -S

    update 2

    I've also found this issue to exist when the apache log files for the virtual host don't exist, or aren't writable.

    • Sumurai8
      Sumurai8 almost 9 years
      I have voted to re-open this question, because both the question and the answer are helpful. Besides this, questions about Apache are not off-topic on StackOverflow, and ServerFault is only for professional setups.
  • Jongosi
    Jongosi over 11 years
    Thx Ketola. At the end of my httpd.conf file, I have the following (where the vhosts are included) NameVirtualHost *:80 [[new line]] Include /etc/httpd/sites-enabled/
  • Ketola
    Ketola over 11 years
    That should work then. Apache can be a bit picky with *:80 at times, so you might want to try 8.8.8.8:80 instead to see if it works better. Also run apachectl -t -D DUMP_VHOSTS to see how Apache parses your configuration files. Updated the answer to reflect these as well.
  • Jongosi
    Jongosi over 11 years
    Thanks Ketola, tried that in both .conf files and the httpd.conf file but no cigar, although the order changes! The apachectl -t -D DUMP_VHOSTS is very useful and shows that Apache is defining the dev server as the default and loading it first.
  • Ketola
    Ketola over 11 years
    Very strange. The only other cause I can think of is a typo somewhere, but that's quite unlikely. Do you have a proxy or anything else in between that could mess up the Host: header in the request? The next step would be to set LogLevel debug in Apache, and/or do telnet localhost 80 on the server and request for the document directly.
  • Jongosi
    Jongosi over 11 years
    Thx anyway Ketola. Going to look at the domain DNS settings again.
  • Ketola
    Ketola over 11 years
    Thank you for accepting the answer Jongosi. I'm sure you'll figure out the problem soon enough.
  • jyore
    jyore almost 11 years
    I had a similar issue...the apachectl -t -D DUMP_VHOSTS command was the tell all: [Mon May 20 23:51:08 2013] [warn] _default_ VirtualHost overlap on port 80, the first has precedence . Adding the NameVirtualHost *:80 solved it
  • Alex Khimich
    Alex Khimich over 9 years
    For googlerslike me use this: <VirtualHost *:80> ServerName www.domain.tld ServerAlias domain.tld *.domain.tld DocumentRoot /www/domain </VirtualHost>
  • softvar
    softvar over 8 years
    Perfect! Was looking for this one for a long time (Y)
  • Bobort
    Bobort over 3 years
    I tried NameVirtualHost, but it is being deprecated. And it didn't work for me anyway.