Skip to main content
Advertisement

Apache HTTPD Installation and Basic Setup

Apache HTTPD (referred to as Apache) is one of the oldest web servers in the world. It supports a wide variety of operating systems and offers excellent module-based extensibility. This chapter covers installation on Ubuntu, CentOS, Windows, and Docker, along with the directory structure, MPM selection, and service management commands.


Understanding MPM (Multi-Processing Module)

Before installing Apache, you need to understand MPM. The MPM is the core module that determines how Apache handles requests in parallel.

MPMMethodCharacteristicsRecommended For
PreforkMulti-process (no threads)Independent process per request, stable, mod_php compatiblePHP + mod_php
WorkerMulti-process + multi-threadBetter memory efficiency, requires thread-safe modulesMedium scale
EventWorker improvement (async Keep-Alive)Manages Keep-Alive connections in a dedicated thread, high performance** Currently recommended**
# Check currently active MPM
apache2ctl -V | grep MPM
# or
httpd -V | grep MPM

Ubuntu / Debian Installation

sudo apt update
sudo apt install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2

After installation, visit http://your-server-ip to see the "Apache2 Ubuntu Default Page."

Change MPM (Prefork → Event)

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
apache2ctl -V | grep MPM
# Server MPM: event

Enable SSL Module

sudo a2enmod ssl
sudo systemctl reload apache2

CentOS / RHEL / Rocky Linux Installation

sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

MPM Configuration (CentOS)

# /etc/httpd/conf.modules.d/00-mpm.conf
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
# LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule mpm_event_module modules/mod_mpm_event.so

macOS Installation (Homebrew)

brew install httpd
brew services start httpd
# Config: /opt/homebrew/etc/httpd/httpd.conf (Apple Silicon)

Running Apache with Docker

docker run -d --name apache -p 80:80 httpd

docker run -d \
--name apache \
-p 80:80 \
-v $(pwd)/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro \
-v $(pwd)/html:/usr/local/apache2/htdocs:ro \
httpd

docker exec apache apachectl graceful

docker-compose.yml:

version: '3.8'
services:
apache:
image: httpd:2.4-alpine
container_name: apache
ports:
- "80:80"
- "443:443"
volumes:
- ./conf/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro
- ./html:/usr/local/apache2/htdocs:ro
restart: unless-stopped

Apache Directory Structure

Ubuntu/Debian

/etc/apache2/                     ← Configuration file root
apache2.conf ← Main configuration file
ports.conf ← Listen port settings
mods-available/ ← Available modules
mods-enabled/ ← Enabled modules (symbolic links)
sites-available/ ← Virtual host configs (including disabled)
000-default.conf ← Default virtual host
sites-enabled/ ← Active virtual hosts (symbolic links)
conf-available/ ← Additional configs
conf-enabled/ ← Active additional configs

/var/www/html/ ← Default web root
/var/log/apache2/ ← Log directory
access.log
error.log

CentOS/RHEL

/etc/httpd/
conf/httpd.conf ← Main config
conf.d/ ← Additional configs (*.conf auto-loaded)
conf.modules.d/ ← Module loading configs

/var/www/html/ ← Default web root
/var/log/httpd/ ← Log directory

Service Management Commands

systemctl

sudo systemctl start apache2       # Start
sudo systemctl stop apache2 # Stop
sudo systemctl restart apache2 # Restart (brief connection drop)
sudo systemctl reload apache2 # Graceful reload (zero-downtime, recommended)
sudo systemctl status apache2 # Check status
sudo systemctl enable apache2 # Enable auto-start on boot

apachectl / apache2ctl

sudo apache2ctl graceful           # Graceful restart (completes in-flight requests)
sudo apache2ctl graceful-stop # Graceful stop
sudo apache2ctl configtest # Validate configuration syntax
sudo apache2ctl -V # Version + compile options
sudo apache2ctl -M # List active modules
sudo apache2ctl -S # Virtual host summary

Key: Use graceful instead of restart. graceful replaces workers only after in-flight requests complete — zero downtime.


Summary

ItemUbuntuCentOSNote
Package nameapache2httpdSame software
Config root/etc/apache2//etc/httpd/Different structure
Web root/var/www/html//var/www/html/Same
Logs/var/log/apache2//var/log/httpd/Different path
Control commandapache2ctlapachectlSame functionality
Recommended MPMEventEventBest performance
Advertisement