Centos – apache virtual hosts – server not found for subdomain

apache-2.2centosvirtualhost

I want to set up a subdomain using virtual hosts. I have altered the httpd.conf file as follows, but I get a "can't find server" error when I try to access the url. I'm running Centos 5.5.

NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

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

<VirtualHost *:80>
    DocumentRoot "/var/www/vhosts/test_mydomain_com/"
    ServerName test.mydomain.com
    ErrorLog logs/test_mydomain_com-error_log
    CustomLog logs/test_mydomain_com-access_log common
</VirtualHost>

I have:

  • Checked the syntax is valid by "service httpd configtest"
  • Restarted Apache
  • Checked that the DocumentRoot described above does indeed get to the folder I have my index.php file in for the subdomain.

I'm suprised that even though it can't find the subdomain, it doesn't get routed to the www.mydomain.com area because that's what I thought that I'd set it up to do with the first virtual host statement.

I'm trying to access the subdomain by:

http://test.mydomain.com

Do I need to do anything to (and thus learn about) the DNS settings to acheive what I want here? Is that what could be going wrong?

Best Answer

You need either a A record or CNAME record added to your DNS server in order for that to work. Sounds like you do not have one. You can confirm for us by posting host test.mydomain.com.

If you get back:

Host test.mydomain.com not found: 3(NXDOMAIN)

Then that is your problem.

Either add an A record (hostname -> IP) or a CNAME (hostname -> hostname) for test.mydomain.com and you should be in business.

Related Topic