Apache: Name Based Hosting
Server Training - Web Server

There are basically two possibilities for Virtual Hosting. One is Name Based Hosting which uses one IP Address for all of the domains that are on the server. Once a domain name is registered and setup the web server will serve the correct domain name despite being on the same IP Address. IP based hosting requires a separate IP Address for each server. This tutorial will help you set up the Name Based Hosting on an Apache Web Server.

If you want to learn about Ubuntu Name Based Hosting CLICK HERE.    The reason is that Ubuntu and CentOS are configured differently.  The documentation on this page is for centOS.

In order to use Name Based Hosting you will need to use the NameVirtualHost directive and supply the IP Address, this must be a public IP Address for the Internet. The VirtualHost directives enclose each virtual host:

Configuration Directives

<VirtualHost 192.168.5.64>

 

</VirtualHost>

Used to enclose a group of directives that will impact only one virtual host.

The address must be:
1. IP Address of the virtual host
2. fully qualified domain name for the virtual host
3. * which will with NameVirtualHost * match all IP Addresses
4. _default_ which when no IP Address from a virtual host is matched will show up

 

Each Virtual Host must correspond to a different IP address, different port number or a different host 
name for the server. If you use different IP Addresses the server machine must be configured to accept
IP packets for multiple addresses.


When using IP-based virtual hosting, the special name _default_ can be specified in which case this virtual host will match
any IP address that is not explicitly listed in another virtual host. In the absence of any _default_ virtual host the "main" server config,
consisting of all those definitions outside any VirtualHost section, is used when no IP-match occurs.

 

Live Class Descriptions:
Linux Server Management - course focus on running a Linux Server correctly (Ubuntu 8.04 or CentOS 5), see class outline below.
Linux Server Daemons - course focus on step-by-step install of servers (Ubuntu 8.04 or CentOS 5), installation and management of web servers, DNS servers, mail servers, DHCP,SSH, FTP,etc.

Self-Teach Packages:
Linux Server Management - course focus on running a Linux Server correctly (Ubuntu 8.04 or CentOS 5), see class outline below.
Linux Server Daemons - course focus on step-by-step install of servers (Ubuntu 8.04 or CentOS 5), installation and management of web servers, DNS servers, mail servers, DHCP,SSH, FTP,etc.



NameVirtualHost Directive
This is the IP that is used to accept requests for the name-based virtual hosts. This must be designated.


Specify a port number
NameVirtualHost 192.168.5.3:82

Beware that if you want to change the port number form port 80, you will need to modify the settings for SELinux and open that port number
in your firewall. You of course also need to edit your httpd.conf.


Receive Requests on all Interfaces
NameVirtualHost *
The IP from NameVirtualHost must match the virtual hosts.

NameVirtualHost 192.168.5.3

<VirtualHost 192.168.5.3>

</VirtualHost>

Main Host
If you decide to use virtual hosting you will have to configure the first virtual host and the main host. The main host is where a request will go if it cannot be fulfilled on other virtual servers.


ServerName Directive
This directive sets the hostname and the port that the server will use. This will be used when creating a redirection for URLs. If the server was web.example.com but the DNS alias was
www.example.com and you want the server to look like www.example.com then you would set up this:


ServerName www.example.com:80

This will afford the best stability and dependability.

ServerAlias Directive
The ServerAlias will allow you to setup alternative alias for the server.

<VirtualHost *>
ServerName web.example.com
ServerAlias server
</VirtualHost>
ServerPath


Check Virtual Host Settings
httpd -S

This will list the virtual hosts and provide feedback about the syntax.

 

Here is an example of several named based setups.


NameVirtualHost 192.168.5.56 ###note this is a private IP Address in the example

<VirtualHost 192.168.5.56>

DocumentRoot /var/www/htdocs/webone/html

ServerName www.webone.com

ErrorLog /var/www/htdocs/webone/logs/error_log

</VirtualHost>



<VirtualHost 192.168.5.56>

DocumentRoot /var/www/htdocs/webtwo/html

ServerName www.webtwo.com

ErrorLog /var/www/htdocs/webtwo/logs/error_log

</VirtualHost>

There are several parts to note with Named Based setups. DocumentRoot is the location on the server where the main documents or web pages for the domain must be placed. The example shows:

/var/www/htdocs/webone/html

/var/www/htdocs/webtwo/html

This DocumentRoot directory must be declared in this directive. It could be other places on the server, for example:

/user/local/htdocs/webone

or

/home/webone/htdocs/webone

However, SELinux expects all web documents to be in the /var/www/ directory. This means that either, you turn SELinux off or you modify the settings in order to work with SELinux.


The important thing to do is create a pattern so you can place these directories in a partition or directory that can be backed up and it is easier to configure.

PLAN AHEAD

When the server is created you must know where you will locate the web services. If the /var directory will be used it must be large enough to hold all of the web services as well as a large number of growing logs. If the /usr/local directory will be used it too must have the resources to grow to the organization’s needs.

The ServerName is also important to declare in the directive. The domain name for the specific server must be declared. This declaration may be with the www included or not.

The ErrorLog determines where the errors for a specific domain will be located. It is most valuable to have logs in the same directory as the domain. Place the logs directory at the same level as the html directory. Here is an example:

/var/www/htdocs/myserver1/html

/var/www/htdocs/myserver1/logs/

Typically, logs are placed in /var/log/httpd unless you have a number of virtual hosts which you may want the logs to be separate then. It is often safer to not place the logs directory in the same directory as the html pages that users will view. This mainly reminds you that the logs directory should not be read by any user except apache.

The actual http.conf or apache2.conf if you are using Ubuntu 8.04 section for Virtual Hosts is listed below.

 

### Section 3: Virtual Hosts

#

# VirtualHost: 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. This is indicated by the asterisks in the directives below.

#

# Please see the documentation at

# <URL:http://httpd.apache.org/docs/2.2/vhosts/>

# for further details before you try to setup virtual hosts.

#

# You may use the command line option '-S' to verify your virtual host

# configuration.



#

# Use name-based virtual hosting.

#

#NameVirtualHost *:80

#

# NOTE: NameVirtualHost cannot be used without a port specifier

# (e.g. :80) if mod_ssl is being used, due to the nature of the

# SSL protocol.

#

#

# VirtualHost example:

# Almost any Apache directive may go into a VirtualHost container.

# The first VirtualHost section is used for requests without a known

# server name.

#

#<VirtualHost *:80>

# ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it

# DocumentRoot /www/docs/dummy-host.example.com

# ServerName dummy-host.example.com

# ErrorLog logs/dummy-host.example.com-error_log

# CustomLog logs/dummy-host.example.com-access_log common

#</VirtualHost>

Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874