August 9, 2018 admin

The evolution of an Apache virtual host

 

The simplest virtual hosts

<VirtualHost *:80>

ServerName example.com   
DocumentRoot /var/www/example.com
   
</VirtualHost>

When an HTTP request comes into the IP address and Port 80, the Apache server will detect the hostname in the request and route the request to the directory declared in the DocumentRoot.

Adding www

<VirtualHost *:80>

ServerName example.com
ServerAlias www.example.com   
DocumentRoot /var/www/example.com
   
</VirtualHost>

It is still not uncommon for people to search for domains by adding www, for example http://butlerraines.com and http://www.butlerraines.com.

To make them both route to the same place we simply add a ServerAlias declaration in our virtualhost. Now when someone visits http://butlerraines.com or http://www.butlerraines.com they will be served the same files.

This is of course assuming you have set up your DNS to point www to your IP address.

Serving files from a user’s home directory.

<VirtualHost *:80>

ServerName example.com
ServerAlias www.example.com   
DocumentRoot /home/user1/example.com/public_html
 

</VirtualHost>

Writing Apache error and access logs to a user’s home directory.

<VirtualHost *:80>

ServerName example.com
ServerAlias www.example.com   
DocumentRoot /home/user1/example.com/public_html
 
#Log file locations
  LogLevel warn
  ErrorLog  /home/user1/example.com/log/error.log
  CustomLog /home/user1/example.com/log/access.log combined
</VirtualHost>

Leave a Reply

Your email address will not be published. Required fields are marked *