Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Multiple Sub Domains With One IP Address

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.

like image 834
Jongosi Avatar asked Dec 12 '12 10:12

Jongosi


People also ask

Can subdomains have the same IP address?

It will assign the same IP as example.com if you have set in your DNS for that to be the case. However, if you've set the IP for sub.example.com to go somewhere else, the traffic will go somewhere else.

Can you have 2 sub domains?

A subdomain is an additional part to your main domain name. Subdomains are created to organize and navigate to different sections of your website. You can create multiple subdomains or child domains on your main domain.


1 Answers

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> 
like image 193
Ketola Avatar answered Sep 22 '22 13:09

Ketola