Chapter 26. Apache

Table of Contents
26.1. Introduction
26.2. Installation
26.3. User directories
26.4. Virtual hosts

26.1. Introduction

Apache is the most popular web server since April 1996. It was originally based on NCSA httpd, and has grown into a full-featured HTTP server. Slackware Linux currently uses the 1.3.x branch of Apache. This chapter is based on Apache 1.3.x.

26.2. Installation

Apache can be installed by adding the apache package from the "n" disk set. If you also want to use PHP, the php ("n" disk set) and mysql ("ap" disk set) are also required. MySQL is required, because the precompiled PHP depends on MySQL shared libraries. You do not have to run MySQL itself. After installing Apache it can be started automatically while booting the system by making the /etc/rc.d/rc.httpd file executable. You can do this by executing:


# chmod a+x /etc/rc.d/rc.httpd

The Apache configuration can be altered in the /etc/apache/httpd.conf file. Apache can be stopped/started/restarted every moment with the apachectl command, and the stop, start and restart parameters. For example, execute the following command to restart Apache:


# apachectl restart
/usr/sbin/apachectl restart: httpd restarted

26.3. User directories

Apache provides support for so-call user directories. This means every user gets web space in the form of http://host/~user/. The contents of "~user/" is stored in a subdirectory in the home directory of the user. This directory can be specified using the "UserDir" option in httpd.conf, for example:


UserDir public_html

This specifies that the public_html directory should be used for storing the web pages. For example, the web pages at URL http://host/~snail/ are stored in /home/snail/public_html.

26.4. Virtual hosts

The default documentroot for Apache under Slackware Linux is /var/www/htdocs. Without using virtual hosts every client connecting to the Apache server will receive the website in this directory. So, if we have two hostnames pointing to the server, "www.example.org" and "forum.example.org", both will display the same website. You can make separate sites for different hostnames by using virtual hosts.

In this example we are going to look how you can make two virtual hosts, one for "www.example.org", with the documentroot /var/www/htdocs-www, and "forum.example.org", with the documentroot /var/www/htdocs-forum. First of all we have to specify which IP addresses Apache should listen to. Somewhere in the /etc/apache/httpd.conf configuration file you will find the following line:


#NameVirtualHost *:80

This line has to be commented out to use name-based virtual hosts. Remove the comment character (#) and change the parameter to "BindAddress IP:port", or "BindAddress *:port" if you want Apache to bind to all IP addresses the host has. Suppose we want to provide virtual hosts for IP address 192.168.1.201 port 80 (the default Apache port), we would change the line to:


NameVirtualHost 192.168.1.201:80

Somewhere below the NameVirtualHost line you can find a commented example of a virtualhost:


#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    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>

You can use this example as a guideline. For example, if we want to use all the default values, and we want to write the logs for both virtual hosts to the default Apache logs, we would add these lines:


<VirtualHost 192.168.1.201:80>
        DocumentRoot /var/www/htdocs-www
        ServerName www.example.org
</VirtualHost>

<VirtualHost 192.168.1.201:80>
        DocumentRoot /var/www/htdocs-forum
        ServerName forum.example.org
</VirtualHost>