greenMind homepage about / projects / clients / services / contact    
 
 

< Adding virtual hosts and IPs to Apache with Linux >

< Virtual hosts add additional web sites to your web server.>

<

Note: this work was done using Red Hat Linux version 6.2

Purpose: You may have a web server that needs to host several web sites. Your web server will traditionally have one IP address to communicate around the internet. Using the following instructions, you can add and assign new virtual IP addresses and create virtual web sites on your server.

You know that you've got your Webby-award winning site, www.jimmyjoejoe.com at the IP address of xxx.xx.xx.aaa. But what if you want to serve www.bigfunkybooty.com on the same machine? If you have another address available, you can do it this way.

I'm a total Linux wanna-be. I can admit that. I've done a fair amount of playing with Linux boxes, I can recompile Apache with the best of them, blah blah blah, open source, yey, I read Slashdot every day and laugh uncomfortably at all of the esoteric humor that I couldn't possibly understand without an entire O'Reilly library at my fingers, but when the going gets intense, I shut off Hyperterminal and run back to my Win98 PC, revelling in the clunky Windows gui-licious delight.But an hour later, I'm back reading In the Beginning There Was the Command Line, and chuckling madly about the latest AYBABTU sighting.

My point here is, you don't have to be the great Linus himself to get this going. If you can chmod, and more importantly, you have a bit of patience to check out the support offered on the Apache web site, you can do this. As far as I can see, there are four basic steps involved.

  1. Add an IP address(alias) to the box
  2. Reconfigure Apache using the Vhost container
  3. Stop n start the web server
  4. Everybody's surfin'

You need root access to your box. If you don't have the root password, see if you can social engineer it from your friend the systems administrator (offer drugs, food, or beer).

You could certainly hurt both Apache and your server if you screw up. It is not for the faint of heart, small children, or those who spasm without a Wizard interface. Please, don't try this on a production web server if you don't know what you are doing!!!

1. Add a virtual IP address to this box.

The term "virtual IP" is borrowed from Microsoft. Sorry, you'll just have to deal with that. It is also called an IP alias.

You can check IP addresses on the machine using netstat:

# netstat rn
Kernel IP routing table

Destination

Gateway

Genmask

Flags

MSS

Window

irtt

Iface

xxx.xx.xx.aaa

0.0.0.0

255.255.255.255

UH

0

0

0

eth0

xxx.xx.xx.0

0.0.0.0

255.255.255.0

U

0

0

0

eth0

127.0.0.0

0.0.0.0

255.0.0.0

U

0

0

0

lo

0.0.0.0

207.92.33.140

0.0.0.0

UG

0

0

0

eth0

xxx.xx.xx.aaa is the address for www.jimmyjoejoe.com

If your IP address is not there, you must add it using ifconfig.

# ifconfig eth0 configures internet interface

You will need the ip_alias module for assigning multiple IPs to a box; if not, you must recompile the kernal. This is standard equipment in the Linux Kernel versions 2.0.x and 2.2.x, and will most likely be loaded already.

To create the IP alias, type the following:


# /sbin/ifconfig eth0:0 xxx.xx.xx.xxx

# /sbin/route add -host xxx.xx.xx.xxx dev eth0:0

# ifconfig eth0:0 xxx.xx.xx.xxx

where eth0:n (n = int) for the number of the alias.

For each additional alias, repeat the above steps, changing eth0:0 to eth0:1, eth0:2 eth0:3, and so on.

Check ifconfig eth0 once more, and it will list the following for your spanking new IP alias:

eth0:0 Link encap:Ethernet HWaddr 00:80:5F:92:78:AF

inet addr:xxx.xx.xx.xxx Bcast: xxx.xx.xx.xxx Mask: xxx.xx.xx.xxx

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Interrupt:3 Base address:0x700

If you run netstat again, you will see your new IP address there as well:

# netstat -rn

Destination

Gateway

Genmask

Flags

MSS

Window

irtt

Iface

xxx.xx.xx.bbb

0.0.0.0

255.255.255.255

UH

0

0

0

eth0

xxx.xx.xx.aaa

0.0.0.0

255.255.255.255

UH

0

0

0

eth0

207.92.33.0

0.0.0.0

255.255.255.0

U

0

0

0

eth0

127.0.0.0

0.0.0.0

255.0.0.0

U

0

0

0

lo

0.0.0.0

xxx.xx.xx.yyy

0.0.0.0

UG

0

0

0

eth0

Ahah, your new IP address, xxx.xx.xx.bbb is available!

2. Configuring vhosts in Apache:

Chdir to /usr/local/bin/apache/conf, and open up httpd.conf in your favorite text editor. I prefer pico, because I am a Unix wimp.

You will need to play with the virtual host container. Use control + w to search for VirtualHost. There are a whole set of instructions there, as well as some examples that have been commented out.

NewHostName ###.##.##.bbb

ServerName= blobby.org

DocumentRoot /home/home1/htdocs/blobbydomain/

Save httpd.conf and exit the text editor.

3. Stopping and restarting the web server.

To stop apache, type the following:

# kill -TERM `cat /usr/bin/local/apache/logs/httpd.pid`, or whatever the path to the httpd.pid file is.

Now let's make sure that your virtual host is configured. Type http S, and you should get the following message:

VirtualHost configuration:

xxx.xx.xx.bbb:80 is a NameVirtualHost default server www.blobby.org (/usr/bin/local/apache/conf /httpd.conf:911) port 80 namevhost www.blobby.org (/usr/bin/local/apache/conf/httpd.conf:911)

Finally, (oh rapture), you can type httpd, and Apache will fire right back up to business.

Important: if you need to reboot, you'll want this data included in the automatic boot sequence.
Create a file (say rc.virtual) in the /etc/rc.d directory and add the following two lines to it. The file should read like this:
#!/bin/sh
/sbin/ifconfig eth0:1 192.168.1.200
/sbin/route add -host 192.168.1.200 dev eth0:1
Mark it executable (chmod +x rc.virtual), then edit the file rc.local in the same directory.
At the end of that file, add the line /etc/rc.d/rc.virtual to run it when it reboots.

If you have told the appropriate authorities (i.e. Register.com, Network Solutions, your firewall) where to find blobby.org, you should be able to see your beautiful new URL within seconds!

>