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
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
Congratulations your vHost now has a domain lets add the www alias to it add the next line
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
Now apache knows where your files are
Once you configure your vHost exit your editor and SAVE YOUR CONFIG!!! then restart/reload apache
ServerAdmin - This provides a way for visitors to contact you about errors in your webserver
ErrorLog - This is where the ErrorLog for the specific Domain is held
CustomLog - This is more commonly used for access Logs
CustomLog /logs/yoursite1.com-access_log common
Well now we have a functioning vHost I guess this tutorial is over
Thank you for reading this guide and happy vhosting