Examples

Examples

Understanding Apache directives is very easy through demonstrating how programming scripts work. Now, let’s say that your main website is named thisismywebsite.com and you will host another website named thisismywebsite2.com. Basically, your main virtual host configuration will be similar to this.

Listen 80 NameVirtualHost thisismywebsite.com <VirtualHost thisismywebsite.com> ServerName thisismywebsite.com DocumentRoot /var/www/html ErrorLog /var/log/httpd/thisismywebsite-com.log </VirtualHost>

<VirtualHost thisismywebsite2.com> ServerName thisismywebsite2.com DocumentRoot /var/www/thisismywebsite2.com ErrorLog /var/log/httpd/thisismywebsite2-com.log </VirtualHost>

The line "listen 80" means that the Apache server must only simulate what is on port 80. The second directive, NameVirtualHost, is the command to name the main virtual host, which is in this case, named thisismywebsite.com. This page will show once a computer user requests for thisismywebsite.com in the status bar. If the user types an invalid request connected to the website, for instance, thisismywebsite.com/profile, the main virtual host will be served if the request does not exist in the directory.

The directive, ServerName thisismywebsite.com is the main command of Apache Virtual Hosts. Once the Apache receives request from users for such domain, the domain will be searched in the roster of virtual hosts. The command DocumentRoot /var/www/html will subsequently execute and show the files from this directory.

The last directive, ErrorLog /var/log/httpd/thisismywebsite2-com.log is a command for a file wherein error logs will be kept. /VirtualHost is the closing tag in Virtual Hosting, closing the roll of commands and configurations.