So,
you want to set up a Apache server using more than
one domain name and only one IP address. No problem!!
Well, I say no problem, but it took me a while to
figure it out and I'm here to tell you what I learned
to save you some time and trouble. I am using Apache2
on a Red Hat 8.0 system, however I will keep this
as generic as possible without leaving out any of
the details. First, we will edit the Apache httpd.conf
by hand using your favorite text editor, then I will
show you how to do it with a GUI called apacheconf.
To go straight to the apacheconf instructions, click
here. Depending on your Linux distribution, you should
find the httpd.conf file in /etc/httpd/conf or /etc/apache/conf.
First
things first. Be sure to copy your clean or working
httpd.conf file to a safe place on your server. This
provides you with a back-up if you mangle the copy
of your httpd.conf file that you are working on. Just
move the clean or working copy of your httpd.conf
file back into your configuration directory and you're
back in business. Take it from me, that little tip
has pulled me out of more jams than I can count!
Now,
lets get to work. As root, open your httpd.conf file
in your favorite text editor. There's all kinds of
good stuff in this file, but don't worry about that.
We will only be making changes to a small portion
of the code. From the top of the httpd.conf file,
scroll down a little way till you see a line that
will say "Listen". This is where you are
telling the server what IP address and port number
to use. There are two acceptable formats to use here.
The first acceptable way is to list your IP address
followed by a colon and the port number. Apache's
default port number is 80. So if your IP address is
12.34.56.78 and Apache is running on port 80, then
the line should look like this;
Listen
12.34.56.78:80
If
you have more than one IP address, you can just add
an asterisk then the hyphen and then the port number.
The asterisk is what is known as a wildcard in many
operating systems. This would mean that it will listen
to all IP's connected to that server. Once again,
using port 80 it should look like this:
Listen
*:80
Scrolling
down a little further, you should find a directive
that looks something like this:
DirectoryIndex
index.html index.htm index.shtml
This
directive determines what files Apache will look for
when a request is made to a directory. This is the
"index" or "home" page automatically
served if found in a directory without having to name
the file specifically. This will make sense later,
for now, just make sure it is not commented.
Continue
scrolling down the file till you see a section called
Name Virtual Host. By defaultm this entry will have
a # symbol in front of it, which comments the line
out. Remove the # symbol to activate this directive.
This directive enables virtual hosting by associating
an IP address with domain names you intend to host
on your Apache webserver. Using our previous examples,
if your IP address is 12.34.56.78, the directive should
look like this:
NameVirtualHost
12.34.56.78
Now
comes the fun part. Scroll down till you see a section
that says Virtual host. This is where you define each
virtual host for each domain name that you will be
serving. There are many options you can use in a Virtual
host directive. It can require as few as three options
to just make it work or many more options depending
on what you want to do. In the first section you need
to enter a virtual host directive. If you've ever
coded HTML, it looks like an HTML tag with your server's
IP address entered into it. It should look like this;
<VirtualHost
12.34.56.78>
Below
that you need to define the web-root directory for
the domain you intend to host. This is where you will
be keeping the all of this web site's html files and
images and it is also where the server will be looking
for the requested pages to serve to your site's many
adoring fans. Apache keeps its files in the /var/www/html
or on some systems just /var/www directory so that's
where we start. Make a directory inside the /var/www/html
(or /var/www) directory for each of your domain names
and I would suggest naming them something dealing
with the site for easy reference. For example, if
your domain is www.BigFeet.org, then name the directory
"bigfeet". To create that directory, use
the command
mkdir
/var/www/html/bigfeet
For
simplicity, make sure that this directory is "world
read/write-able" by setting the permissions with
chmod to 666. This is not the most secure configuration
for the directory, but you can set permissions after
you get Apache running properly.
Now
you have to tell Apache that when someone goes to
www.BigFeet.org that it is to go to /var/www/html/bigfeet
and serve the requested page from there. So under
your
<VirtualHost
12.34.56.78>
tag,
add this:
DocumentRoot
/var/www/html/bigfeet
Under
that you need to add a section called ServerAdmin.
This is an Apache setting that gives an administrator
email address particular domain. This address appears
on some server-generated pages, such as error documents.
The entry for this setting should look like this:
ServerAdmin
You@Youremail.com
Next,
we'll tell Apache what your domain name is. So below
the ServerAdmin line add this:
ServerName
bigfeet.org
Now
there are many other options that you can add to new
lines in this section like a custom 404 Page Not Found
screen (ErrorDocument 404 "http://www.BigFeet.org/missing.html"),
but we wont get into too much of that here.
Just
like in html coding, you have to close your tags.
As it stands, we still have an open <VirtualHost>
tag dangling around up there, and we cant have that
can we? Nope, we sure cant, so lets close it. You
do that just like you would in an html document. On
a new line add:
</VirtualHost>
That's
it!! One domain name down. Each domain has to have
its own virtual host section. So for each new domain
name you have to add all the info from <VirtualHost
12.34.56.78> to </VirtualHost>. Below is
a quick sample of a server running 2 domains. One
called www.BigFeet.org and one called www.SmellyCarpet.com
(Don' ask!). They should look like this:
#
Virtual host Virtual Host 1
<VirtualHost
12.34.56.78>
DocumentRoot /var/www/html/bigfeet
ServerAdmin webmaster@bigfeet.org
ServerName bigfeet.org
DirectoryIndex index.html index.html index.htm index.shtml
ErrorDocument 404 "http://bigfeet.org/missing.html"
</VirtualHost>
#
Virtual host Virtual Host 2
<VirtualHost
12.34.56.78>
DocumentRoot /var/www/html/smellycarpet
ServerAdmin webmaster@smellycarpet.com
ServerName smellycarpet.com
DirectoryIndex index.html index.html index.htm index.shtml
ErrorDocument 404 "http://smellycarpet.com/missing.html"
</VirtualHost>
There
you go. That's the the quick and the short of setting
Apache up to use Virtual Hosting. Happy Serving!
|