Search

Thursday 5 May 2011

Password protect folder in Apache

If you need to setup basic authentication and a protected folder, use the following procedure:

  • Setup a basic Apache build with document root set to /htdocs/
  • Create the folder you want to protect under /htdocs/ - for example /htdocs/secret/
  • Create a text file in your 'secret' folder called - '.htaccess' (no quotes)
  • Populate this file with the settings (directives) that you want you 'secret' folder to have, in this case:

AuthType Basic
AuthName "Secret Web"
AuthUserFile /srv/www/.htpasswd
Require valid-user

  • Now create a text file called '.htpasswd' (again no quotes) in /srv/www or somewhere other than your Apache document root
  • Edit this and using a password encryption generation tool (http://www.tools.dynamicdrive.com/password/) create a username/password combination. It will look something like this:
cheese:y4E7Ep8e7EYV

If you now try to access your 'secret' URL you will be prompted for your username and password.