Most Apache performance and security work comes down to one decision: what modules are loaded. Every module is code in the request path or in the parent process. The fewer modules loaded, the smaller the attack surface, the smaller the memory footprint per worker, and the easier the configtest output is to read. This article is the module management workflow we use on Debian and Ubuntu Apache hosts — the helpers, the file layout, and how to audit the loaded set.
How to verify
Two questions to answer every time you touch modules: what is loaded right now, and what is intended to be loaded? They should be the same answer.
sudo apachectl -M | sort
ls /etc/apache2/mods-enabled/ | sort
diff <(apachectl -M | awk '{print $1}' | sort) <(ls /etc/apache2/mods-enabled/ | grep '\.load$' | sed 's/.load$//' | sort)
sudo apache2ctl configtest
apachectl -M is the runtime truth — what the daemon has actually loaded. The mods-enabled/ directory is the on-disk intent. A diff between them surfaces modules loaded by an include that bypassed the helper, or modules that should be loaded but are not because of a missing .load symlink.
What’s happening
On Debian-derived distros, every Apache module has two files in /etc/apache2/mods-available/:
<name>.load— theLoadModuledirective.<name>.conf— optional default configuration for the module.
a2enmod <name> creates symlinks from mods-enabled/ to the matching .load (and .conf if present). a2dismod <name> removes those symlinks. The helpers also handle dependencies — enabling proxy_http will prompt to enable proxy first because the second module depends on the first. The symlink approach means the canonical config lives in one place and the enabled state is a separate, version-controllable artifact.
The same model applies to:
a2ensite/a2dissite— for vhosts undersites-available/andsites-enabled/.a2enconf/a2disconf— for global snippets underconf-available/andconf-enabled/.
If you SSH into an Apache box from Red Hat (RHEL, CentOS, Rocky), none of these helpers exist — RPM-based distros use a single httpd.conf plus conf.modules.d/*.conf files that ship with the httpd package and its sub-packages. The mental model translates, but the commands do not.
The procedure
-
Decide whether you need the module at all. A useful default list for a modern PHP/static workload behind Let’s Encrypt:
mpm_event(the worker),headers,rewrite,ssl,http2,proxy,proxy_fcgi,setenvif,mime,deflate,expires,socache_shmcb,alias,autoindex(only if directory listing is desired).
Anything beyond that list deserves justification.
-
Enable a module. The helper handles symlinks and prompts for restart vs reload depending on whether the module requires a restart:
sudo a2enmod http2 sudo apache2ctl configtest sudo systemctl restart apache2mod_http2is one of the modules that needs a full restart, not just a reload, because it interacts with the MPM. The helper tells you which. -
Disable a module. Symmetrically:
sudo a2dismod autoindex sudo apache2ctl configtest sudo systemctl reload apache2If a vhost or
.htaccessreferences a directive provided by the module you just disabled, the configtest fails with “Invalid command” pointing at the offending line. Fix or comment the line before the reload. -
Audit the active set during change windows. Capture the baseline before and after changes; check it into the config repo:
apachectl -M | sort > /etc/apache2/.loaded-modules.txtAny subsequent unexplained drift in that file is visible in
git diff. -
For multiple modules at once, list them in one command. The helper accepts a list:
sudo a2enmod rewrite headers expires deflate sudo a2dismod status infomod_statusandmod_infoship loaded on some installs and expose/server-statusand/server-info. Disable them or restrict access if you have not deliberately configured the endpoints. -
Use Ansible or your CM tool to make the state declarative. A community-maintained pattern: a list of “enabled modules” and “disabled modules”, and the playbook reconciles them with the symlink state. This removes the “did someone a2enmod something last week” question.
Common pitfalls
- Forgetting to restart or reload after
a2enmodis the single most common failure mode — the symlink exists, the configtest passes, but the running daemon does not know about the change. The helper prints a reminder; do not ignore it. - Some modules conflict — only one MPM can be loaded. Switching from
mpm_preforktompm_eventrequiresa2dismod mpm_preforkbeforea2enmod mpm_event, or the configtest fails with “MPM defined twice”. See MPM event vs prefork. mod_php(when present) forcesmpm_preforkbecause the bundled PHP SAPI is not thread-safe. Moving to PHP-FPM lets you switch tompm_eventand gain workers.- Disabling
mod_setenvifbreaks manyBrowserMatchrules that older config snippets rely on for legacy IE workarounds — read the configtest output, do not blindly disable. - On a security review,
mod_status,mod_info, andmod_userdirare the three most commonly-loaded-by-default modules that should be explicitly justified or disabled.
Stack Harbor manages the loaded-module set as a versioned artifact across every Apache host under management, with reconciliation in CM and drift alerts in monitoring. That discipline is part of the Managed Operations baseline — small, predictable surface area; everything else is a deliberate exception.