Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Apache vHosts Explained


Hello,
Today we are going to talk about vHosts or Virtual Hosts within apache.

What is an Apache vHost?
Well put simply this is how apache finds a specific URL on a server with multiple web sites stored on it. A better explanation however is :
A Virtual Host utilizes a domain name to find the website associated with it on the server

What is a vHost useful for?
These are particularly useful if you run multiple websites on your server. For example
yoursite1.com is stored in /var/www/yoursite1
yoursite2.com is stored in /var/www/yoursite2
Apache will consult the vhost configuration to find what the domain is pointing to

Thats great but how do I enable them?
Well first we need to open the apache configuration file using your text editor (I will be using nano over SSH)
[root@keiran ~]# nano /etc/httpd/conf/httpd.conf
You will be greeted with something similar to this



Now you must find the line
#NameVirtualHost *:80
Simply remove the "#" Symbol then save and reload apache.

Now that Virtual Hosts are enabled how do I make a vHost?
Well its fairly simple. In this guide I will show you how to create a basic virual host. Scroll to the bottom of your apache configuration file and add the following tags

Code:

<VirtualHost *:80>

</VirtualHost>

Now we have our VirtualHost ready to create. Lets associate it with a domain.

Inside your <VirtualHost> block add the following line

Code:

ServerName yoursite1.com
Congratulations your vHost now has a domain lets add the www alias to it add the next line

Code:

ServerAlias www.yoursite1.com
as most people know www is a subdomain you could make it the ServerName and use the yoursite1.com as the Alias (You can make subdomains an ServerName also)

So far so good but how does apache know what path to point to? Good question Lets add the next line shall we :)

Code:

DocumentRoot /var/www/yoursite1
Now apache knows where your files are :)

Once you configure your vHost exit your editor and SAVE YOUR CONFIG!!! then restart/reload apache

OPTIONAL LINES!

ServerAdmin - This provides a way for visitors to contact you about errors in your webserver
ServerAdmin you@somedomain.com

ErrorLog - This is where the ErrorLog for the specific Domain is held
ErrorLog /logs/yoursite1.com-error_log
CustomLog - This is more commonly used for access Logs
CustomLog /logs/yoursite1.com-access_log common
Summary
Well now we have a functioning vHost I guess this tutorial is over :)

Thank you for reading this guide and happy vhosting
View Original Thread
Comments
0
Be the first to comment

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect
avatar
Tags: vhosts, apache, add, domain
DO NOT REPLY New: REQUEST_URI should be explained
Aug 11, 2010
https://issues.apache.org/bugzilla/show_bug.cgi?id=49741 Summary: REQUEST_URI should be explained Product: Apache httpd-2 Version: 2.2-HEAD Platform: PC OS/Version: Linux Status: NEW …

DO NOT REPLY New: Misleading typo in http://httpd.apache.org/docs/2.2/vhosts/examples.html
Jul 17, 2010
https://issues.apache.org/bugzilla/show_bug.cgi?id=49611 Summary: Misleading typo in http://httpd.apache.org/docs/2.2/vhosts/examples.html Product: Apache httpd-2 Version: 2.3-HEAD …

svn commit: r992810 [3/5] - in /httpd/httpd/trunk/docs/manual: ./ mod/ programs/ vhosts/
Sep 5, 2010
Modified: httpd/httpd/trunk/docs/manual/mod/quickreference.html.es URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/quickre ference.html.es?rev=992810&r1=992809&r2=992810&view=diff

svn commit: r987254 - /httpd/httpd/trunk/docs/manual/vhosts/details.xml
Aug 19, 2010
Author: poirier Date: Thu Aug 19 18:03:50 2010 New Revision: 987254 URL: http://svn.apache.org/viewvc?rev=987254&view=rev Log: Overhaul the detailed doc for virtual hosts: - remove references to implementation details - re-order information to…

svn commit: r987255 - in /httpd/httpd/trunk/docs/manual: ./ developer/ mod/ vhosts/
Aug 19, 2010
Author: poirier Date: Thu Aug 19 18:05:20 2010 New Revision: 987255 URL: http://svn.apache.org/viewvc?rev=987255&view=rev Log: Update transforms and add some html and meta files generated from new pages Added: …

svn commit: r992810 [5/5] - in /httpd/httpd/trunk/docs/manual: ./ mod/ programs/ vhosts/
Sep 5, 2010
Modified: httpd/httpd/trunk/docs/manual/programs/httpd.xml.ko URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/programs/ht tpd.xml.ko?rev=992810&r1=992809&r2=992810&view=diff

svn commit: r992810 [4/5] - in /httpd/httpd/trunk/docs/manual: ./ mod/ programs/ vhosts/
Sep 5, 2010
Modified: httpd/httpd/trunk/docs/manual/mod/quickreference.html.ko.euc-kr URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/quickre ference.html.ko.euc-kr?rev=992810&r1=992809&r2=992810&view =diff

svn commit: r992810 [1/5] - in /httpd/httpd/trunk/docs/manual: ./ mod/ programs/ vhosts/
Sep 5, 2010
Author: sf Date: Sun Sep 5 16:01:15 2010 New Revision: 992810 URL: http://svn.apache.org/viewvc?rev=992810&view=rev Log: update transformations Modified: httpd/httpd/trunk/docs/manual/logs.html.en …

svn commit: r992810 [2/5] - in /httpd/httpd/trunk/docs/manual: ./ mod/ programs/ vhosts/
Sep 5, 2010
Modified: httpd/httpd/trunk/docs/manual/mod/quickreference.html.en URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/quickre ference.html.en?rev=992810&r1=992809&r2=992810&view=diff

svn commit: r989156 - in /httpd/httpd/branches/2.2.x/docs/manual/vhosts: examples.html.en examples.xml
Aug 25, 2010
Author: rbowen Date: Wed Aug 25 15:01:36 2010 New Revision: 989156 URL: http://svn.apache.org/viewvc?rev=989156&view=rev Log: Add link to main vhosts doc from the top of the example doc, for folks who don't know what a vhost is. Modified: …