Wednesday, November 5, 2008

Virtual Hosts on Apache

Virtual Hosts on Apache
The term Virtual Host (Vhost) refers to the practice of hosting a number of websites on a single webserver, serving diffrent content for each website.

Lets say for example you needed to setup a webserver for your companys website. Now asume that your company decides it needs to host a second website. In this scenario one may asume that you need to go buy and set-up a new web-server. That is not the case, with Apache's Vhost functionality it becomes a easy task to setup more the one website on the same server.

Understanding how Apache works
Before one can actually start seting up Vhosts its a good practice to understand the way they work and how Apache handles the process.

When a user is typing an URL address in the web browser and hits the Go button that address is translated to an IP address used for locating the webserver. That is a request and its being sent to the webserver for processing. When the Apache webserver recieves this request it checks the headers to determin the name of that address. This is the most important thing in the whole process, Apache needs to know the actual address.

When Apache is started on Linux, it loads its configuration file (usually located in /etc/httpd/conf/httpd.conf). This is the main configuration file for Apache, and this is also the place to add Virtual Hosts. Reading the httpd.conf file, Apache creates a list of its IP addresses and Virtual Hosts.

When Apache recieves the request for a web page it checks its lists to determine if a Virtual Host for that domain exists and what directory it should serve. If there are no VHosts for that domain, it will serve the default DocumentRoot of the server, usually locate in /var/www/html. However, if Apache is configured with VHosts and a ServerName match is found, it will server the containing files of the folder specified in the VHost.
Creating a basic Virtual Host
After you get your Apache server up and running you can start setting up the VHosts. Using your favorite text-editor openup the httpd.conf file usually locate in /etc/httpd/conf/httpd.conf.
Lets say you are using the VI editor, type the following command:

vi /etc/httpd/conf/httpd.conf

You will notice a large file, filled with configuration variables. Scroll down to the end of the file (you can do that faster by pressing Page_Down) untill you find the following text:

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#


Ther you have it, an official VHost example, located in the httpd.conf file.
The "#" charactes in front of every line are comment-out characters this means those lines dont affect the Configuration, they are there just for helping purposes.

As you may notice a new term called DIRECTIVE is mentioned. A directive are Apache commands (or variables) processed by the server. The directives in this case are: ServerAdmin, DocumentRoot, ServerName, ErrorLog and CustomLog. This directives are the main ones we are going to use and they are self explanatory.

Now lets asume that your main website is named example.com and the second website is website2.com. Your main VHost configuration will be like this:

Listen 80
NameVirtualHost example.com

ServerName example.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/example-com.log



ServerName website2.com
DocumentRoot /var/www/website2.com
ErrorLog /var/log/httpd/website2-com.log


Dont get scared, we will clear everything up right now.Listen 80 - this will tell Apache to listen only on port 80.

NameVirtualHost example.com - this is the name of the main Virtual Host, your main web-server name (this could also be replaced by the server's IP address or by the * charachter).
- this is the starting tag of the first VHost, your main server domain, that will be served when a user types example.com. If for example Apache recieves a request for a domain that is not matched in the VHosts, it will serve files from the first VHost, example.com. This field could also be replaced by , wich basicly means the same thing but I prefer keeping things organized in my http.conf file and I always use this notation.
ServerName example.com - this is the main directive of Apache VHosts. When Apache recieves a request for a domain, that domain name will be searched in this lists of VHosts.
DocumentRoot /var/www/html - if the ServerName is located in the VHost, Apache will start serving files from this directory, thus if example.com is requested, files from /var/www/html will be served.
ErrorLog /var/log/httpd/example-com.log - this is the place where error logs will be stored. Its a good practice to keep your error logs separated for everydomain, thus when you will encounter a problem with a website you will only need to debug a single error log file.
< /virtualhost> - the ending tag of a Virtual Host.
In order for the VirtualHost to take effect the server (httpd) needs to be restarted or reloaded.
Use the follwing command to restart Apache:
Service httpd restart

1 comment:

Unni said...

Great article...thanks for the info...i think this is something that every web developer should understand basically...especially LAMP developers...seems like you arenot writing any posts these days..hope you will continue to write good tips...