Apache Configuration Basics
Apache Configuration Basics Apache HTTP Server is the most widely deployed web server in the world. Its configuration is file-driven — understanding the httpd.c…
Apache Configuration Basics
Apache HTTP Server is the most widely deployed web server in the world. Its configuration is file-driven — understanding the httpd.conf structure, MPM selection, and service management is the foundation of running Apache in production.
httpd.conf Structure & Core Directives
The main config file is typically at /etc/apache2/apache2.conf (Debian/Ubuntu) or /etc/httpd/conf/httpd.conf (RHEL/CentOS). It uses a directive-based syntax inside block containers.
# Core server identity directives
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@example.com
# Document root — where your site files live
DocumentRoot "/var/www/html"
# Directory block — controls access to a path
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All # Allow .htaccess to override directives
Require all granted # Apache 2.4 access control (replaces Order/Allow/Deny)
</Directory>
# Restrict access to a sensitive directory
<Directory "/var/www/html/admin">
Require ip 192.168.1.0/24 # Only allow internal network
</Directory>
# Options values:
# Indexes — show directory listing if no index file exists
# FollowSymLinks — follow symbolic links
# ExecCGI — allow CGI scripts
# None — disable all options
# -Indexes — disable Indexes specifically
# Listen on multiple ports
Listen 80
Listen 443
# Include additional config files
Include conf.d/*.conf
IncludeOptional sites-enabled/*.confMPM — Multi-Processing Modules
The MPM controls how Apache handles concurrent connections. Choosing the right one impacts performance, memory usage, and PHP compatibility.
# Check active MPM
apachectl -V | grep MPM
apache2ctl -V | grep "Server MPM"
# prefork — one process per connection; required for non-thread-safe PHP (mod_php)
# worker — threads + processes; better memory efficiency
# event — like worker but handles keep-alive connections in a dedicated thread (best for modern workloads)
# Configure event MPM in /etc/apache2/mods-available/mpm_event.conf
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
# Switch MPM on Ubuntu/Debian
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
# On RHEL/CentOS edit /etc/httpd/conf.modules.d/00-mpm.conf
# Comment out LoadModule mpm_prefork_module and uncomment mpm_event_moduleService Management
# systemctl (modern — use on all systemd distros)
sudo systemctl start apache2 # Start
sudo systemctl stop apache2 # Stop
sudo systemctl restart apache2 # Full restart (drops connections)
sudo systemctl reload apache2 # Graceful reload (no dropped connections)
sudo systemctl enable apache2 # Auto-start on boot
sudo systemctl status apache2 # Show status + recent logs
# apachectl / apache2ctl (cross-platform alternative)
sudo apachectl start
sudo apachectl graceful # Graceful restart (waits for in-flight requests)
sudo apachectl configtest # Validate config syntax BEFORE reloading
sudo apachectl -t # Same as configtest
sudo apachectl -S # Show parsed virtual host config
# RHEL/CentOS uses httpd instead of apache2
sudo systemctl restart httpd
sudo apachectl configtest
# Log files
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
tail -f /var/log/httpd/error_log # RHEL/CentOS path
# Custom log format
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
CustomLog /var/log/apache2/access.log combined
ErrorLog /var/log/apache2/error.log
LogLevel warn