Apache - Virtual Host

1 - About

Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. If you want to maintain multiple domains/hostnames on your machine you can setup VirtualHost containers for them. Most configurations use only name-based virtual hosts so the server doesn't need to worry about IP addresses.

3 - How to setting Up a Virtual Host

3.1 - httpd.conf

in the Apache Configuration file (apache/conf/httpd.conf), uncomment the line “Include conf/extra/httpd-vhosts.conf”

# Virtual hosts
conf/extra/httpd-vhosts.conf

3.2 - Syntax

Below is an example of Apache configuration, where a new virtual host is added in the httpd.conf file.

<VirtualHost *:80>
  ServerName gerardnico.local
  DocumentRoot "c:/www/"
  DirectoryIndex index.php
  <Directory "c:/www/">
    AllowOverride All
    Allow from All
  </Directory>
  Alias /alias /alias_directory
  <Directory "/alias_directory">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

In the example above, if you want that a call to gerardnico.com respond on your computer, you must change your DNS or you local Network - Hosts File. In this way, a ping will show you computer as responder.

3.2.1 - Farm Syntax

<VirtualHost *:8080>                     # this only works for a specific port
    ServerName gerardnico.local
    ServerAlias *.gerardnico.local       # this works for all subdomains of gerardnico.local
    DocumentRoot "f:/www/farmer"           
</VirtualHost>
 
<VirtualHost *>                          # this works for all ports
    ServerName blog.gerardnico.local     # this only works for one specific subdomain
    DocumentRoot "f:/www/farmer"           
</VirtualHost>

Suppress the comments (#) in the configuration file, they are not allowed in the xml node.

4 - Documentation / Reference

apache/virtual_host.txt · Last modified: 2015/10/25 09:33 by gerardnico