Directory listing is one of those Apache defaults that often turns on by accident — an Options +Indexes deep in an Include, or Options All somewhere — and starts publishing the contents of /uploads or /backups to anyone who asks. This article is the audit, the disable-by-default policy, and the legitimate cases where directory listing is a feature (file mirrors, internal download pages) and how to tune it cleanly.
How to verify
Check what Options are set across the config tree and whether any vhost is exposing directory listing:
grep -rE 'Options.*Indexes|<IfModule.*autoindex' /etc/apache2/sites-enabled/ /etc/apache2/conf-enabled/ /etc/apache2/apache2.conf
curl -sI https://example.com/uploads/ | head -3
curl -s https://example.com/uploads/ | grep -i '<title>Index of'
If the curl returns HTML with <title>Index of /uploads</title>, directory listing is on. That title is mod_autoindex’s signature; it appears any time autoindex serves a directory.
What’s happening
When Apache receives a request for a URL that maps to a directory (e.g. /uploads/ for /var/www/example.com/public/uploads/), it checks the DirectoryIndex directive — typically index.html index.php. If a matching file exists, Apache serves it. If not, the behavior depends on the Options Indexes setting:
Options +Indexes(orOptions Indexesin a fresh block) —mod_autoindexgenerates an HTML listing of the directory contents and serves it.Options -Indexes— Apache returns 403 Forbidden.
The default in modern Apache packages (Ubuntu, Debian) is Options Indexes FollowSymLinks in the global <Directory /var/www> block. That means any new directory without an index file shows its contents publicly.
The audit-and-fix is simple: change the global default to Options -Indexes, then explicitly enable indexes only in directories where listing is intentional.
The procedure
-
Audit the current global setting. In
/etc/apache2/apache2.confor/etc/apache2/conf-enabled/:grep -B1 -A3 '<Directory /var/www' /etc/apache2/apache2.confThe default block usually looks like:
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> -
Disable Indexes globally. Change to:
<Directory /var/www/> Options FollowSymLinks AllowOverride None Require all granted </Directory>Note the absence of
Indexes. The-Indexesform also works:Options -Indexes +FollowSymLinks -
Disable autoindex module if not needed anywhere. If no directory on the box needs auto-listing:
sudo a2dismod autoindex sudo apache2ctl configtest sudo systemctl reload apache2With the module gone, even an accidental
Options +Indexescannot serve a listing —mod_autoindexis the engine that builds the HTML. -
For directories where listing is a feature, enable it explicitly. For an internal download repository:
<Directory /var/www/internal/downloads> Options +Indexes IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst NameWidth=40 SuppressDescription IndexIgnore .* *.tmp *~ Require ip 10.0.0.0/8 </Directory>IndexOptionscontrols the listing format.FancyIndexingadds icons and sortable columns.IndexIgnorehides dotfiles and temporary files.Require iprestricts access to internal networks. -
For public mirror sites, where directory listing is the entire point:
<Directory /var/www/mirror> Options +Indexes IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst NameWidth=* AddDescription "Ubuntu LTS releases" releases HeaderName HEADER.html ReadmeName README.html </Directory>HeaderNameandReadmeNameinject HTML before and after the listing. -
Reload and verify.
sudo apache2ctl configtest sudo systemctl reload apache2 curl -sI https://example.com/uploads/ # should be 403 now curl -sI https://example.com/internal/downloads/ # should still serve listing if VPN'd in -
Set up a CI check. A common drift is a developer dropping
Options Allin a.htaccessor new vhost. A grep in CI:grep -rE 'Options.*Indexes|Options All' /etc/apache2/ && exit 1 || exit 0Run this in your config repo’s pre-commit or CI.
Common pitfalls
- A subdirectory
Options +Indexesoverrides the parentOptions -Indexes— vhost-level discipline is necessary; setting it only at the global level is not enough. Options AllincludesIndexes— usingOptions Allanywhere is rarely correct.- Some packages (vendor-supplied web apps, control panels) ship
.htaccessfiles withOptions +Indexes—AllowOverride Nonedefends against them; see .htaccess pitfalls. - A directory served by a front controller (WordPress, Laravel) does not need
Indexesbecause the rewrite catches every request — disabling globally has no impact on app-served URLs. - An empty
index.htmlis enough to suppress directory listing without changingOptions— a quick mitigation for a single directory, but not a substitute for the global config.
Stack Harbor sets Options -Indexes globally on every Apache host we onboard, with explicit +Indexes only in documented exceptions. The audit is a one-time pass during onboarding, then a CI check defends against drift. The discipline is part of the Managed Operations hardening baseline alongside other small but compounding defaults.