Beginners guide in Virtual Hosts on Apache
|Written by DanielRo|
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.
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 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.
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.
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:
Ther you have it, an official VHost example, located in the httpd.conf file.
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:
Dont get scared, we will clear everything up right now.
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.
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:
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.
written by Girdha , July 20, 2007
written by Krish , May 20, 2008
written by brandon , July 08, 2008
for some reason both of my virual hosts will not work only the first one i have done it exactly like this and have tried other sites no luck.
written by kishore , July 23, 2008
how to run static files using Apache
written by mountaindrummer , January 23, 2009
I have the same issue as Brandon above. I have my httpd VH section laid out the exact same way as the example and traffic from both urls get routed to the index.html inside the /var/www/html directory (the index.html in the subdir for the other site is ignored). It's almost as if the DocumentRoot parm is getting ignored. Anybody have any ideas?
Do you need more help? Ask now!
|Last Updated ( Saturday, 23 June 2007 )|