Authentication & Access Control in Apache

From the Apache cheat sheet ยท Security & Access Control ยท verified Jul 2026

Authentication & Access Control

Password-protect directories and restrict access by IP

apacheconf
# Password-protect a directory
<Directory /var/www/admin>
    AuthType Basic
    AuthName "Admin Area"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

# Create password file
# htpasswd -c /etc/apache2/.htpasswd admin
๐Ÿ’ก htpasswd -c creates the file โ€” omit -c when adding users to an existing file or it overwrites
โšก Options -Indexes prevents directory listing โ€” always disable this in production
๐Ÿ“Œ Block .env, .git, and backup files with FilesMatch โ€” they should never be web-accessible
๐ŸŸข Combine Require valid-user + Require ip inside <RequireAll> for defense in depth
authhtpasswdaccess-control

More Apache tasks

Back to the full Apache cheat sheet