10.7: Virtual Hosts and multiple web servers

Oct 13, '11 07:30:00AM

Contributed by: swarten

After a great deal of searching the net for answers I have pieced together what is needed to do a couple of things I have wanted to do for some time now. The first is to have virtual hosts working nicely on 10.7 Lion Server. The second and related item is to have multiple web servers within a LAN accessible from one WAN address.

Virtual Hosts

To get this working add the following line to the /etc/apache2/httpd.conf file and then restart the web service.

NameVirtualHost *:80

Restarting the web service can be done in the Terminal using:

sudo serveradmin stop web
sudo serveradmin start web


Now you can add virtual hosts through the Server app.

Multiple web servers within a LAN accessed from one WAN address.

Set up: The following assumes that you have 3 servers with correctly working DNS and apache services. The 10.10.10.x subnet is used for the examples, change them to whatever configuration you are using.

Set up a NAT rule on your router/modem to point port 80 through to, for example, 10.10.10.200 (or the address of the primary apache service that will redirect domains to their correct machine):

DNS on the primary machine should be set up as follows: External DNS needs to be set up that points first.domain.com, second.domain.com and third.domain.com to your external IP address where your web server is located such as 210.55.102.xx (remember this can take up to 48 hours to become live).

Once this is done you can start adding the .conf files to the /etc/apache2/sites folder to do the redirecting.

You can name the files as you like so long as they have the .conf extension.

1st File I will call 0000_any_80_first.domain.com.conf it needs the following in it to do the redirect to the correct machine:
<VirtualHost *:80>
ServerName first.domain.com
ProxyPreserveHost on
ProxyPass / http://server1.domain.com/
ProxyPassReverse / http://server1.domain.com/
</VirtualHost>
2nd File is called 0000_any_80_second.domain.com.conf containing:
<VirtualHost *:80>
ServerName second.domain.com
ProxyPreserveHost on
ProxyPass / http://server2.domain.com/
ProxyPassReverse / http://server2.domain.com/
</VirtualHost>
3rd File is called 0000_any_80_third.domain.com.conf containing:
<VirtualHost *:80>
ServerName third.domain.com
ProxyPreserveHost on
ProxyPass / http://server3.domain.com/
ProxyPassReverse / http://server3.domain.com/
</VirtualHost>
Restart the web service with the Terminal command:

sudo serveradmin stop web
sudo serveradmin start web


[crarko adds: I haven't tested this one. This might also be possible to do in Lion client, but the hint as is applies to Lion Server.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2011101123595425