Subdomains not working with virtual hosts on apache2 ubuntu
Solution 1
How to debug all similar errors:
1) make sure the domain red.whatever.com is pointing to the correct ip
How to do this? Use a tool like DIG on linux/osx or resort to an online tool.
the common error the browser reports in such a case is COULD NOT FIND DOMAIN...
2) If the IP is correct check both Apache logs (error.log and access.log)
If there is no information on these logs check your firewall to see if port 80 is open.
the common error on the browser reports in such case is COULD NOT CONNECT...
3) IP is correct, port is open, Apache is not serving the content you were expecting
The solution to your problem will be given by the log files
the browser will show a 404 or a similar http error sent by the server...
EDIT: from your comment your problem is on number 1.
Solution 2
Does the NameVirtualHost
directive (e.g., NameVirtualHost *:80
) appear in your configuration file before the virtual hosts sections? If not, all requests will go to the first vhost.
Related videos on Youtube
cy834sh4rk
Updated on September 18, 2022Comments
-
cy834sh4rk almost 2 years
I'm trying to set up a subdomain on my ec2 account but can't figure out what's going on. I've looked for a few hours and haven't been able to find an answer :-/
I'm trying to set up a subdomain using virtual hosts but no matter what I try the browser can't find the subdomain :-(
I have the following vhosts files set up:
apache2/sites-available/mysite (this site currently works)
<VirtualHost *:80> ServerName mysite.com ServerAdmin webmaster@localhost DocumentRoot /home/sites/mysite <Directory /home/sites/mysite> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/mysite-error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined </VirtualHost>
apache2/sites-available/red (this is the subdomain I'm trying to set up)
<VirtualHost *:80> ServerName red.mysite.com ServerAdmin webmaster@localhost DocumentRoot /var/www/red <Directory /var/www/red> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/red-error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/red-access.log combined </VirtualHost>
Apache mod_rewrite is enabled.
I've enabled both sites using a2ensite and I make sure I restart apache every time I make a change.
/etc/hosts
127.0.0.1 localhost 127.0.0.1 mysite.com 127.0.0.1 red.mysite.com
Any help would be appreciated.
Thanks!
-
BudwiseЯ over 11 yearsThis question might belong to serverfault.
-
-
saeid alipour over 11 yearsyes, I'm aware of the difference in the <Directory> paths. I tried making a new directory in /home/sites for the subdomain but it still didn't work. As far as being able to ping the domains, yes, both commands ping the same IP... I don't get any errors on the apache error log :-/
-
saeid alipour over 11 yearsand actually I don't think the message is not getting to EC2 since I don't see anything in the access.log file. Is there anything I need to do on the AWS side to make this work?
-
Admin over 11 yearsI tried adding the following line to both files NameVirtualHost *:80 and when I restart apache I get the following warning message: * Restarting web server apache2 [Tue Oct 09 02:31:12 2012] [warn] NameVirtualHost *:80 has no VirtualHosts ... waiting [Tue Oct 09 02:31:13 2012] [warn] NameVirtualHost *:80 has no VirtualHosts The subdomain still does not work :-/
-
Admin over 11 yearsAdd it before the VirtualHost sections.
-
Admin over 11 yearsThanks for your help! I did. I tried adding it to both files before the <VirtualHost *:80> section and I also tried adding it to one file only. I still get the same warning message when restarting apache.
-
Admin over 11 yearsNo, add it in the main configuration file. And while you're add it, check to make sure the line to include sites-available is not commented it out.
-
Frankie over 11 years@cy834sh4rk that message from Google Chrome means the the domain is badly configured. red.mysite.com is NOT pointing to the IP it should be.
-
cy834sh4rk over 11 years@Dondi - I'm sorry but I don't know what you mean by "the main configuration file". I tried adding it to http.conf and the default vhost but that didn't help.
-
cy834sh4rk over 11 yearsI think you're correct. The red.mysite.com domain is not pointing to the correct IP address but I'm not sure how to make it point to the correct address. I thought that setting up separate vhost files for the main domain and the subdomain would be enough. I tried doing 'dig red.mysite.com' and I don't get an "ANSWER SECTION:" so I'm assuming that means it doesn't resolve to the correct IP. Any other ideas?
-
Frankie over 11 years@cy834sh4rk can I assume red.mysite.com is a FQDN (fully qualified domain name)? Meaning that you paid some register for a .com domain name that you can access everywhere? If so, most providers have a control panel where you set up the DNS. You have to create a new entry for
red
and point it to your IP. -
cy834sh4rk over 11 yearsYes, I purchased a domain and it is pointing to my AWS EC2 instance. The main domain works but when I do red.mydomain.com it doesn't. I tried adding a CNAME record on GoDaddy for red.mydomain.com and it's still not working :-/ Is there anything else I can check? Nothing gets logged when I go to red.mydomain.com, no entries in error.log or access.log
-
Frankie over 11 yearsJust add an
A record
red
to the domainmysite.com
pointing to your IP. On computers things must be solved in layers. No need to check the logs when the domain is not pointing in the correct direction... -
cy834sh4rk over 11 yearsThanks a lot for your help! it was a set up issue on GoDaddy. I had an A record for red but I also had a CNAME record which I guess were conflicting but GD's tech support said that was correct.
-
Frankie over 11 years@cy834sh4rk I'm glad this worked out. Now I've seen you're a new user around SE (stack exchange) so I would adive you to read the FAQ both the one for ServerFault and the one for StackOverflow. Essentially as a newcomer the community will be expecting that you upvote/accept good answers on your questions. Welcome!