Forum Moderators: coopster & phranque

Message Too Old, No Replies

[Apache] VirtualHosts on 'localhost'

How do I create VirtualHosts on my testserver?

         

GrodanBoll

4:51 pm on Mar 10, 2002 (gmt 0)



Hi

Can someone tell if I can create VirtualHosts on my testserver? I access the testserver by the usual 'http://localhost'.

If i can, how do I do it?

/Grodan :)

PsychoTekk

4:54 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



look at the end of httpd.conf

GrodanBoll

4:58 pm on Mar 10, 2002 (gmt 0)



Hi PsychoTekk :)

I know how to create virtual hosts when I have different IP:s or a DNS. But in this case I only have 'localhost'.

Can I create VirtualHosts on my testserver?

/Grodan :)

PsychoTekk

5:00 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



NameVirtualHost *

<VirtualHost *>
ServerName some.host.tld
DocumentRoot /www/somehostdir
</VirtualHost>

i suppose name based vhost is what u need, i've never tried it myself though..

Air

5:01 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah have a look at the example at the end of the httpd.conf file as PsychoTekk suggested. In addition, which may not be obvious, when you have created a virtual host, Apache will recognize requests for it by name not IP. Therefore you will need to add entries in your "host" file so that you can request the virtual host by name not by IP.

GrodanBoll

5:01 pm on Mar 10, 2002 (gmt 0)



Can I maybe use 127.0.0.1:80 and just change the port?
Do you know if it works?

/Grodan :)

PsychoTekk

5:04 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



i don't think you can maintain 2 different ports on one apache...

GrodanBoll

5:06 pm on Mar 10, 2002 (gmt 0)



Thx for the answers, but I don't have a domain-adress to my testserver. I can only use 'localhost', '127.0.0.1' or my real IP.

I'll try to have different portnumbers and se what happens... :)

/Grodan :)

Air

5:08 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



GrodanBoll,

are you using Windows or *nix? The HOST file is the answer.

GrodanBoll

5:10 pm on Mar 10, 2002 (gmt 0)



I'm sitting om Win 2000...

PsychoTekk

5:18 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



i don't know for what purpose you need to have another host, but
you could also set up another server.. xitami for example can be run
on WinNT 5, too

Air

5:26 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok look for the hosts file, in Win2000 it is located at C:\WINNT\system32\drivers\etc\hosts (I believe), it will probably just have one entry in it for "localhost", just follow the format to add the host names that you defined to Apache as virtual servers, and map each to 127.0.0.1 just as localhost is.

Now you can navigate to the virtual server by name, just as if you had a DNS entry.

PsychoTekk

5:32 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



yup works fine, at least for me :)

GrodanBoll

5:34 pm on Mar 10, 2002 (gmt 0)



So when i change in the 'host'-file I can add:
127.0.0.1:80 localhost
127.0.0.1:8008 localhost2

I can then use name-based virtual hosts?

/Grodan :)

PsychoTekk

5:36 pm on Mar 10, 2002 (gmt 0)

10+ Year Member




127.0.0.1 localhost
127.0.0.1 private.localhost

that's what my host file looks like
and this is my httpd.conf

NameVirtualHost *

<VirtualHost *>
DocumentRoot /www/private.localhost
ServerName private.localhost
</VirtualHost>

and it works fine

Air

5:48 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>So when i change in the 'host'-file I can add:
>127.0.0.1:80 localhost
>127.0.0.1:8008 localhost2

As in PsychoTekk's example the port is not required. The requests are all served on port 80. The host can be named whatever you want, and have as many hosts as you have defined in Apache. eg. www1.example.com, www.another-example.com then your host file would look like this:

127.0.0.1 localhost
127.0.0.1 www1.example.com
127.0.0.1 www.another-example.com

The only caution is to stay away from real registered domain names, because your hosts file takes precedence over DNS resolution, i.e. if you used www.yahoo.com as one of your host names and put it in your hosts file then you could never get to the real www.yahoo.com because it would resolve to 127.0.0.1 and your local Apache would try to serve content for that host.

(edited by: Air at 5:49 pm (utc) on Mar. 10, 2002)

GrodanBoll

5:49 pm on Mar 10, 2002 (gmt 0)



Hmmm...

Host-file:
127.0.0.1 localhost
127.0.0.1 wwwhkr.localhost
127.0.0.1 test.localhost

httpd.conf:
NameVirtualHost *

<VirtualHost *>
DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs"
ServerName localhost
</VirtualHost>

<VirtualHost *>
DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs/wwwhkr3"
ServerName wwwhkr.localhost
</VirtualHost>

<VirtualHost *>
DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs/test"
ServerName test.localhost
</VirtualHost>

[localhost...] woks fine but none of the others... ?

/Grodan

Air

5:51 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you reboot your machine after adding the entries to the Hosts file?

GrodanBoll

5:59 pm on Mar 10, 2002 (gmt 0)



I reebooted the webbserver but not the machine itself... i'll try that... :)

/Grodan

PsychoTekk

6:24 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



you don't need to reboot your machine, i think you need to
add "NameVirtualHost *" before each <VirtualHost *> ... </VirtualHost> tag

PsychoTekk

6:33 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



hm, no that ain't the problem, you just need that line once...

do you get a 404 from your apache or does your browser complain?

littleman

7:44 pm on Mar 10, 2002 (gmt 0)



I bet it is a problem with the host lookup, your httpd.conf looks fine. I tried your setup on my Linux box and it worked. I bet there is some setting in win2k that has to be set for the loop-back to work with names other than localhost.

amoore

8:05 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



I don't think the <virtualhost> directive can take a "*". Put in a name or an IP address. I can't tell that it matters which one, but put something in there besides a *.

gethan

8:22 pm on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I don't have control over the dns I use different ports ... eg: 80,81,8080 to run different virtual hosts.

Also DNS is a pain .. if I need another test server its quicker just to listen on a different port.

Listen 80
Listen 81
Listen (etc)

<VirtualHost *:80>
DocumentRoot "C:/Webfiles/host1/"
ServerName localhost
</VirtualHost>

<VirtualHost *:81>
DocumentRoot "C:/Webfiles/host2/"
ServerName localhost
</VirtualHost>

Just restart apache and voila.

HTH

littleman

8:23 pm on Mar 10, 2002 (gmt 0)



I tried it with the "*" and it worked ok for me -- though it might be different for the windows version of apache.

PsychoTekk

8:24 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



[httpd.apache.org...]
anything one need to know hehe

PsychoTekk

8:52 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



i like gethan's example best but unfortunately it does not seem to work
on win32-apaches... at least not on my Apache/1.3.20
as soon as i set apache up to listen to another port besides the default port,
listen 81 for example, in combination with
NameVirtualHost *

<VirtualHost *:81>
ServerName localhost2
</VirtualHost>
the virtual host works fine while the default wort doesn't work anymore :(

PsychoTekk

8:55 pm on Mar 10, 2002 (gmt 0)

10+ Year Member



things like these make me look forward to the day that i will
finally install linux on my machine :)

GrodanBoll

8:10 am on Mar 11, 2002 (gmt 0)



Hi again

Air & PsychoTekk: It works fine now. The problem was that in my '.htaccess'-file I had an old path that didn't work with the new VirtualHost:s. Sorry my error again :(

Thx everyone for your answers :D

/Grodan :)

gethan

10:35 am on Mar 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PsychoTekk:

You did put in the

listen portnum

And only NameVirtualHost * once?

In the main section of the httpd.conf file? Search for "Listen 80" and just add a couple of extra lines. It tells apache to use the extra ports as well - obviously don't choose a port that is used by something else. Works just fine on my apache on WinNT ... 80,81,82,83 etc.

Apache 1.3.23.

[erm sorry reread the post ;)]

[added]

You need to set up a virtual host for every port that you wish to use - including port 80 ;)

<VirtualHost *:80>
blah blah
</VirtualHost>

[/added]

This 34 message thread spans 2 pages: 34