|
Host more then one site on your Linux Apache web server using Virtual Hosts. This tutorial will walk you through the basics of how Apache and Vhosts work and include examples.
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.
#
#<VirtualHost *:80>
# ServerAdmin
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
# 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
#</VirtualHost>
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
<VirtualHost example.com>
ServerName example.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/example-com.log
</VirtualHost>
<VirtualHost website2.com>
ServerName website2.com
DocumentRoot /var/www/website2.com
ErrorLog /var/log/httpd/website2-com.log
</VirtualHost>
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).
<VirtualHost example.com> - 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 <VirtualHost *>, 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
I have found that when running a webserver with a large number of websites it just doesnt cut it to restart the server everytime you modify or ad a new VHost. Thus I found out that you can also make the VHosts active just reloading the server with the following command:
service httpd reload
Lets say for example you set everything up but it's not working properly. In my search for a debuggin method I found the following command:
apachectl -S
This command will dump out a description of how Apache parsed the configuration file and generaly what's wrong with your configuration. You can also try man apachectl for a complete list of options that come with this command.
- Install Photoshop on Ubuntu Linux
Believe it or not Photoshop CS2 is now fully supported on Ubuntu Linux using Wine. For a long period of time I was eager in switching to Ubuntu but the incompatibility of my most used Windows based applications always made me switch back to Linux. No more Windows based Photoshop skills, now you can powerup Photoshop on Ubuntu.
- TrustRank - building trust around a website
In this article I will be
discussing the TrustRank and how you can promote your website on a
different level, building trust around your website and investing in
what really matters.
- Display Date with JavaScript
In this tutorial it will be explained how to display the Date in your website using JavaScript.
The Date will be shown in the format Sunday, May 20 2007.
- Put a digital clock made with JavaScript on your website

This tutorial aims to explain how to create and put on your website a JavaScript digital clock.
- CSS Images Menu using transparency for your website
In this CSS tutorial it wil be explained how to create an awesome CSS menu using images and some transparency level.
Subscribe now via RSS feed and get all the new tutorials
|