Solved

Virtual Hosts

Posted on 2004-11-01
34,491 Views
Last Modified: 2013-11-18
I have been trying to set up virtual hosts on my Apache 2.x.x. From my registar godaddy.com I have set my domain as parked but have used total dns control to direct www to my IP address as well as pic to my IP address. Here are my virtual hosts:

<VirtualHost *:80>
    ServerAdmin webmaster@bejhan.com
    DocumentRoot C:/Server/Apache/Apache2/htdocs
    ServerName www.bejhan.com
    ServerAlias bejhan.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
#   ServerAdmin webmaster@bejhan.com
    DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
    ServerName www.pic.bejhan.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

For some reason pic.bejhan.com does not work (i tried servername of www.pic and also pic) if I comment out the first server then www and pic direct to the documentroot of pic.bejhan.com how can I correct this?
0
Question by:bejhan
    30 Comments
     
    LVL 19

    Expert Comment

    by:ramazanyich
    do you have following directive in the main part of your httpd.conf:
    NameVirtualHost *
    ?
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    ramazanyich is correct;  you need two statements in your httpd.conf as well as the virtual host:

    # Tell apache to listen to all requests on port 80
    Listen 80

    # Tell Apache that we are doing name-based virtual hosting for all IP addresses on port 80
    NameVirtualHost *

    Additionally, if you want pic.bejhan.com to work as well as www.pic.bejhan.com, you have to not only set up DNS for both pic.bejhan.com and www.pic.bejhan.com, but you have to modify your second virtual host statement as follows:

    <VirtualHost *:80>
    #   ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
        ServerName www.pic.bejhan.com
        ServerAlias pic.bejhan.com
    #    ErrorLog logs/dummy-host.example.com-error_log
    #    CustomLog logs/dummy-host.example.com-access_log common
    </VirtualHost>

    ServerAlias is a way of saying 'this is another name for this server'.

    0
     
    LVL 1

    Author Comment

    by:bejhan
    Yes  I had those statements, I just didn't include and I knew about the serveralias part too I think I found my problem sorry for the trouble.
    0
     
    LVL 1

    Author Comment

    by:bejhan
    Actually there is another problem:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    # Catch all
    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName *.bejhan.com
    </VirtualHost>

    When I use http://bejhan.com and http://www.bejhan.com it works fine and goes to C:/Server/Apache/Apache2/htdocs but when I use http://pic.bejhan.com (which is registered to my IP address as well) I am not directed to C:/Server/Apache/Apache2/htdocs/missing it is just sent to C:/Server/Apache/Apache2/htdocs.
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    Perhaps it is an obvious question, but did you restart the server after making the configuration change?
    0
     
    LVL 1

    Author Comment

    by:bejhan
    Yes I did restart the server and did it one more time just to make sure :) But sometimes the answer is actually that simple... but unfortunately not this time.
    0
     
    LVL 5

    Expert Comment

    by:mrielf
    Create a default settings for non www.bejhan.com requests

    example:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    <VirtualHost _default_:*>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
    </VirtualHost>

    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    What if you declare pic.behjan.com explicitly, like:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    # Catch all
    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName pic.bejhan.com
    </VirtualHost>

    and restart the server - does it then see pic.bejhan.com ?  

    You have the Listen 80
    and
    NameVirtualHost *

    correct?

    0
     
    LVL 1

    Author Comment

    by:bejhan
    I have my virtual hosts set up like this now:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    # Catch all
    <VirtualHost _default_:*>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName *.bejhan.com
    </VirtualHost>

    <VirtualHost *:80>
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName pic.bejhan.com
    </VirtualHost>

    pic.bejhan.com is sent to the www.bejhan.com documentroot. It seems as though pic.bejhan.com and www.bejhan.com are just being directed to the IP address and the virtual hosts are not affecting anything?
    0
     
    LVL 5

    Expert Comment

    by:mrielf
    try to set the ip address at NameVirtualHost instead *
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    I agree with mrielf - even though it shouldn't matter, you should try using the actual IP address in the NameVirtualHost setting, and even in the VirtualHost, like the following (where your ip address is xxx.xxx.xxx.xxx).  

    I've also moved the ServerName first, as that makes it a bit easier to read.  Order is also important - pic.bejhan.com wouldn't have been found in the config that you gave above as the catchall would have been found first.

    Finally, I combined the last two entries.

    Listen 80
    NameVirtualHost xxx.xxx.xxx.xxx

    <VirtualHost xxx.xxx.xxx.xxx:80>
        ServerName www.bejhan.com
        ServerAlias bejhan.com
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
    </VirtualHost>

    <VirtualHost xxx.xxx.xxx.xxx:80>
        ServerName pic.bejhan.com
        ServerAlias *.bejhan.com
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
    </VirtualHost>

    0
     
    LVL 1

    Author Comment

    by:bejhan
    I don't think it has a problem with the IP address...

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    <VirtualHost *:80>
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName pic.bejhan.com
    </VirtualHost>

    # Catch all
    <VirtualHost _default_:*>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/missing
        ServerName *.bejhan.com
    </VirtualHost>

    I had that but it told me that there was an overlap on port 80... so that may have something to do with it.
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    Do you have the configuration statement:

    Listen 80

    in your configuration file???
    0
     
    LVL 1

    Author Comment

    by:bejhan
    yes i do
    0
     
    LVL 5

    Expert Comment

    by:mrielf
    Instead

         NameVirtualHost xxx.xxx.xxx.xxx

         <VirtualHost xxx.xxx.xxx.xxx:80>

    try:

         NameVirtualHost xxx.xxx.xxx.xxx

         <VirtualHost xxx.xxx.xxx.xxx>

    or

         NameVirtualHost xxx.xxx.xxx.xxx:80

         <VirtualHost xxx.xxx.xxx.xxx:80>


    Maybe...

    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    I agree, particulary with the idea of trying:

    NameVirtualHost xxx.xxx.xxx.xxx:80

    <VirtualHost xxx.xxx.xxx.xxx:80>
    ...
    </VirtualHost>

    When you run the following command:

    apachectl configtest

    Do you get any errors?

    ... OH!  I just noticed that you are running under Windows.  What Operating System?

    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    (I mean, what version of Windows)
    0
     
    LVL 1

    Author Comment

    by:bejhan
    windows xp professional
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    It occurs to me that we are using Linux style paths, not Windows (which uses forward slashes instead of backslashes).  Have you tried:

    <VirtualHost xxx.xxx.xxx.xxx:80>
        ServerName www.bejhan.com
        ServerAlias bejhan.com
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:\Server\Apache\Apache2\htdocs
    </VirtualHost>

    # Catch all
    <VirtualHost xxx.xxx.xxx.xxx:80>
        ServerName *.bejhan.com
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:\Server\Apache\Apache2\htdocs\missing
    </VirtualHost>

    0
     
    LVL 1

    Author Comment

    by:bejhan
    Yes I used the backslash instead but that gave me double slashes.

    Having this setup:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName bejhancom.sytes.net
    </VirtualHost>

    #<VirtualHost *:80>
    #   DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
    #    ServerName pic.bejhan.com
    #</VirtualHost>

    # Catch all
    <VirtualHost _default_:*>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
        ServerName *.bejhan.com
    </VirtualHost>

    I receive the error:

    [warn] _default_ Virtualhost overlap on port 80, the first has precedence
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    That's correct - there *is* an overlap there.  www.bejhan.com and *.bejhan.com both match for www.bejhan.com.  However, for www.bejhan.com, the first entry will 'win' as it is first - isn't that what you want?

    However, 'pic.bejhan.com' does seem to go to the same place as www.bejhan.com still - what should I see there?
    0
     
    LVL 1

    Author Comment

    by:bejhan
    well i want pic.bejhan.com to go to a different page as it is not defined so it shoudl go to the catchall shouldn't it?
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    What happens (just for testing) when you comment out the default, and uncomment the pics.bejhan.com section?
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    (I'm wondering if it doesn't like the overlap).
    0
     
    LVL 1

    Author Comment

    by:bejhan
    in that case pic goes to its defined place...
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    the correct spot?  Then we know:

    (1) VirtualHosts are working
    (2) They can't overlap and work properly.

    Does this answer your questions now?
    0
     
    LVL 1

    Author Comment

    by:bejhan
    No because... the catchall account doesn't function correctly as when the virtual hosts are analyzed it sees that pic.bejhan.com is not defined it sends it to the www.bejhan.com location instead of *.bejhan.com
    0
     
    LVL 15

    Accepted Solution

    by:
    It looks like if you define www.bejhan.com, you can't use a catchall because of the overlap, though.  

    Your original question had to do with just getting the virtualhosts working, and we've now proved that they do work, as if you remove the 'default' (with the overlap), the two separate entries work.

    Maybe you could do this instead:

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName www.bejhan.com
        ServerAlias bejhan.com
    </VirtualHost>

    <VirtualHost *:80>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs
        ServerName bejhancom.sytes.net
    </VirtualHost>

    #<VirtualHost *:80>
    #   DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
    #    ServerName pic.bejhan.com
    #</VirtualHost>

    # Catch all
    <VirtualHost _default_:*>
        ServerAdmin webmaster@bejhan.com
        DocumentRoot C:/Server/Apache/Apache2/htdocs/nexus
        ServerName a*.bejhan.com
        ServerAlias b*.bejhan.com
        ServerAlias c*.bejhan.com
    </VirtualHost>

    (etc. for each letter of the alpha other than w?)  Basically, it appears you can't have two ServerName's the match www.bejhan.com or else it will dismiss the default.
    0
     
    LVL 1

    Author Comment

    by:bejhan
    ahh i c well thats good enuff forme
    0
     
    LVL 15

    Expert Comment

    by:periwinkle
    hope all works out smoothly for you!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Suggested Solutions

    Title # Comments Views Activity
    file server quastion 2 40
    Contact Forms for Website 6 52
    htaccess file 3 41
    Tape vs RDX backup 7 25
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    How to fix error ""Failed to validate the vCentre certificate. Either install or verify the certificate by using the vSphere Data Protection Configuration utility" when you are trying to connect to VDP instance from Vcenter.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to download, install and use CrashPlan from Code42.com on Windows 8. Open your browser and go to crashplan.com: Click get started, it’s free then Click the Download CrashPlan button: Click save in Internet Explorer, the…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now