Skip to content

Enabling and disabling Apache modules on Debian and Ubuntu

How a2enmod and a2dismod actually work, when to use them, and how to keep your loaded-module set as small as the workload truly needs.

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 — the LoadModule directive.
  • <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 under sites-available/ and sites-enabled/.
  • a2enconf / a2disconf — for global snippets under conf-available/ and conf-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

  1. 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.

  2. 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 apache2

    mod_http2 is one of the modules that needs a full restart, not just a reload, because it interacts with the MPM. The helper tells you which.

  3. Disable a module. Symmetrically:

    sudo a2dismod autoindex
    sudo apache2ctl configtest
    sudo systemctl reload apache2

    If a vhost or .htaccess references 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.

  4. 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.txt

    Any subsequent unexplained drift in that file is visible in git diff.

  5. For multiple modules at once, list them in one command. The helper accepts a list:

    sudo a2enmod rewrite headers expires deflate
    sudo a2dismod status info

    mod_status and mod_info ship loaded on some installs and expose /server-status and /server-info. Disable them or restrict access if you have not deliberately configured the endpoints.

  6. 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 a2enmod is 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_prefork to mpm_event requires a2dismod mpm_prefork before a2enmod mpm_event, or the configtest fails with “MPM defined twice”. See MPM event vs prefork.
  • mod_php (when present) forces mpm_prefork because the bundled PHP SAPI is not thread-safe. Moving to PHP-FPM lets you switch to mpm_event and gain workers.
  • Disabling mod_setenvif breaks many BrowserMatch rules 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, and mod_userdir are 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.