KrnlPanic's Linux Notes and Tips

Working with linux since kernel version 2.0.30

Apache Virtual Hosts

Apache Virtual Hosts

Apache is an open source web server that is currently dominating the market because of it’s low cost (nothing) and ease of use and configuration. There is a large support database available from www.apache.org and the source is freely available from the same site. Apache also offers ease of use for Perl, PHP, JServ (Java Servlets) and CGI scripts.

There are two ways to do webhosting, IP Based Virtual Hosting which binds one IP address to a specific URL and Name-Based Virtual Hosting which allows multiple domain names to operate on one IP.

Installing:
Hopefully, you have already realized that you need to have Apache installed before going any further. Click here if you need instructions on how to install Apache.

Configuring:
cd to the apache directory (/usr/local/apache).
There are several subdirectories (conf, bin, logs). Start in the conf directory. Edit the file httpd.conf using the command ‘vi conf/httpd.conf’. If the vi editor is foreign to you, you can brush up on editor commands with the vi editor tutorial

There are 2 ways to run apache, IP based Virtual Hosting and Name-Based Virtual Hosting.

The NameVirtualHost directive
-=-=-=-=-=-

NameVirtualHost 192.168.1.10:8080

REQUIRED for Name-based virtual hosting. (A * can be used to specify that the server is only doing Name-Based Virtual Hosting). The line above tells httpd to only run name based virtual hosts on 192.168.1.10 port 8080.

The following tells httpd to listen for name based virtual hosts on all interfaces

NameVirtualHost *

The BindAddress Directive
-=-=-=-=-=-
For IP Based hosting, you need to add each IP that apache will listen on.

BindAddress 192.168.1.20 192.168.1.25 10.0.0.1

NOTE: The IPs MUST be the IP bound to the interfaces, and IPs from multiple interfaces can be used. (A * can be used for all IPs on a given machine)

The Directive
-=-=-=-=-
This works the same for IP and name based virtual hosting, with the exception of the ServerName and ServerAlias being absent with IP Based Virtual Hosting

Examples
-=-=-=-==-

  • Name-Based Virtual Hosting
  • #The following VirtualHost directive will answer for domain.com and *.domain.com (* being a wildcard)


    ServerAdmin admin@domain.com
    ServerName domain.com
    ServerAlias *.domain.com
    DocumentRoot /home/user/www
    ErrorLog logs/domain.com-error_log
    TransferLog logs/domain.com-access_log
    ScriptAlias “/cgi-bin/” /home/user/www/cgi-bin

    # This Virtual Host will answer for circle.com, square.com and triangle.com. It *WILL NOT* answer for www.circle.com, www.square.com or www.triangle.com.


    ServerAdmin admin@circle.com
    ServerName circle.com
    ServerAlias square.com
    ServerAlias triangle.com
    ErrorLog logs/circle.com-error_log
    TransferLog logs/circle.com-access_log
    ScriptAlias “/cgi-bin/” /home/circle/www/cgi-bin

  • IP Based Virtual Hosting
  • #This example will serve up trapezoid.com content to anyone browsing 10.0.0.1


    ServerAdmin admin@trapezoid.com
    ErrorLog logs/trapezoid.com-error_log
    TransferLog logs/trapezoid.com-access_log
    DocumentRoot /home/trapezoid/www
    ScriptAlias “/cgi-bin/” /home/trapezoid/www/cgi-bin

    Starting and Stopping
    -=-=-=-=-=-=-
    Start:
    cd /usr/local/apache/bin
    ./apachectl start

    Stop:
    cd /usr/local/apache/bin
    ./apachectl stop

    Status of currently running server:
    cd /usr/local/apache/bin
    ./apachectl status