Apache HTTP Server Tutorial

By Xah Lee. Date:

This is a basic tutorial of Apache HTTP web server.

Install

On Ubuntu Linux, do sudo apt-get install apache2.

To read the man page, do man apache2. (on some systems, apache2 is named httpd)

To see your version, type apache2 -v.

By default, it's installed at /usr/sbin/apache2. Type which apache2 to find the path on your machine.

Release Dates

summary of release dates

Start / Stop Apache

  1. Type sudo apachectl start to start the server.
  2. Launch a browser, goto http://localhost/ and you should see “It Works!”.
  3. Type sudo apachectl stop to stop the server.

General Description

The server program is named apache2 (on some distros, it's named httpd).

When the server program starts, it runs as a background process (aka daemon), and this process is the master process. The master process spawns (usually 5) worker processes. The worker processes serve HTTP requests.

Normally, you start/stop Apache by a control script named apachectl.

When Apache starts, first thing it does is to read a config file. Apache's behavior is controlled by its config file.

Config file controls Apache's behavior. For example, whether to run CGI, whether to list dir, URL rewrite/redirect, log location , log format, virtual hosts, MIME types, compression, ban IP address, etc.

Config files are made of mostly lines, and they are called “directives”.

Apache can be built (compiled) with many modules. Many modules are compiled-in, but Apache can also load modules dynamically.

Each module allows some features, and also has its own config file syntax.

To master Apache, 90% are about understanding the config file.

Get Info from Compiled Binary

Sometimes you need to know what modules or parameters is your server compiled with. Type sudo apache2 -V.

◆ sudo apache2 -V

Server version: Apache/2.2.22 (Ubuntu)
Server built:   Nov  8 2012 21:37:51
Server's Module Magic Number: 20051115:30
Server loaded:  APR 1.4.6, APR-Util 1.3.12
Compiled using: APR 1.4.6, APR-Util 1.3.12
Architecture:   32-bit
Server MPM:     Worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/worker"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"
apache2 -l
List modules compiled into the server.
◆ apache2 -l

Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  worker.c
  http_core.c
  mod_so.c

Scripts and Locations

in general, you need to be root to control web server. So, either switch to root sudo su root or precede your commands with sudo.

apache2
/usr/sbin/apache2, the main binary executable, the server. man apache2.

Note: it's named “apache2” instead of “apache” because “apache” is the version 1.x branch.

Note: apache2 binary executable is sometimes named as httpd on some systems. For example, on this official doc page https://httpd.apache.org/docs/2.2/programs/httpd.html, it's the same as man apache2.

apachectl
/usr/sbin/apachectl, a control script for start/stop etc. man apachectl.

Note: “apachectl” and “apache2ctl” are the same thing. /usr/sbin/apachectl is a link to /usr/sbin/apache2ctl

Config File Location

By default, config file is at /etc/apache2/apache2.conf

This location is compiled-into the program. You can find the location by sudo apache2 -V. Here's a part of a sample output:

◆ sudo apache2 -V

  …
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

It is also possible to start Apache by specifying a different config file location, by sudo apache2 -f config_path. This is not recommended.

the config file may have “include” statement to include other config files. They are usually all in this dir /etc/apache2. Here's a sample dir content:

apache2.conf
conf.d
envvars
httpd.conf
magic
mods-available
mods-enabled
ports.conf
sites-available
sites-enabled

Web Doc Root Location

The documentation root dir is specified in your config file.

By default, it's usually at /var/www/.

You can find the location by looking for the directive “DocumentRoot”. For example, cd /etc/apache2, then sudo grep -r DocumentRoot *. Sample output:

◆ sudo grep -r DocumentRoot *

sites-available/default-ssl:    DocumentRoot /var/www
sites-available/default:        DocumentRoot /var/www
sites-enabled/000-default:      DocumentRoot /var/www

auto-start, sys init script location

auto start script /etc/init.d/apache2

start and stop

basics:

here's sample ps output showing Apache's processes.

◆ ps -ef | grep apache

root     11202     1  0 18:38 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 11203 11202  0 18:38 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 11204 11202  0 18:38 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 11206 11202  0 18:38 ?        00:00:00 /usr/sbin/apache2 -k start

Apache's master process takes unix signals to start/stop etc. You normally shouldn't just force kill it such as kill -9 pid because that'll stop it uncleanly.

the following signals are meaningful to Apache: {TERM, USR1, HUP, WINCH}. You can send signal to the master process by sudo kill -signal_name pid, or use wrapper script apachectl, like this:

apache2ctl start
Start the server.
apache2ctl stop (sending TERM)
Stop the server.
apache2ctl restart (sending SIGHUP)
Restart the server.
apache2ctl graceful (sending SIGUSR1)
Gracefully restarts. (current connections are not aborted)
apache2ctl graceful-stop (WINCH)
Gracefully stop. (current connections are not aborted)

If your config file has syntax error, Apache won't start. You can check by sudo apachectl configtest. That'll check for basic syntax error, but won't catch all errors.

Config File Syntax and Semantics

See official doc. https://httpd.apache.org/docs/2.2/

Getting PHP to Work

this section is work in progress.

to install PHP module, do: sudo apt-get install libapache2-mod-php5

After that, restart Apache and it should work.

you can test by creating a file named test.php at the doc root dir. The content can be this:

<?php
phpinfo();
?>