1

Apache's httpd.conf file has three sections. Section 2 is for the 'Main' server configuration, and Section 3 is for Virtual Hosts. In section 2, one has the option to give their server a name. In the past, I always filled this section out regardless of whether I am also using VirtualHosts.

ServerName www.mysite.com
DocumentRoot "/var/www/html/"

Recently, I found my VirtualHost wasn't working as I expected. Later I found that the Section 2 configuration was taking precedent over the VirtualHost using the same ServerName. I had thought that the VirtualHosts would extend the second section, however, it doesn't seem to be the case.

When using VirtualHosts, should ServerName not be filled out (or left as example.com) in Section 2?

8
  • What OS are you using? What version of Apache are you using? I think it makes a difference.
    – closetnoc
    Commented Feb 2, 2015 at 16:25
  • @closetnoc Centos 6 with Apache 2.2. Commented Feb 2, 2015 at 17:51
  • Thanks! The reason why I ask is because there are different Apache packages depending upon the OS and there are different configuration requirements as a result. For example, I do not see in my configuration the same ServerName you do so I am not sure I know how to answer this. At least not yet. I do seem to remember this for older versions of Apache on Redhat. Some packages are similar to older configuration requirements and others are completely different.
    – closetnoc
    Commented Feb 2, 2015 at 18:01
  • 1
    Okay. I did a quick look using centos 6 apache 2.2 ServerName in Google. It appears that the use of ServerName should be used in the various VirtualHost directives. If it exists above these directives in the httpd.conf then it should be commented out. This page may help: tecmint.com/apache-ip-based-and-name-based-virtual-hosting Try this and let me know if this works okay for you. You will have to restart Apache.
    – closetnoc
    Commented Feb 2, 2015 at 18:12
  • @closetnoc Thanks! I never read that it should be commented out, but it appears to need to. Commented Feb 2, 2015 at 18:36

1 Answer 1

2

For future readers, there are several builds of Apache one for each of the major *inux instances out there and there are differences between them. One of the major differences is whether Apache is using httpd.conf where it is primarily configured as a single site web server or using apache2.conf where it is primarily configured for virtual hosts. Personally, I prefer the latter, but both are fine. But what happens is that the builds using httpd.conf primarily configured as a single site occasionally confuses people. It is actually how Apache used to be built before the change. I am not sure where the confusion comes in, but I rather suspect that having different sets of instructions on the net may have something to do with it. For the record, if you have httpd.conf, it may pay to reference older Apache documentation regarding configuration at times but not forgetting what is depreciated/changed by referring to the new documentation.

In this case, the OP seems to be creating virtual sites below the original single site. Each section would use the directive ServerName. Without looking at his configuration specifically, there seems to be a conflict between the original single site and one of the virtual sites.

There are two options for solving his problem.

1] Commenting out the ServerName within the single site configuration since there seems to be some confusion there. It may be likely that this entire section should be commented out.

--or--

2] Configure the original single site as a catch-all site.

Since this is an answer based upon comments, the OP commented out the ServerName in the original single site configuration which solved his problem. This is option 1. In the comments, I referred to http://www.tecmint.com/apache-ip-based-and-name-based-virtual-hosting/ as a resource that might help the OP. In all of the help I found on the net, none of them seemed to refer to this problem directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.