This post includes a short snippet and some explanation about how you can use Virtual Hosts with Apache.
Why use a Virtual Host
To run multiple sites with a single Apache install you can use Virtual Hosts.
This is a feature where Apache will load different webserver configurations depending on the hostname that is requested. To configure this you can add a Virtual Host in the Apache vhosts file.
How to add a Virtual Host
By default Apache normally includes a
httpd-vhosts.conf file which you would place the virtual host data in.
An example virtual host entry is below.
CustomLog "logs/testing.local-access.log" common
Here the first line of the tag defines what hostname and port will be matched for this entry. Instead of defining a hostname you can use * to match any hostnames for the given port.
The entries inside this VirtualHost tag are mostly optional but define your new website. Any requests for
testing.local at your webserver on port 80 will trigger this virtual host and be loaded.
Here I define a few tags:
- ServerAdmin – An email address that will be used by your server. This is sometimes printed when there is an error.
- DocumentRoot – The location of the files for this host. These files will be served when the user loads the site.
- ServerName – The name of the server this Virtual Host is hosting.
- ServerAlias – You may specify a number of additional server aliases. In this case www.testing.local is also defined as an alias.
- ErrorLog – This configures the error logs to be output to a custom Virtual Host specific log file.
- CustomLog – This configures a custom logger to be output to a custom Virtual Host specific log file.
Once added, you will just need to reboot your webserver and it will be ready to use.