If you are an Apache user for the hosting of either internal or external web sites then you know how flexible this web server can be. But did you know you can host more than one site on that server? You can thanks to Virtual sites. You can host virtual sites based on either IP address or Name. If IP addresses are in short supply the best choice, of course, is name-based virtual hosting.
In this article I will show you how to add name-based virtual hosts in your Apache web server. This will require you to edit the /etc/httpd/conf/httpd.conf file so be prepared for some command-line action. NOTE: This file can be located in various locations
If you've never edited your httpd.conf file don't worry, it's not that difficult. Just make sure you use good comments so you know what you did and where you did it. Otherwise your virtual host section could get lost in the other sections.
The section of Apache where your virtual hosts will go is called, aptly, Virtual Hosts. The format of an entry looks like this:
CustomLog logs/dummy-host.example.com-access_log common
Before you actually start configuring your virtual host you need to make sure you have the NameVirtualHosts line uncommented out. The line will look like:
You can find the line by using the grep command like so:
grep -n NameVirtualHosts /etc/httpd/conf/httpd.conf
(NOTE: Edit the path to your httpd.conf file to reflect your setup.)
The grep command will return to you which line the NameVirtualHost entry is on. Now open up the Nano editor and scroll down a bit. You can hit the key combination Ctrl-c to find out what line you are on. Once you find the line you will remove the "#" character (no quotes).
With that out of the way you are ready.
To illustrate how this is done I will show you how to create a virtual server for mail. The following assumes the doc root is /var/www/. Add the following entries to your httpd.conf file:
# Other directives here
# Other directives here
NOTE: Where yourcompany.com is the actual FQDN you use.
NOTE: If you need to change your port you can change it to suit your needs from the default port 80.
The above is the bare minimum configuration for your virtual servers. There are a LOT of possible directives you can use. For example, you might want to add error logging to these virtual hosts for debugging purposes. To add error logs to your virtual mail host? you would add the lines:
ErrorLog /var/log/httpd/mail-error_log common
CustomLog /var/log/httpd/mail-access_log common
To your mail virtual host entry (above the </VirtualHost> line.)
Once you have these entries complete you will need to save the httpd.conf file and restart Apache. To restart Apache issue one of the following commands:
You're done. You should now be able to hit the new virtual hosts.
Creating virtual hosts is a quick way to expand the capabilities of your Apache server, cut costs, and save IP addreses. Have you ever deployed virtual hosts with Apache? If so, share your experiences.Advertisement
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.