Page 1 of 1

[Apache] vHosts Explained A guide on vhosts ad how to create them Rate Topic: -----

#1 Keiran  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 06-December 09

Posted 06 December 2009 - 03:21 PM

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

Posted Image

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

<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

ServerName	 yoursite1.com


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

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 :)

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

Is This A Good Question/Topic? 1
  • +

Page 1 of 1