Table of Contents
Overview of Apache HTTP Server
Apache HTTP Server (often just "Apache") is one of the most widely used web servers. In this chapter, the focus is on:
- Understanding core Apache concepts and terminology.
- Basic installation and service control.
- The high‑level structure of Apache’s configuration.
- Serving simple static content.
- Enabling and disabling basic modules and sites.
- Intro to logs and simple troubleshooting.
You will see Apache again in later chapters (e.g. for SSL/HTTPS and as a reverse proxy). Here we stay at a practical, foundational level.
Installing Apache
Package names differ slightly between distributions:
- Debian/Ubuntu:
apache2 - RHEL/CentOS/Rocky/Alma/Fedora:
httpd - openSUSE:
apache2
Typical installation commands:
- Debian/Ubuntu:
sudo apt update
sudo apt install apache2- RHEL/Fedora:
sudo dnf install httpd- openSUSE:
sudo zypper install apache2After installation, the main service name is usually:
apache2on Debian/Ubuntu.httpdon RHEL/Fedora/openSUSE.
Managing the Apache Service
Use systemctl to start, stop, and enable Apache at boot. Adjust apache2 vs httpd according to your distro.
- Start:
sudo systemctl start apache2 # or httpd- Stop:
sudo systemctl stop apache2- Restart (useful after config changes):
sudo systemctl restart apache2- Reload (apply config without dropping existing connections if possible):
sudo systemctl reload apache2- Enable at boot:
sudo systemctl enable apache2- Check status:
systemctl status apache2
If Apache fails to start or reload, status output and logs (see later) are your first checks.
Default Document Root and Test Page
After installation and service start, point your browser to:
http://localhost/when working locally.http://SERVER_IP/from another machine (ensure firewall allows HTTP, usually port 80).
Common default document roots:
- Debian/Ubuntu:
/var/www/html - RHEL/CentOS/Fedora:
/var/www/html - openSUSE:
/srv/www/htdocs
You can test Apache by creating a simple page:
echo 'Hello from Apache' | sudo tee /var/www/html/index.html
Then load http://localhost/ again. You should see that text.
Apache Configuration Layout
Exact paths differ by distribution, but the general ideas are consistent.
Main Configuration File
- Debian/Ubuntu:
/etc/apache2/apache2.conf - RHEL/CentOS/Fedora:
/etc/httpd/conf/httpd.conf - openSUSE:
/etc/apache2/httpd.confor/etc/apache2/default-server.conf
This file usually:
- Sets global options.
- Includes other configuration files and directories.
- Loads or references modules.
Avoid putting everything in this file. Instead, use the distro’s layout (sites, conf.d, etc.).
Included Directories
Common patterns:
- Debian/Ubuntu:
/etc/apache2/ports.conf— port bindings./etc/apache2/sites-available/— site (virtual host) configs (not live by default)./etc/apache2/sites-enabled/— symlinks to enabled sites./etc/apache2/mods-available/and/etc/apache2/mods-enabled/— modules and their configs./etc/apache2/conf-available/and/etc/apache2/conf-enabled/— generic config snippets.- RHEL/CentOS/Fedora:
/etc/httpd/conf.modules.d/— module load files./etc/httpd/conf.d/— additional configuration (virtual hosts, apps, etc.).- Main
httpd.confoften includes these directories.
Always check apache2ctl -M or httpd -M and main config for Include / IncludeOptional lines to understand what is actually loaded.
Basic Configuration Directives
Apache configuration is directive‑based. A directive usually has the form:
DirectiveName value1 value2 ...Basic, commonly seen directives:
ServerRoot— top of the Apache installation tree (usually set by package).Listen— IP/port to listen on:
Listen 80
Listen 0.0.0.0:80
Listen [::]:80ServerName— the canonical hostname of the server:
ServerName www.example.comDocumentRoot— directory containing files to serve for a particular context:
DocumentRoot /var/www/html
Directives often appear inside configuration blocks like <VirtualHost> or <Directory>, which limit their scope.
The `apachectl` / `apache2ctl` Utility
Apache provides a control script that can test configurations and control the server. The name depends on the distro:
- Debian/Ubuntu:
apache2ctl - RHEL/Fedora:
apachectl
Useful commands:
- Test configuration syntax:
sudo apache2ctl configtestor
sudo apachectl configtest
This should output Syntax OK if there are no parsing errors.
- List loaded modules:
apache2ctl -M
# or
httpd -M
These tools do not replace systemctl, but complement it.
Modules in Apache
Apache’s functionality is mostly provided by modules. Common ones:
mod_mime— map file extensions to MIME types.mod_dir— handle directory index files (likeindex.html).mod_alias— define URL path aliases and redirects.mod_rewrite— flexible URL rewriting.mod_ssl— HTTPS support.mod_proxyand friends — reverse proxying.
Enabling/Disabling Modules (Debian/Ubuntu)
Debian/Ubuntu provide helper tools:
- List enabled modules:
apache2ctl -M- Enable a module:
sudo a2enmod rewrite- Disable a module:
sudo a2dismod rewrite
Enabling a module usually creates symlinks from mods-available/ to mods-enabled/.
After changing modules, reload or restart:
sudo systemctl reload apache2Modules on RHEL/Fedora and Others
On RHEL/Fedora and many other distros, modules are loaded via LoadModule directives in:
/etc/httpd/conf.modules.d/*.confor- within
httpd.conf.
Example:
LoadModule rewrite_module modules/mod_rewrite.so
To "disable" a module, comment out or remove its LoadModule line and reload Apache.
Basic Site (Virtual Host) Configuration
Multiple websites can be served from a single Apache instance using virtual hosts. Apache supports:
- Name‑based virtual hosts: Different sites on the same IP/port, distinguished by
Host:header. - IP‑based virtual hosts: Different IPs, each serving a different site.
Here we focus on simple name‑based virtual hosts on port 80.
Common Structure of a Virtual Host
A direct <VirtualHost> block might look like:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>Key points:
<VirtualHost *:80>: Respond to connections on port 80 on any IP.ServerName: Primary domain for this virtual host.ServerAlias: Additional names pointing to the same site.DocumentRoot: Files to serve for this site.ErrorLogandCustomLog: Where to store logs;${APACHE_LOG_DIR}is usually defined by the distro.
NameVirtualHost *:80 is no longer needed in modern Apache 2.4+.
Debian/Ubuntu: `sites-available` and `sites-enabled`
Typical workflow:
- Create a new directory for your site content:
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com- Create virtual host config file:
sudo nano /etc/apache2/sites-available/example.com.confExample contents:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>- Enable the site:
sudo a2ensite example.com.conf- Test config and reload:
sudo apache2ctl configtest
sudo systemctl reload apache2
Apache uses symlinks in sites-enabled/ to determine which sites are active.
Disable a site with:
sudo a2dissite example.com.conf
sudo systemctl reload apache2RHEL/Fedora: `conf.d`
On RHEL/Fedora, you typically place virtual host definitions into /etc/httpd/conf.d/. Example:
sudo nano /etc/httpd/conf.d/example.com.conf
Insert similar <VirtualHost> content, then:
sudo apachectl configtest
sudo systemctl reload httpdDirectory and Access Control Basics
Apache has directives that control how it serves specific directories and URLs. Two common approaches:
- Using
<Directory>blocks in the main configs. - Using
.htaccessfiles (if allowed).
`<Directory>` Blocks
<Directory> targets filesystem paths. For example, to allow .htaccess to override certain settings:
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>Key points:
AllowOverridespecifies which directives are allowed in.htaccess.Allis permissive; you can restrict toNone,FileInfo,AuthConfig, etc.Require all grantedallows everyone to access this directory.Require all deniedblocks access.
`.htaccess` Files (Basics)
.htaccess files are per‑directory configuration files read on every request, if allowed by AllowOverride.
Example: simple redirect in .htaccess:
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
Use .htaccess only when you cannot edit the main server config (e.g. shared hosting). For performance and clarity, prefer <Directory> blocks in main configs where you have access.
Logs and Simple Troubleshooting
Apache logs are crucial for diagnosing problems.
Default Log Locations
Common defaults:
- Debian/Ubuntu:
/var/log/apache2/ - RHEL/CentOS/Fedora:
/var/log/httpd/
You’ll usually see:
access.log— successful/attempted requests.error.log— warnings, errors, failures.
Example (Debian/Ubuntu):
sudo tail -f /var/log/apache2/error.logUse this while testing new configs or debugging startup failures.
Reading Errors on Startup
When Apache fails to start or reload:
- Run:
sudo apache2ctl configtestor
sudo apachectl configtest- Check
error.logfor: - Syntax errors (missing
</VirtualHost>, typos in directives). - Port conflicts (e.g. “Address already in use: AH00072”).
- Missing files/directories (e.g. non‑existent
DocumentRoot).
Correct the issue, test again, then reload.
Basic Security and Performance Considerations (High Level)
Full hardening is covered elsewhere, but for Apache basics, be aware of:
- File permissions: The Apache user (often
www-dataorapache) must be able to readDocumentRootand any included files. Avoid running Apache asroot. - Directory listing: Disable automatic directory listing unless explicitly required. Example in a
<Directory>or<VirtualHost>:
Options -Indexes- Keep configuration organized: Use separate files per site and per application, especially when working with many virtual hosts.
Practical Checklist
To recap, a basic Apache setup usually involves:
- Install Apache via your system’s package manager.
- Start and enable the service with
systemctl. - Confirm default site works via browser.
- Create a new
DocumentRootfor your site. - Create a
<VirtualHost>configuration file: - Set
ServerName,ServerAlias, andDocumentRoot. - Optionally define dedicated
ErrorLogandCustomLog. - Enable the site (
a2ensiteon Debian/Ubuntu, or add.confinconf.don RHEL/Fedora). - Test configuration (
apache2ctl configtestorapachectl configtest). - Reload Apache.
- Check logs if anything misbehaves.
These basics provide a foundation upon which later chapters (virtual hosts in depth, SSL/HTTPS, reverse proxy configurations, performance tuning, and security hardening) will build.