Apache - Virtual Host (Vhost)

About

virtual host configuration with Apache

How to set Up a Virtual Host

httpd.conf

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

Example:

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

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.

where:

  • servername is the FQDN with optionally the port. See doc

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.

Debug

With the httpd cli, you can check the virtual host

Example:

httpd -t -D DUMP_VHOSTS
*:80                   is a NameVirtualHost
         default server localhost (C:/Apache24/conf/extra/httpd-vhosts.conf:33)
         port 80 namevhost localhost (C:/Apache24/conf/extra/httpd-vhosts.conf:33)
         port 80 namevhost doku (C:/Apache24/conf/extra/httpd-vhosts.conf:57)
*:443                  localhost (C:/Apache24/conf/extra/httpd-ahssl.conf:142)

Documentation / Reference


Powered by ComboStrap