Apache: create a password protected directory

It’s is useful to protect a directory with a password and to allow a directory index. In order to this you need to create a .htaccess file in the directory you created, with these┬álines:

AuthType Basic
AuthName "Restricted Files"
AuthBasicProvider file
AuthUserFile /usr/local/apache2/bin/.htpassword
Require valid-user
Options +Indexes
IndexOptions FancyIndexing

You will need to create the .htpassword like this:

/usr/local/apache2/bin/htpasswd -bc .htpassword username password

Replace the username and password with the ones that you want to use. You can create more then one user like that, just leave out the ‘c’ in the command flags then because that means ‘create a new file’.

Leave a Reply