(?) NameVirtualHost in Apache

From Rich Price

Answered By Huibert Alblas, Faber Fedor, Mike "Iron" Orr, Heather Stern

I am running Slackware 7.1.0 and Apache/1.3.12 on my web server which I have been running for many years. Recently, I acquired a second domain name and I attempted to use name based virtual hosting to support both domains on this server.
I have been unable to configure apache to do this. I have been using the second edition of "Apache The Definitive Guide" by Ben and Peter Laurie as a guide. But repeated attempts to come up with a httpd.conf file have resulted in either both web sites showing the same [primary site] pages or in both sites showing an error page.
I have a number of questions that the Laurie's book has not been able to answer:
  1. Can you have both a primary site and a "NameVirtualHost" site that use
  2. Can you have both a primary site and a "NameVirtualHost" site that use the same IP address?
  3. Should I make both sites be "NameVirtualHost" sites?
  4. Should I make both sites be "NameVirtualHost" sites?
  5. Is there anything obviously wrong with these httpd.conf statements?
  6. Is there anything obviously wrong with these httpd.conf statements?
#
NameVirtualHost aaa.bbb.ccc.ddd
<VirtualHost virt-host-name>
   ServerAdmin relevant-email-address
   DocumentRoot /websites/virthost/htdocs
   ErrorLog /var/log/error_log
   CustomLog /var/log/access_log common
</VirtualHost>
#
Any advice would be appreciated!
Rich Price
(!) [Halb] Yes, as far as I can see you're missing the ServerName entry:
 <VirtualHost virt-host-name>

    ServerName virt-host-name

    ServerAdmin relevant-email-address
    DocumentRoot /websites/virthost/htdocs
    ErrorLog /var/log/error_log
    CustomLog /var/log/access_log common
 </VirtualHost>
Hope it helps

(?) Thanks for your reply. But my problem remains.

When I add the NameVirtualHost and <VirtualHost > commands [with the ServerName statement] to the end of the httpd.conf file, I get the "HTTP 404 - File not found" error message when trying to access either website.

(!) [Faber] Hang on. From what I've seen in this thread, you need to do this:
NameVirtualHost aaa.bbb.ccc.ddd

<VirtualHost aaa.bbb.ccc.ddd>   <-- note this nibbly bit
    ServerName virt-host-name
    <...snip...>
</VirtualHost>
NOT
NameVirtualHost aaa.bbb.ccc.ddd

<VirtualHost virt-host-name>    <-- this line is the problem
    ServerName virt-host-name
    <...snip...>
</VirtualHost>
(!) [Iron] That's how our sites are set up, those that do name virtual host. However, the Apache docs (version 1.3) say you can use either the IP or domain name, although they recommend the IP for the NameVirtualHost line. I would thus use the IP on both the NameVirtualHost and VirtualHost lines so it's easier for the person to match up which VirtualHost stanzas go with which NameVirtualHost.
Note that you can also use ServerAlias to define multiple names for one virtual host:
NameVirtualHost 10.0.0.1:8080
<VirtualHost 10.0.0.1:8080>
  ServerName buffy.vampire.slayer
  ServerAlias www.buffy.vampire.slayer
  ...
</VirtualHost>
This can be called the "Rick Moen can't get his users trained not to type the 'www'" approach.

(?) I figgured it out!!!!

The Apache online documentation says:

...............

Note: the "main server" and any _default servers will never be served for a request to a NameVirtualHost IP Address . . .

...............

So I added the origional "main server" as another virtual host like this:

See attached named-host.httpd.conf

And the original host continued to work with the new httpd.conf file.

The problem with the new host turned out to be a misspelled directory path. [I blush with shame].

Thanks for all the help.

Rich Price

(!) [Iron] Oh, OK. Often you think the problem is one thing but it turns out to be something else.
(!) [Heather] So, I didn't join this thread when it was active during the month, and here I am adding this as I edit the thread. But I figured it would be valuable to complete the example.
You need to have permissions for the directory set up in your httpd.conf too, since the very default permissions usually explicitly describe that anywhere in the web tree should be inaccessible unless opened up (or possibly, only open to localhost). The defaults may work pretty well until you start moving your web directories to a place unplanned for by your installation of Apache. (For example, having a websites directory off of the root directory, not exactly LSB compliant , but heck, it's your server.) That Rich misspelled this little tidbit, then realized and spelled it right, is what fixed him. So here's that part, presuming that these are pages for access by a world of visitors, not just a limited number of friends.
AllowOverride suggests what things .htaccess control files can affect.

See attached host-directories.httpd.conf

Now, the fact that he mentions "htdocs" suggests that he probably also has a dedicated cgi directory and maybe even a dedicated icons directory (so fancy indexing gets a touch of local flavor). So here's an expanded and slightly fancier example that covers the lot, and puts things in a slightly more reasonable place in the directory tree. Also I seperate the logs.

See attached completist.httpd.conf

Of course, to use my example you should also touch and set permissions for the logfiles you'll be keeping, and give them group permission that allows the webmasters to see their own stuff.
You'll want to change that "lettered" IP address to a real one. You can use 127.0.0.2 and a bunch of entries in your /etc/hosts file to test things out if you need to. DNS "A records" should exist pointing these sites to your IP addrress; it might be good if the less preferred names (the ServerAlias values) are all CNAMEs to their primaries, so that at least a few browsers will get the hint when bookmarking.

See attached test-fragment.etc-hosts.txt

Create /home/websites/virthost1 and its subdirectory /errors with 2 files in it, likewise for virthost2, and copy their sets of icons in.
Make sure that the webfiles and grpahics for the web are world readable in your filesystem too, so the webdaemon (which usually runs as an unprivileged user, either 'nobody' or one dedicated to run only apache) can access the files. They can be group writable by the webmaster for the given site.
Keeping the custom error pages in the directory "errors" under the web tree and numbered like that is compatible with thttpd; so is the attitude that everything under the htdocs directories iw world-readable at the filesystem. To really go whole hog with that, your virthostX directories would be named the same as the preferred name of the site instead, and your thttpd.conf would contain the lines:

dir=/home/websites
vhost
Useful to know if you're switching over in a hurry if there is some nasty apache hole that's been discovered and you can't afford to have the websites down while the patch is being prepared. Set symlinks for the ServerAliased names and it should just work, though your cgi scripts will be dead without additional setup, thttpd doesn't do fancy indices, and the logs will be somewhere new.
Add norobots.txt files for spice if you like to keep your sites from being web-indexed until they are ready.

See attached simple.norobots.txt

When things are good you can take out the second Disallow and replace it with lines for specific places that shouldn't be traversed.
After you've finished mixing all these ingredients in a big mixing bowl labelled "disk space", season to taste, stuff in the oven, bake at 450 for 10 seconds, frost. Serves thousands.


Copyright © 2003
Copying license https://www.linuxgazette.net/copying.html
Published in Issue 86 of Linux Gazette, January 2003
HTML script maintained by Heather Stern of Starshine Technical Services, https://www.starshine.org/


[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4 [ Index of Past Answers ]