Setting up Virtual Hosting

I am endeavouring to set up virtual hosting in OpneSUSE 10.3

I have added these entries to the httpd.conf file:

NameVirtualHost *
<VirtualHost *>
ServerName www.library.com
DocumentRoot /srv/www/htdocs/library.com
ServerAdmin webmaster@library.com
ErrorLog /var/log/apache2/www.library.com/error_log
CustomLog /var/log/apache2/www/lbrary.com-access_log common
</VirtualHost>

I have added a folder called library.com to my htdocs folder and a test index.html file in the folder.

I had assumed this would allow me to access the address:

www.library.com

But this does not work. Could anyone help explain what I’m doing wrong?

With thanks

Robert

On Mon, 30 Jun 2008 10:46:04 GMT
rsterry <rsterry@no-mx.forums.opensuse.org> wrote:

>
> I am endeavouring to set up virtual hosting in OpneSUSE 10.3
>
> I have added these entries to the httpd.conf file:
>
> NameVirtualHost *
> <VirtualHost *>
> ServerName www.library.com
> DocumentRoot /srv/www/htdocs/library.com
> ServerAdmin webmaster@library.com
> ErrorLog /var/log/apache2/www.library.com/error_log
> CustomLog /var/log/apache2/www/lbrary.com-access_log common
> </VirtualHost>
>
> I have added a folder called library.com to my htdocs folder and a
> test index.html file in the folder.
>
> I had assumed this would allow me to access the address:
>
> www.library.com
>
> But this does not work. Could anyone help explain what I’m doing
> wrong?
>
> With thanks
>
> Robert
>
>
Hi
Assuming it’s on your local network, can you get to it via the ip
address (or localhost if you are on the machine with apache running)?

If you can get to it via the above, unless you are running a local DNS,
you would need to add in the hosts file for the machines on the local
LAN.


Cheers Malcolm °¿° (Linux Counter #276890)
SLED 10.0 SP2 x86_64 Kernel 2.6.16.60-0.23-smp
up 19 days 8:50, 0 users, load average: 0.02, 0.15, 0.19
GPU GeForce 8600 GTS Silent - Driver Version: 173.14.09

There are two parts to virtual web hosting.

  1. The virtual host domain name must map to the IP address of the server. This implies you must have control over DNS resolution. This means you must be able to intercept any resolution requests for www.library.com and return the IP address of your server. If you were not using www.library.com as an example, to do that you would have to interpose your own nameservers in the lookup. And in the process you would lose access to the real www.library.com in the outside world.

Anyway, let’s assume you have a domain name you can legitimately use and resolve.

  1. Then you have to tell Apache, when you see a request for www.library.com in the Host: line, it goes to this virtual server, as you have done in your example.

But without condition 1, the HTTP request never even reaches your server.

OpenSUSE has a directory intended for vhost config files, /etc/apache2/vhosts.d and there’s a template there you can use as a starting point. The actual config file should end in .conf. You also have to enable virtual hosts in /etc/apache2/listen.conf by uncommenting the appropriate NameVirtualHost line.

Thanks to you both for your replies.

First, in reply to Malcolm’s suggestion.

“Assuming it’s on your local network, can you get to it via the ip
address (or localhost if you are on the machine with apache running)?”

Not sure what you mean. Do you mean typing:
http://localhost/library.com in the browser on my Linux machine? I have Apache running.

http://localhost works fine but /library.com does not.

In reply to Ken:

“The virtual host domain name must map to the IP address of the server. This implies you must have control over DNS resolution.”

As I am new to Linux, I am following the directions in the 2007 ebook by Michael McCalister: OpenSUSE Linux Unleashed.

He says “there are two ways to implement virtual hosts in Apache: name-based and IP address–based hosting.” He recommends the former and that’s the one I was trying to implement. He only mentions the config on the httpd.conf file - as I have done. He doesn’t say anything about adding a folder called “library.com” (as in my example) in the htdocs folder. That was my own effort to try and get it working. Nor does he say anything else is needed to get it working. Has he left out some vital steps?

“Let’s assume you have a domain name you can legitimately use and resolve.”

I thought that the OpenSUSE had it’s own DNS server that could resolve addresses - at least within my LAN?

“OpenSUSE has a directory intended for vhost config files, /etc/apache2/vhosts.d and there’s a template there you can use as a starting point. The actual config file should end in .conf. You also have to enable virtual hosts in /etc/apache2/listen.conf by uncommenting the appropriate NameVirtualHost line.”

This is new information. Not mentioned in the book at all. As I am new to Linux, I would need considerably more guidance to set up the above. And probably much more than could reasonably be given over a forum. I am finding it extremely difficult to find good documentation on OpenSUSE. Perhaps you could direct me to some more detailed documentation on the subject of Virtual Hosting?

Many thanks to you both

Robert

No, it’s not a matter of adding a folder called library.com. You have to make it so that when your clients look for a machine called library.com, they will go to your server. This means you have to control the name resolution for your clients. As there is already a www.library.com in the real world, it means you are in a sense hijacking access to that site for your internal clients.

I thought that the OpenSUSE had it’s own DNS server that could resolve addresses - at least within my LAN?

Yes, but you have to set it up and make your clients use it. And it has to forward resolution requests for other domains to the outside world.

“OpenSUSE has a directory intended for vhost config files, /etc/apache2/vhosts.d and there’s a template there you can use as a starting point. The actual config file should end in .conf. You also have to enable virtual hosts in /etc/apache2/listen.conf by uncommenting the appropriate NameVirtualHost line.”

This is an OpenSUSE thing. I don’t know where it’s documented because I worked out what that directory is for from the main config file above. Possibly in a SUSE book.