Coral: apache vhosts

From The Uniform Server Wiki
Jump to navigation Jump to search

Apache Vhosts

The Uniform Server is configured for name-based virtual hosting. This allows you to run any number of web sites from a single IP address. Each domain-name you host must first be registered and an “A” record created on a DNS server. This “A” record translates the domain-name to your IP address.


Note: For testing, “A” records are simulated using a PC's hosts files.

UniServer 8-Coral
  Home
  Quick Start
  General
» Apache
  MySQL
  PHP
  MSMTP
  CRON
  DtDNS
  Db Backup
  Perl
  Main Index

Features

The main features of The Uniform Server’s Vhost configuration menu are:

  • Include separate log files and server alias
  • Auto enable when first vhost created
  • Auto disable when last vhost deleted
  • Port tracking follows main server port (defaults to port 80)
  • Writes host name to Windows hosts file when vhost created
  • Deletes host name from Windows hosts file when vhost deleted

Overview

Vhosts are configured using a separate configuration file UniServer\usr\local\apache2\conf\extra\httpd-vhosts.conf.

This file requires enabling in Apache’s main configuration file UniServer\usr\local\apache2\conf\httpd.conf

Search this file for the following line:< br />

#Include conf/extra/httpd-vhosts.conf

Remove the hash "#" to enable as shown below:< br />

Include conf/extra/httpd-vhosts.conf

Note: The above is automatically performed when using The Uniform Server’s Vhost configuration menu.

Top

Inform Apache to use Vhosts

Apache requires the directive NameVirtualHost address:port which instructs it to use virtual hosts and resolve an address on the specified port. The Uniform Server uses *:80, where the * is a wildcard meaning use all addresses. Port 80 is the standard web server listening port. This can be changed if required, for example, to port 8080.

A separate NameVirtualHost directive is required for each different port used. These NameVirtualHost directives must have a corresponding listening directive. The Uniform Server’s Vhost configuration file contains the following block:


 #--US_START--------------Do not remove
 NameVirtualHost *:80
 #--US_END----------------Do not remove

The "Do not remove" Lines are used by The Uniform Server to locate the block and insert lines as appropriate.
The NameVirtualHost directive has a matching Listen directive Listen 80 located in the main configuration file so it is not required in this configuration block.

However, adding a virtual host that uses port 8080, a listen directive is required. The configuration block becomes:

 #--US_START--------------Do not remove
 Listen 8080
 NameVirtualHost *:80
 NameVirtualHost *:8080
 #--US_END----------------Do not remove

The main configuration file (httpd.conf) does not contain a Listen 8080 directive and it is required and added to this configuration block along with the NameVirtualHost directive.


Note: These are automatically added when using The Uniform Server’s Vhost configuration menu.

Top

Adding a Vhost

Each virtual host requires a separate configuration block. These blocks inherit their configuration from the main configuration file httpd.conf. Specifying an Apache directive in a Vhost block overrides that of the main configuration file. The advantage of this inheritance is to reduce number of Apache directives required for each Vhost. The absolute minimum required is to specify a DocumentRoot and ServerName.


Apache resolves a Vhost by matching its port and ServerName. On finding a match, the server's content is fetched from the folder specified by DocumentRoot. If it cannot find a match, it uses the first Vhost defined (the default). Note: to avoid conflicts, Apache uses a _default_ directive for the address of this first virtual host.


Example: to add a virtual host for fred.com on port 8080 and to serve content from folder UniServer\vhosts\fred, the minimum configuration required is:

 #--US_START--------------Do not remove
 Listen 8080
 NameVirtualHost *:80
 NameVirtualHost *:8080
 #--US_END----------------Do not remove

 <VirtualHost _default_:80>
  DocumentRoot C:/Coral/UniServer/www
  ServerName localhost
 </VirtualHost>

 <VirtualHost *:8080>
  DocumentRoot C:/Coral/UniServer/vhosts/fred
  ServerName fred.com
 </VirtualHost>

The default Vhost maps to The Uniform Server’s folder www. If you wish, this can be changed as well as the server name.


The new Vhost fred.com maps to folder UniServer\vhosts\fred. This can be any folder you wish. All vhost root folders are created in the folder vhosts. This is not a strict requirment, but it is provided for portability (as when using a USB memory stick).


Note: Port 8080 is shown as an example. Generally all Vhosts would use the standard port 80.

Top

Vhost additional Apache directives

Other Apache directives can be included in a Vhost block. Common directives are shown in this example block:


<VirtualHost *:80>
  ServerAdmin webmaster@fred.net
  DocumentRoot C:/Coral/UniServer/vhosts/fred_net
  ServerName fred.net
  ServerAlias www.fred.net  *.fred.net
  ErrorLog logs/fred.net-error.log
  CustomLog logs/fred.net-access.log common
</VirtualHost>

ServerAlias Allows you to access the server using sub-domains. Each sub-domain must have a corresponding DNS entry.


Each Vhost can have a separate ErrorLog and CustomLog log file. If you do not specify these log files, the main server log files are used.


Note: Separate log files will eat into your file allocation resources, depending on the total number of Vhosts you wish to host. This may become an issue .

Apache Vhosts

This menu allows you to quickly create virtual host blocks.

UniController: Server Configuration > Apache > Apache Vhosts

Creating a Vhost requires only two form entries, a server root folder and a host name. After entering these, click the confirm button. All other directives are optional, and are automatically filled in with values based on the above two form entries. Clicking the Create Vhost button creates the root folder if it does not exist. A Vhost is created in the Vhost configuration file and an entry is written to your PC’s host file. Here is a summary of the steps:

  • A3) Either replace *** with the folder name you wish to use as your server root folder or click Select Folder button and navigate to an existing folder and select it.
  • A4) Enter your server name (host name). This is the domain name you registered with a registrar, for example fred.com. It is the name you enter in a browser to access your web site (excluding the http:// part).
  • A13) Click the Confirm button. This enables the greyed out buttons and fills in form fields with selected values for other Apache directives. Change or delete these as appropriate.
  • A10) Click Create Vhost button to create your virtual host.

Note 1: After creating a Vhost you must restart your Apache server otherwise Apache will not recognise the new configuration.

Note 2: For detailed information see Apache Vhosts

Top

Vhost Configuration Menu Example

For clarity following example shows Vhost form filled in and the corresponding Vhost block created in the configuration file for host name fred.com and server root folder fred.

The block created in httpd-vhosts.conf

<VirtualHost *:80>
  ServerAdmin webmaster@fred.com
  DocumentRoot C:/UniServer/vhosts/fred
  ServerName fred.com
  ServerAlias www.fred.com  *.fred.com
  ErrorLog logs/fred.com-error.log
  CustomLog logs/fred.com-access.log common
</VirtualHost>

The entry created in host file

 127.0.0.1 fred.com

Note: An entry will be created in the List of Hosts (B1). After restarting Apache and returning to this menu you can click an entry. Clicking an entry enables the greyed out buttons, allowing you to perform the following tasks:

  • A9) The Virtual host setup form is filled in allowing you to edit the Vhost. Click the Update Vhost button to apply your modification.
  • B2) To remove a Vhost, click the Delete Vhost button. It deletes the Vhost block from the configuration file and removes the entry in the Hosts file. The root folder and all its content are deleted, however you are requested to confirm this action before it is implemented.
  • B3) View In Browser opens default browser and displays the Vhost selected.

Top