Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Edited (again): I thought that an apache2 installation on my ubuntu os (personal) would enable me to "host" websites internally - without going onto the web. I am realizing now that the two domains that "work" are actually registered domain names, the other two are names that I intend to register later when I go live. But disconnecting to the Internet stops even the first two domains from working - though I know them to be pulling their files from the localhost.mysitename.com . What's happening here? I'm now reading up on etc/host and etc/host.conf and etc/nsswitch.conf - am I heading in the right direction?

I re-wrote this questions with more info. The problem persists. Thanks:

How in the world can you thoroughly read the apache docs on vHost configuring, try virtually every concoction and still have the initial scenario where the first two vHosts and directories work great and then every one after comes up bad. "Bad Request" "Server Not Found."

Here's the basic vHost file (in /etc/apache2/sites-available), replicated exactly 4 times over with only the "mysitename1" changing, its DocumentRoot and its Directory changing. BTW, this version is using the localhost IP (127.0.0.1) but I've used *:80 with the exact same results.

<VirtualHost 127.0.0.1>
 ServerAdmin xx@yyyyy.com
 ServerName localhost.mysitename1.com

 DocumentRoot /home/folder/folder/folder/folder
 <Directory />
  Options FollowSymLinks
  AllowOverride All
 </Directory>
 <Directory /home/folder/folder/folder/folder>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
 </Directory>

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
 <Directory "/usr/lib/cgi-bin">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
 </Directory>

 ErrorLog "path to this site's error.log"

 LogLevel warn

 CustomLog "path to this site's access.log"

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

And inside the ports.conf is this: (I've started with the *:80 instead of the 127.0.0.1) - same results as described above:

Listen 80
NameVirtualHost 127.0.0.1

I'm wondering about the doc sentence of: "You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries." That is all I'm trying to do, essentially a developing environment where I can work on multiple sites locally. I can't find this "hosts" file, but I'm wondering if I entered my first two domains (a long time ago) and forgot how I did it. ??

Thanks for any help.

share|improve this question
    
There is no limit to virtual hosts. –  lg. Jan 17 '11 at 8:26
add comment

5 Answers

up vote 1 down vote accepted

The answer was in the /etc/hosts file. To discover IP addresses for names the systems DNS resolver will (should) first look to the /etc/hosts file for name=ip, if not there it goes on a DNS search. Read "man hosts" to see the syntax for the hosts file. Assign all of your domains (preferrably not FQDN) the ip # of your localhost (127.0.0.1) example:

127.0.0.1 localhost.exmaplesite1.com
127.0.0.1 localhost.exmaplesite2.com

Use the localhost.examplesite1.com and localhost.examplesite2.com as your ServerName in your VirtualHost directive. See the docs link for other settings and syntax. @Antonious use of a sole wildcard is not documented by Apache2 - but he did not understand I was trying to force certain names through a localhost loopback so that I could develop web applications offline. I never understood @lynxman's assumption that a "Bad Request" or "Server Not Found" error could write to the error logs to begin with - though I was using separate log files for each VirtualHost directive anyway.

BTW: "sudo host localhost.examplesite.com" will print out the IP address for the site IF 'examplesite.com' is an actual site. Apparently the "sudo host " command does NOT look for the /etc/hosts settings and influence on the IP. <-- yeah, I know, that screwed me over good.

cheers!

share|improve this answer
add comment

You can host as many virtual hosts as you wish in apache, this looks like the hostnames are either very similar or overlapping, when creating a virtual host in apache make sure that

  • When using DNS based virtual hosts you always have a NameVirtualHost directive per IP and port you're serving
  • Make sure that the virtual host names do not collide, you can always check in which one you're landing by either adding %V to your log format (so it shows which domain is hitting) or having a file in each VH declaring who it is
share|improve this answer
    
Still no go. I've read the httpd.apache.org/docs/2.0/vhosts/name-based.html doc. Again, first two vHosts are good. I now have two more and they both error, one "Bad Request" the other "Server Not found." I assume I should be able to listen on one port (80) and have the NameVitrualHost pick up the VirtualHost pick up the ServerName. The fact that the first two work and then nothing....has got me dumbfounded. –  Ricalsin Jan 17 '11 at 21:51
    
The thing is that VirtualHost ServerName is very very important, apache will try to understand who you're calling so it'll always try to map you to the right one. The fact that everything is falling into the first two is a good indicator that apache is at least confused. As said try to add the %V to your LogFormat config and that should show you which virtual host is it falling into, it'll give you some clues about what's happening. –  lynxman Jan 18 '11 at 13:08
    
I have separate log files for each virtual host. Everything does not "fall" into the first two, it accurately handles the first two. How do you "add %V to the log format"? I suspect the V option is get the verbrosity about what's going on because right now I'm not seeing anything in the logs on two domains that fail. –  Ricalsin Jan 18 '11 at 16:45
add comment

I'll share my working config with you.

Make sure you have linked your virtual hosts in sites-enabled. Apache executes each sites-enabled file in alphanumeric order so name your default site something like 0000-default so it will execute first. This is the site your users should see when virtual hosting fails. Mine looks like this:

NameVirtualHost *

    <VirtualHost *>
            ServerAdmin contact-us@obscuredomain.com

            DocumentRoot /var/www/0000-default-site
            <Directory />
                    Options FollowSymLinks
                    #AllowOveride set to All for mod rewrite (default None)
                    AllowOverride All
            </Directory>
            <Directory /var/www/0000-default-site>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
                    # Uncomment this directive is you want to see apache2's
                    # default start page (in /apache2-default) when you go to /
                    #RedirectMatch ^/$ /apache2-default/
            </Directory>


            ErrorLog /var/log/apache2/error.log

            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel info

            CustomLog /var/log/apache2/access.log combined
            ServerSignature On


    </VirtualHost>

Note the NameVirtualHost * directive. It's important that this be the very first directive in the virtual configs. So you put it at the top of 0000-default and make sure nothing else is executed first.

Next name your first virtual host 0010-yourdomain.name and your second 0020-another.name. This is how my virtuals look (notice they are slightly different from the default):

<VirtualHost *>
        ServerName www.lickmyquads.net
        ServerAlias lickmyquads.net
        ServerAdmin contact-us@lickmyquads.net

        DocumentRoot /var/www/lickmyquads.net/www/docroot
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/lickmyquads.net/www/docroot>
                Options FollowSymLinks
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        #<Directory "/usr/lib/cgi-bin">
        #       AllowOverride None
        #       Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        #       Order allow,deny
        #       Allow from all
        #</Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel info

        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

        #Custom Error
        #ErrorDocument 404 /app/404.php
        #ErrorDocument 404;2 /app/404.php
        #ErrorDocument 404;3 /app/404.php

</VirtualHost>

Hopefully this working example helps you sort things out. If it doesn't try running the apache2ctl configtest command to help find any syntax errors in your configuration.

share|improve this answer
    
"lickmyquads"? You'd need longer hair and less weight. (-: –  Ricalsin Jan 18 '11 at 16:47
    
Still, I'm trying build a local system for web development, not actually serve tothe public. I appreciate your info. I'm still looking into it. I'd prefer a loopback (internal) structure so it does not go out to the Internet - which I think my config is doing to try and resolve the DNS. (?) –  Ricalsin Jan 18 '11 at 16:50
add comment

"Server Not Found" sounds like your host file isn't set up properly. Tell us a little about your client setup; OS, browser, hosts file etc.

share|improve this answer
add comment

If you have multiple clients and don't want to maintain separate host files you can set up a split horizon DNS server.

http://en.wikipedia.org/wiki/Split-horizon_DNS

It's not as hard as it sounds and there are advantages to having a local caching DNS server.

share|improve this answer
    
Interesting, but not the solution to the original problem. Thanks for your effort, Antonious. –  Ricalsin Jan 21 '11 at 19:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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